Looking for met1501s25-a.sg test answers and solutions? Browse our comprehensive collection of verified answers for met1501s25-a.sg at distance3.sg.digipen.edu.
Get instant access to accurate answers and detailed explanations for your course questions. Our community-driven platform helps students succeed!
Given the following definition statement:
char a[] = "Digipen";evaluate expression sizeof(a). If the expression cannot be compiled, write [for compile-time error]. If the expression generates undefined behavior, write [for undefined behavior]. Otherwise, write the exact value resulting from the expression's evaluation.
Brief side-note on undefined behavior: The C standard says that statements such as c = (b = a + 2) - (a = 1); and c = (b = a + 2) - (a = 1); cause undefined behavior [because we don't know whether the left or right operand of operator - is evaluated first]. When a program ventures into the realm of undefined behavior, all bets are off. The program may behave differently when compiled with different compilers. But that's not the only thing that can happen. The program may not compile in the first place, if it compiles it may not run, and if it does run, it may crash, behave erratically, or produce meaningless results. In other words, undefined behavior should be avoided like the plague.
It is a compiler error to subtract two pointers that are referencing different arrays.
It is a compiler error to use pointer arithmetic with a pointer that does not reference an array.
Adding to a pointer increases the address stored in the pointer by byte.
Given the definitions
int array[1000];
int *pa;
is the expression pa = array valid?
When we define an array, as in
int x[3];the name x refers to a set of unnamed int elements that are contiguously stored in memory. We make references to each of these three unnamed int elements using the subscript or index or array operator []
C/C++ arrays are always "-origin". That is, the definition int x[3] defines the elements x[0], x[1], and x[2].
Given the following declaration statement:
float x[] = {1.1f, 2.2f, 3.3f};write the exact value resulting from the evaluation of expression sizeof(x[1]).
An array definition consists of a declaration specifier, an identifier, a dimension, followed by an optional comma-separated list of values enclosed in braces:
type-specifier name [ dimension ] = {initialization-list}opt;can be any of the simple data types we've studied so far including signed and unsigned versions of char, int, long, and floating-point types such as float, double, and long double except void. Later in the course we'll study about storage specifiers and type qualifiers and expand on the declaration specifier.
Square brackets surround dimension which specifies the number of elements in the array. dimension must be a constant integral expression, that is, it must be possible to evaluate the value of dimension at compile-time and the type of the expression must be integral. Note that the constant integral expression dimension must evaluate to a value greater than or equal to . Note that C99 introduced variable-length arrays which allows the dimension to be specified at run-time. The committee changed its mind and C11 makes variable-length arrays optional. But the big thing is that variable-length arrays have always been illegal in C++. Therefore, assume throughout this course and all assessments related to this course that variable-length arrays are illegal; and this is enforced during compilation by using GCC option -Werror=vla.
initialization-list sets array elements to specific values and is optional. The number of values in the initialization list may be less than dimension, but not more. dimension is optional if you include initialization-list and explicitly initialize all values.
Arrays are not first-class citizens: an array cannot be initialized with another array, nor can one array be assigned to another. To copy one array into another, each element must be copied in turn.
The only operations that can be performed directly on an array name are the application of the sizeof and address-of & operators. For sizeof operator, the result is the number of bytes occupied by the array. That is, if each element of the array is of type T and the array dimension is n, the result of the sizeof operator is equal to n times sizeof(T).
Does the following array definition compile?
int x[100];If the following code fragment cannot be compiled, write [for compile-time error]. If the code fragment generates undefined behavior, write [for undefined behavior]. Otherwise, write the exact text written to the standard output stream.
int list[] = {2, 1, 2, 1, 1, 2, 3, 2, 1, 2};
printf("%d|", list[2]);
printf("%d|", list[list[2]]);
printf("%d|", list[list[2]+list[3]]);
printf("%d", list[list[list[2]]]);
Brief side-note on undefined behavior: The C standard says that statements such as c = (b = a + 2) - (a = 1); and c = (b = a + 2) - (a = 1); cause undefined behavior [because we don't know whether the left or right operand of operator - is evaluated first]. When a program ventures into the realm of undefined behavior, all bets are off. The program may behave differently when compiled with different compilers. But that's not the only thing that can happen. The program may not compile in the first place, if it compiles it may not run, and if it does run, it may crash, behave erratically, or produce meaningless results. In other words, undefined behavior should be avoided like the plague.
If the following code fragment cannot be compiled, write [for compile-time error]. If the code fragment generates undefined behavior, write [for undefined behavior]. Otherwise, write the exact text written to the standard output stream.
int i, grades[] = {-10,20,-30,40,-50};
for (i = sizeof(grades)/sizeof(grades[0]); i >= 0; --i) {
printf("%d%s", grades[i],(i==0?"":","));
}
Brief side-note on undefined behavior: The C standard says that statements such as c = (b = a + 2) - (a = 1); and c = (b = a + 2) - (a = 1); cause undefined behavior [because we don't know whether the left or right operand of operator - is evaluated first]. When a program ventures into the realm of undefined behavior, all bets are off. The program may behave differently when compiled with different compilers. But that's not the only thing that can happen. The program may not compile in the first place, if it compiles it may not run, and if it does run, it may crash, behave erratically, or produce meaningless results. In other words, undefined behavior should be avoided like the plague.
If the following code fragment cannot be compiled, write [for compile-time error]. If the code fragment generates undefined behavior, write [for undefined behavior]. Otherwise, write the exact text written to the standard output stream.
int i, list[10] = {0};
/* some other code here */
for (i = 0; i <= 5; ++i) list[2*i+1] = i+2;
for (i = 0; i < 10; ++i) printf("%d|", list[i]);
Brief side-note on undefined behavior: The C standard says that statements such as c = (b = a + 2) - (a = 1); and c = (b = a + 2) - (a = 1); cause undefined behavior [because we don't know whether the left or right operand of operator - is evaluated first]. When a program ventures into the realm of undefined behavior, all bets are off. The program may behave differently when compiled with different compilers. But that's not the only thing that can happen. The program may not compile in the first place, if it compiles it may not run, and if it does run, it may crash, behave erratically, or produce meaningless results. In other words, undefined behavior should be avoided like the plague.