CS 223 Multi-D Array in Class Exercise
CS 223 Multi-D Array in Class Exercise
dimensional arrays
Single dimensional array
1. Declare a 16-integer array ilist and assign 1, 2, and 3 to the first three elements.
int ilist[16];
ilist[1] = 1;
ilist[2]=2;
ilist[3]=3;
2. Given ilist declared in question 1, write a statement to assign the forth element (at index 3) with
the sum of first three elements in ilist, (index at 0, 1, 2)
int ilist[16];
ilist[1] = 1;
ilist[2]=2;
ilist[3]=3;
ilist[4] = ilist[1]+ilist[2]+ilist[3];
3. Declare a 32-char array name and assign string “Hello” as its initialization value.
4. Given name declared in question 3, write a statement to print out the string content in name.
2D array
5. Declare an 2D 2×8 integer array iMatrix and initialize all elements with 0.
int iMatrix[2][8] = 0;
6. Use iMatrix declared in question 5, write a statement to set the element at row 0 and column
7 in iMatrix to 100.
int iMatrix[2][8] = 0;
iMatrix[0][7] = 100;
7. Declare 2D 4×8 char array strlist and use following strings to initialize rows: “abc”, “def”,
“ghi”, and “jkl”.
char strlist[4][8];
strlist[1][] = abc;
strlist[2][] = def;
strlist[3][] = ghi;
strlist[4][] = jkl;
8. Given strlist declared in question 7, write a statement to print out the content in row 2 (the 3rd
row) of strlist.
printf(“%s”, strlist[2] );
--------------------------------------------
3D array
9. Declare 3D 9×9×9 integer array cube and initialize all elements with 0.
int cube[9][9][9] = 0;
10. Given cube declared in question 9, write a statement to set the center of 3D cube to 1.
int cube[9][9][9] = 0;
cube[5][5][5]= 1;