Download presentation
Presentation is loading. Please wait.
1
Multidimensional Arrays
2
Example Write a program to keep track of all warmup scores for all students. Need a list of a list of scores Student – score 1 score 2 … 1 3 2 … 2 3 3 …
3
Multidimensional Arrays double warmups[14][30]; Declares a multidimensional array with 14 rows and 30 columns … 0 1 13 0 1 2 29
4
Example Set first warmup score for first student to 3 … 0 1 13 0 1 2 29
5
Example Set first warmup score for first student to 3 warmups[0][0] = 3; … 0 1 13 0 1 2 29
6
Example Print all scores for all students … 0 1 13 0 1 2 29
7
Example Print all scores for all students #define ROWS 14 #define COLS 30 void print(double warmups[][30]) { int i, j; for(i = 0; i < ROWS; i++) { for(j = 0; j < COLS; j++) { printf(“Score: %lf “, warmups[i][j]); } printf(“\n”); } Must specify number of columns when passing as a parameter!
8
Array of Characters char my_chars[20] = {‘S’, ‘a’, ‘m’, ‘i’}; char name[20] = “Sami”; //String Sami my_chars: ?…? Sami name: \0…? Null character – C string-handling functions will ignore rest of array
9
Strings char name[20] = “Sami”; printf(“Name: %s\n”, name); printf(“Enter name change: “); scanf(“%s”, name); char class[15][20]; printf(“Enter student: “); scanf(“%s”, class[0]); printf(“Student %s”, class[0]);
10
More String Functions if(strcmp(name1, name2) == 0) negative – name1 before name2 (alphabetically) zero – strings are equal positive – name2 before name1
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.