9th Lecture
9th Lecture
1
1-D Arrays
• A program to add two arrays.
2
1-D array
Lab work:
1- read an array and check how many prime numbers in this array?
3
2-D arrays
An array is useful for storing and working with a set of data. Sometimes, though, it’s necessary to
work with multiple sets of data.
For example, in a grade-averaging program a teacher might record all of one student’s test scores
in an array of doubles.
If the teacher has 30 students, that means she’ll need 30 arrays of doubles to record the scores for
the entire class. Instead of defining 30 individual arrays, however, it would be better to define a
two-dimensional array.
4
Declaring 2-D array
To define a two-dimensional array, two size declarators are required. The first one is
for the number of rows, and the second one is for the number of columns.
double scores[3][4];
The element of the first row (row 0) The elements of row 1 are The elements of row 2 are
Scores [2][1]=92.25; 5
Initialization 2-D array
One way of initializing a two-dimensional array is
7
2-D array
8
2-D array
9
The End
10