C Programming Exam 5
C stores lists of values in _________.
arrays.
Write statements to accomplish each of the following: Total the elements of floating-point array c of 100 elements.
for ( loop = 0; loop <= 99; ++loop ) sum += c[ loop ];
The elements of an array are related by the fact that they ________.
have the same name and type.
Consider a 2-by-5 integer array t. Write a definition for t.
int t[ 2 ][ 5 ];
Write statements to accomplish the following: Define table to be an integer array and to have 3 rows and 3 columns. Assume the symbolic constant SIZE has been defined to be 3.
int table[ SIZE ][ SIZE ];
Follow the instructions below regarding an array called fractions. Define a symbolic constant SIZE to be replaced with the replacement text 10.
#define SIZE 10
Consider a 2-by-5 integer array t. How many elements does t have?
10
An array that uses two subscripts is referred to as a(n) _________ array.
Double-subscripted
Find the error(s) in each of the following statements: double f[ 3 ] = { 1.1, 10.01, 100.001, 1000.0001 };
Too many initializers. double f[ 3 ] = { 1.1, 10.01, 100.01 };
Find the error(s) in each of the following statements: Assume: double d[ 2 ][ 10 ]; d[ 1, 9 ] = 2.345;
d[ 1 ][ 9 ] = 2.345;
Write statements to accomplish each of the following: Input a value into element 4 of single-subscripted floating-point array b
scanf( "%f", &b[ 4 ] );
State whether the following are true or false. If the answer is false, explain why. It is an error if an initializer list contains more initializers than there are elements in the array.
True.
State which of the following are true and which are false. If false, explain why. An array definition reserves space for the array.
True.
State which of the following are true and which are false. If false, explain why. The mean, median and mode of the following set of values are 5, 6 and 7, respectively: 1, 2, 5, 6, 7, 7, 7.
True.
The name of the element in row 3 and column 5 of array d is ________.
d[ 3 ][ 5 ].
Consider a 2-by-5 integer array t. Write a series of statements that initialize each element of t to zero. Do not use a repetition structure.
t[ 0 ][ 0 ] = 0; t[ 0 ][ 1 ] = 0; t[ 0 ][ 2 ] = 0; t[ 0 ][ 3 ] = 0; t[ 0 ][ 4 ] = 0; t[ 1 ][ 0 ] = 0; t[ 1 ][ 1 ] = 0; t[ 1 ][ 2 ] = 0; t[ 1 ][ 3 ] = 0; t[ 1 ][ 4 ] = 0;
Consider a 2-by-5 integer array t. Write the names of all the elements in the third column of t.
t[ 0 ][ 2 ], t[ 1 ][ 2 ].
Consider a 2-by-5 integer array t. Write the names of all the elements in the second row of t.
t[ 1 ][ 0 ], t[ 1 ][ 1 ], t[ 1 ][ 2 ], t[ 1 ][ 3 ], t[ 1 ][ 4 ].
Consider a 2-by-5 integer array t. Write a single statement that sets the element of t in row 1 and column 2 to zero.
t[ 1 ][ 2 ] = 0;
The contents of a particular element of an array is called the ________ of that element.
value.
Consider a 2-by-5 integer array t. How many rows does t have?
2
Consider a 2-by-5 integer array t. How many columns does t have?
5
Lists and tables of values are stored in _________.
Arrays
Find the error in each of the following program segments and correct the error. Assume int a[ 2 ][ 2 ] = { { 1, 2 }, { 3, 4 } }; a[ 1, 1 ] = 5;
Error: Array subscripting done incorrectly. Correction: Change the statement to a[ 1 ][ 1 ] = 5;
Find the error in each of the following program segments and correct the error. SIZE = 10;
Error: Assigning a value to a symbolic constant using an assignment statement. Correction: Assign a value to the symbolic constant in a #define preprocessor directive without using the assignment operator as in #define SIZE 10.
Find the error in each of the following program segments and correct the error. #define VALUE = 120
Error: Assigning a value to a symbolic constant using an assignment statement. Correction: Assign a value to the symbolic constant in a #define preprocessor directive without using the assignment operator as in #define VALUE 120.
Find the error in each of the following program segments and correct the error. Assume int b[ 10 ] = { 0 }, i; for ( i = 0; i <= 10; ++i ) b[ i ] = 1;
Error: Referencing an array element outside the bounds of the array (b[ 10 ]). Correction: Change the final value of the control variable to 9.
Find the error in each of the following program segments and correct the error. #define SIZE 100;
Error: Semicolon at end of #define preprocessor directive. Correction: Eliminate semicolon.
Find the error in each of the following program segments and correct the error. #include <stdio.h> ;
Error: Semicolon at end of #include preprocessor directive. Correction: Eliminate semicolon.
State whether the following are true or false. If the answer is false, explain why. An array can store many different types of values.
False. An array can store only values of the same type.
State whether the following are true or false. If the answer is false, explain why. An array subscript can be of data type double.
False. An array subscript must be an integer or an integer expression.
State whether the following are true or false. If the answer is false, explain why. If there are fewer initializers in an initializer list than the number of elements in the array, C automatically initializes the remaining elements to the last value in the list of initializers.
False. C automatically initializes the remaining elements to zero.
State whether the following are true or false. If the answer is false, explain why. An individual array element that is passed to a function as an argument of the form a[i] and modified in the called function will contain the modified value in the calling function.
False. Individual elements of an array are passed by value. If the entire array is passed to a function, then any modifications to the elements will be reflected in the original.
State which of the following are true and which are false. If false, explain why. A C program that totals the elements of a double-subscripted array must contain nested for statements.
False. It is possible to total the elements of a double-subscripted array by enumerating all the elements in a calculation; however, it's much simpler to use nested loops.
State which of the following are true and which are false. If false, explain why. A C program that initializes the elements of a 15-element array to zero must contain one for statement.
False. The elements of an array can be initialized in the definition.
State which of the following are true and which are false. If false, explain why. To refer to a particular location or element within an array, we specify the name of the array and the value of the particular element.
False. We specify the name and the subscript of the element.
State which of the following are true and which are false. If false, explain why. To indicate that 100 locations should be reserved for integer array p, the programmer writes the definition p[ 100 ];
False. You'd begin the definitions with int.
Write statements to accomplish the following: How many elements does the array table contain? Print the total number of elements.
Nine elements. printf( "%d\n", SIZE * SIZE );
Determining whether an array contains a certain key value is called ___________ the array.
Searching
The process of placing the elements of an array in order is called __________ the array.
Sorting
The number used to refer to a particular element of an array is called its _______.
Subscript
A(n) _________ should be used to specify the size of an array because it makes the program more scalable.
Symbolic constant
Find the error(s) in each of the following statements: Assume: int a[ 3 ]; printf( "$d %d %d\n", a[ 1 ], a[ 2 ], a[ 3 ] );
a[ 3 ] is outside the bounds of the array. printf( "%d %d %d\n", a[ 0 ], a[ 1 ], a[ 2 ] );
In a double-subscripted array, the first subscript (by convention) identifies the ___________ of an element and the second subscript (by convention) identifies the ___________ of an element.
row, column.
Naming an array, stating its type and specifying the number of elements in the array is called ________ the array.
defining.
Follow the instructions below regarding an array called fractions. Define an array with SIZE elements of type double and initialize the elements to 0.
double fractions[ SIZE ] = { 0.0 };
Consider a 2-by-5 integer array t. Write a nested for statement that initializes each element of t to zero.
for ( i = 0; i <= 1; ++i ) for ( j = 0; j <= 4; ++j) t[ i ][ j ] = 0;
Consider a 2-by-5 integer array t. Write a statement that inputs the values for the elements of t from the terminal.
for ( i = 0; i <= 1; ++i ) for ( j = 0; j <= 4; ++j) { printf( "%s", "Enter an integer: " ); scanf( "%d", &t[ i ][ j ] ); }
Consider a 2-by-5 integer array t. Write a statement that displays the elements of the first row of t.
for ( i = 0; i <= 4; ++i ) printf( "%d ", t[ 0 ][ i ] );
Write statements to accomplish each of the following: Copy array a into the first portion of array b. Assume double a[ 11 ], b[ 34 ];
for ( loop = 0; loop <= 10; ++loop ) b[ loop ] = a[ loop ];
Write statements to accomplish each of the following: Initialize each of the 5 elements of single-subscripted integer array g to 8.
for ( loop = 0; loop <= 4; ++loop ) g[ loop ] = 8;
Write statements to accomplish the following: Print the values of each element of array table. Assume the array was initialized with the definition: int table[ SIZE ][ SIZE ] = { { 1, 8 }, { 2, 4, 6 }, { 5 } };
for ( x = 0; x < SIZE; ++x ) for ( y = 0; y < SIZE; ++y ) printf( "table[%d][%d] = %d\n", x, y, table[ x ][ y ] ); Output: table[0][0] = 1 table[0][1] = 8 table[0][2] = 0 table[1][0] = 2 table[1][1] = 4 table[1][2] = 6 table[2][0] = 5 table[2][1] = 0 table[2][2] = 0
Write statements to accomplish the following: Use a for repetition statement to initialize each element of table to the sum of its subscripts. Assume the integer variables x and y are defined as control variables.
for ( x = 0; x < SIZE; ++x ) for ( y = 0; y < SIZE; ++y ) table[ x ][ y ] = x + y;
Follow the instructions below regarding an array called fractions. Print all the elements of the array, using a for repetition statement. Assume the integer variable x has been defined as a control variable for the loop. Show the output.
for ( x = 0; x < SIZE; ++x ) printf( "fractions[%d] = %f\n", x, fractions[ x ] ); Output: fractions[0] = 0.000000 fractions[1] = 0.000000 fractions[2] = 0.000000 fractions[3] = 0.000000 fractions[4] = 0.000000 fractions[5] = 0.000000 fractions[6] = 3.333000 fractions[7] = 0.000000 fractions[8] = 0.000000 fractions[9] = 1.667000
Follow the instructions below regarding an array called fractions. Refer to array element 4.
fractions[ 4 ]
Follow the instructions below regarding an array called fractions. Assign the value 3.333 to the seventh element of the array.
fractions[ 6 ] = 3.333;
Follow the instructions below regarding an array called fractions. Assign the value 1.667 to array element nine.
fractions[ 9 ] = 1.667;
An m-by-n array contains _______ rows, ________ columns and _______ elements.
m, n, m * n.
The names of the five elements of array p are _____, _____, _____, ______ and _______.
p[ 0 ], p[ 1 ], p[ 2 ], p[ 3 ], p[ 4 ].
Follow the instructions below regarding an array called fractions. Print array elements 6 and 9 with two digits of precision to the right of the decimal point, and show the output that is displayed on the screen.
printf( "%.2f %.2f\n", fractions[ 6 ], fractions[ 9 ] ); Output: 3.33 1.67.
Write statements to accomplish each of the following: Display the value of the seventh element of character array f.
printf( "%c\n", f[ 6 ] );
Consider a 2-by-5 integer array t. Write a series of statements that print the array t in tabular format. List the column subscripts as headings across the top and list the row subscripts at the left of each row.
printf( "%s", " 0\t1\t2\t3\t4\n" ); for ( i = 0; i <= 1; ++i ) { printf( "%d ", i ); for ( j = 0; j <= 4; ++j ) printf( "%d\t", t[ i ][ j ] ); puts( "" ); }
Write statements to accomplish each of the following: Determine and print the smallest and largest values contained in 99-element floating point array w
smallest = largest = w[ 0 ]; for ( loop = 1; loop <= 98; ++loop ) if ( w[ loop ] < smallest ) smallest = w[ loop ]; else if ( w[ loop ] > largest ) largest = w[ loop ];
Consider a 2-by-5 integer array t. Write a series of statements that determine and print the smallest value in array t.
smallest = t[ 0 ][ 0 ]; for ( i = 0; i <= 1; ++i ) for ( j = 0; j <= 4; ++j) if ( t[ i ][ j ] < smallest ) smallest = t[ i ][ j ]; printf( " smallest is %d\n", smallest );
The process of placing the elements of an array into either ascending or descending order is called _______.
sorting.
Find the error(s) in each of the following statements: Assume: char str[ 5 ]; scanf( "%s", str ); // User types hello
str needs a minimum length of 6; one element for each letter in hello and an element for the terminating NULL character.
When referring to an array element, the position number contained within square brackets is called a(n) ________.
subscript.
Consider a 2-by-5 integer array t. Write a statement that totals the elements of the fourth column of t.
sum = t[ 0 ][ 3 ] + t[ 1 ][ 3 ];