0% found this document useful (0 votes)
40 views

CS 223 Multi-D Array in Class Exercise

1) The document provides examples of declaring and initializing single dimensional, 2D, and 3D arrays in C. 2) It shows how to declare a 16 integer array, assign values to the first 3 elements, and set the 4th element to the sum of the first 3. 3) Examples are also given for declaring and initializing a 2D array, accessing elements, and printing string values from a 2D array.

Uploaded by

Zack Overfield
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

CS 223 Multi-D Array in Class Exercise

1) The document provides examples of declaring and initializing single dimensional, 2D, and 3D arrays in C. 2) It shows how to declare a 16 integer array, assign values to the first 3 elements, and set the 4th element to the sum of the first 3. 3) Examples are also given for declaring and initializing a 2D array, accessing elements, and printing string values from a 2D array.

Uploaded by

Zack Overfield
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

CS 223 In Class Exercise – arrays and multi-

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.

char name[32]= Hello;

4. Given name declared in question 3, write a statement to print out the string content in name.

char name[32]= Hello;


printf(“Hello \n“, 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;

You might also like