Chapter 7
Arrays
PROGRAMMING IN ANSI C
2
5/14/2024
Question
 Questions:
How do we read in 100 scores and decide the every
grade of the score according to the average score?
 If a score is 10 or above greater than the average,
it is classified as grade 'A';
 If a score is 10 or above less than the average, it
is classified as grade 'C';
 Others are classified as grade 'B'.
3
5/14/2024
Question
main()
{ float s1, s2, …, s100, ave;
char grade1, grade2, ..., grade100;
scanf ( "%f", &s1 );
scanf ( "%f", &s2 );
……
ave = (s1 + s2 + … + s100) /100;
if ( s1 >= ave + 10 ) grade1 = 'A';
else if ( s1 < ave – 10 ) grade1 = 'C';
else grade1 = 'B';
if ( s2 >= ave + 10 ) grade2 = 'A';
……
}
 This program uses 100
variables s1, s2, ..., s100 to
deal with similar data and
do similar operations.
 We can use a structure to
organize those similar
variables – Array.
Array is a derived data type.
An array is a fixed-size sequenced collection
of elements of the same data type.
4
5/14/2024
Question
main()
{ float s[100], ave = 0; int i;
char grade[100];
for ( i = 0; i < 100; i ++ )
{ scanf ( "%f", &s[i] );
ave = ave + s[i];
}
ave = ave / 100;
for ( i = 0; i < 100; i++ )
if ( s[i] >= ave + 10 ) grade[i] = 'A';
else if ( s[i] < ave – 10 ) grade[i] = 'C';
else grade[i] = 'B';
}
5
5/14/2024
Chapter 6
 In this chapter, we will learn:
 One-dimensional arrays
 Two-dimensional arrays
6
5/14/2024
valid identifier
the type of the elements
One-dimensional Arrays - Declaration
 The general form of array declaration:
type array_name [size];
e.g. int a[6]; An integer constant,
the maximum number of
elements can be stored
inside the array
a
a[5]
5
a[4]
4
a[3]
3
a[2]
2
a[1]
1
a[0]
0
Array name is the start address
of this array in memory, and its
value is an address constant.
The spaces of elements
are continuous.
The first element is a[0].
The subscripts begin with 0.
The array "a" contains 6 elements.
The last element is a[5].
The subscripts of array a[n]
end with n-1.
Don't use
a[6] !!!
7
5/14/2024
main()
{ int i, s = 10;
float f[s];
f[0] = 0;
for ( i = 1; i < s; i ++ )
f[i] = f[i - 1] + 1;
……
}
main()
{ int i, s = 10;
float f[10];
f[0] = 0;
for ( i = 1; i < s; i ++ )
f[i] = f[i - 1] + 1;
……
}
#define SIZE 10
main()
{ int i;
float f[SIZE];
f[0] = 0;
for ( i = 1; i < SIZE; i ++ )
f[i] = f[i - 1] + 1;
……
}
One-dimensional Arrays - Declaration
 When the elements of
an array are used, the
subscripts of an array
can be integer
constants or variables
or expressions.
 However, in the
declaration of an array,
the size can be only an
integer constant
expression.
Error … : Constant expression
required in function ...
8
5/14/2024
 Like primary variables, an array must be declared
before it is used.
 We can only use the elements one by one, and can't
use the whole array.
(Notice: The array name is the start address of this
array, but it can't represent the overall elements.)
e.g. int a[6];
……
printf ( "%d", a );
One-dimensional Arrays - Usage of
Elements

e.g. int a[6], i;
……
for ( i = 0; i <= 5; i++ )
printf( "%d", a[i] );
9
5/14/2024
One-dimensional Arrays - Usage of
Elements
#define SIZE 10
main()
{ int i;
float f[SIZE];
f[0] = 0;
for ( i = 1; i < SIZE; i ++ )
f[i] = f[i - 1] + 0.1;
for ( i = 0; i < SIZE; i = i + 2 )
printf ( "%4.1f", f[i] );
}
0.0 0.2 0.4 0.6 0.8
The declaration of array
The usage of array elements
10
5/14/2024
One-dimensional Arrays - Usage of
Elements
e.g. int a[6];
a[6] = 10;

a
a[5]
5
a[4]
4
a[3]
3
a[2]
2
a[1]
1
a[0]
0
The elements are from a[0] to a[5].
a[6] is invalid.
 The array name "a" is the start address of this array,
which is the address of the first element a[0].
 a[i] is the (i+1)th element, the subscript i represents the
offset from the start address of the array.
 When a element is used, the compiler calculate its
address according to the start address and the
subscript, and then take out the data from the address.
11
5/14/2024
One-dimensional Arrays - Usage of
Elements
main()
{
int a[2];
a[0] = 1;
a[1] = 2;
printf ( "%dn", a[0] );
printf ( "%dn", a[1] );
printf ( "%d %x", a, a );
}
1
2
- 42 ffd6
12
5/14/2024
One-dimensional Arrays - Usage of
Elements
main()
{
int a[2];
scanf ( "%d", &a[0] );
a[1] = a[0] * 2;
printf ( "%dn", a[0] );
printf ( "%dn", a[1] );
}
5
5
10
scanf ( "%d", a );
scanf ( "%d", a + 1 );
13
5/14/2024
One-dimensional Arrays - Initialization
1. Like primary variables, we can initialize the elements
of an array when the array is declared.
 The general form of initialization of array is:
type array-name[size] = { list of values }
The values in the list are separated by commas.
e.g. int a[5] = { 1, 2, 3, 4, 5 };
Equivalent to:
int a[5];
a[0] = 1; a[1] = 2; a[2] = 3; a[3] = 4; a[4] = 5;
14
5/14/2024
One-dimensional Arrays - Initialization
2. Initialization may be partial.
 The number of initializers may be less than the
declared size. In such cases, the remaining elements
are initialized to zero.
e.g. int a[5] = { 1, 2, 3 };
Equivalent to:
int a[5];
a[0] = 1; a[1] = 2; a[2] = 3; a[3] = a[4] = 0;
15
5/14/2024
One-dimensional Arrays - Initialization
2. If the number of initializers is more than the
declared size, the complier will produce an error.
e.g. int a[3] = { 1, 2, 3, 4, 5 };
Error … : Too many initializers in function ...
16
5/14/2024
One-dimensional Arrays - Initialization
3. The size may be omitted.
 In such cases, the compiler allocates enough space
for all initialized elements. If the number of
initializers is n, the declared size of the array is n.
e.g. int a[ ] = { 1, 2, 3, 4, 5 };
Equivalent to:
int a[5] = { 1, 2, 3, 4, 5 };
17
5/14/2024
One-dimensional Arrays - Initialization
4. If the elements are not initialized before they are
used in an expression, the result of this expression is
unexpected.
 Before the elements are used, they must be
initialized, whether in compile time or in run time.
e.g. int a[3];
printf ( "%d, %d, %d", a[0], a[1], a[2] );
0, 64, 3117
18
5/14/2024
One-dimensional Arrays - Program 1
 Read in 10 integers, and find out the maximum and
the minimum and the average.
 Step 1: Read 10 numbers into array "a".
 Step 2: max = min = sum = a[0];
 Step3: for ( i = 1; i < 10; i++ )
{ if ( a[i] > max ) max = a[i];
if ( a[i] < min ) min = a[i];
sum = sum + a[i]; }
 Step 4: ave = sum / 10;
 Step 5: Output the values of max, min and ave.
N-S Flow Chart
for i = 0 to 9
Read in a[i]
max = min = sum = a[0];
ave = (float) sum / 10
for i = 1 to 9
max = a[i]
Y N
max<a[i]
min = a[i]
Y N
min>a[i]
sum = sum + a[i]
Output: max, min, ave
19
5/14/2024
#define N 10
main()
{ int a[N], max, min, sum, i; float ave;
printf ( "Enter %d integers:n", N );
for ( i = 0; i < N; i++ ) scanf ( "%d", &a[i] );
max = min = sum = a[0];
for ( i = 1; i < N; i++)
{ if ( a[i] > max ) max = a[i];
if ( a[i] < min ) min = a[i];
sum = sum + a[i]; }
ave = (float) sum / N;
printf ("max = %d, min = %d, ave = %.1f", max, min, ave );
}
#define N 10
main()
{ int a[N], max, min, sum, i; float ave;
printf ( "Enter %d integers:n", N );
for ( i = 0; i < N; i++ ) scanf ( "%d", a + i );
max = min = sum = a[0];
for ( i = 1; i < N; i++)
{ if ( a[i] > max ) max = a[i];
if ( a[i] < min ) min = a[i];
sum = sum + a[i]; }
ave = (float) sum / N;
printf ("max = %d, min = %d, ave = %.1f", max, min, ave );
}
One-dimensional Arrays - Program 1
Enter 10 integers:
1 2 3 4 5 6 7 8 9 10
max = 10, min = 1, ave = 5.5
20
5/14/2024
One-dimensional Arrays - Program 2
 Read in 10 scores, and decide the every grade of the
score according to the average score.
 If a score is 10 or above greater than the average,
it is classified as grade 'A';
 If a score is 10 or above less than the average, it
is classified as grade 'C';
 Others are classified as grade 'B'.
 Step 1: Read in 10 scores (float type).
 Step 2: Calculate the average score.
 Step 3: Compare every score with the average to
decide the every grade, and then output it.
21
5/14/2024
One-dimensional Arrays - Program 2
#define N 10
main()
{ float score[N], ave, sum = 0; int i; char grade;
for ( i = 0; i < N; i++ )
{ printf ( "Please input the score of student %d:", i+1 );
scanf ( "%f", &score[i] );
sum = sum + score[i]; }
printf("The average is : %.1fn", ave = sum / N);
for ( i = 0; i < N; i++ )
{ if ( score[i] - ave >= 10 ) grade = 'A';
else if ( score[i] - ave <= -10 ) grade = 'C';
else grade = 'B';
printf( "The grade of student %d is: %cn", i+1, grade); }
}
22
5/14/2024
One-dimensional Arrays - Program 3
 Output the former 40 numbers of the Fibonacci
sequence.
 Fibonacci sequence: 1, 1, 2, 3, 5, 8, 13, 21, 34, ……
F1 = 1 (n = 1)
F2 = 1 (n = 2)
Fn = Fn-1 + Fn-2 (n≥3)
f[39]
39
f[5]
5
f[4]
4
f[3]
3
f[2]
2
f[1]
1
1
f[0]
1
0
2
3
5
8
……
23
5/14/2024
One-dimensional Arrays - Program 3
main()
{
int i;
long f[40] = { 1, 1 };
for ( i = 2; i < 40; i++ )
f[i] = f[i-2] + f[i-1];
for ( i = 0; i < 40; i++ )
{ if ( i%4 == 0)
printf ( "n" );
printf ( "%12ld", f[i] );
}
}
main()
{
long f1 = 1, f2 = 1;
int i;
for ( i = 1; i <= 20; i++ )
{ printf ( "%12ld %12ld", f1,
f2 );
if ( i%2 == 0)
printf ( "n" );
f1 = f1 + f2;
f2 = f2 + f1;
}
}
24
5/14/2024
One-dimensional Arrays - Program 4
 Read in 20 characters, find out the character
specified by user, and output its position.
 Step 1: Read 20 characters into array "c".
 Step 2: Read in the character which the user wants
to search, and assign to variable ch.
 Step3: Compare ch with the array "c" from c[0] to
[9] to test whether the array contains this
character or not.
25
5/14/2024
main()
{ char c[20], ch; int i;
printf ( "Please input the 20 characters:n" );
for ( i = 0; i < 20; i++ ) scanf ( "%c", &c[i] );
printf("What do you want to search?"); scanf("%c", &ch);
for ( i=0; i<20; i++)
{ if ( c[i] == ch )
{ printf("'%c' at the position %d.", ch, i+1 );
break; }
else if ( i == 19 ) printf ( "No found!" ); }
}
One-dimensional Arrays - Program 4
main()
{ char c[20], ch; int i;
printf ( "Please input the 20 characters:n" );
for ( i = 0; i < 20; i++ ) scanf ( "%c", &c[i] );
scanf ( "%c", &ch );
printf("What do you want to search?"); scanf("%c", &ch);
for ( i=0; i<20; i++)
{ if ( c[i] == ch )
{ printf("'%c' at the position %d.", ch, i+1 );
break; }
else if ( i == 19 ) printf ( "No found!" ); }
}
Please input the 20 characters:
abcdefghijklmnopqrst
What do you want to search?No found!
Please input the 20 characters:
abcdefghijklmnopqrst
What do you want to search?
'd' at the positon 4.
d
26
5/14/2024
One-dimensional Arrays - Program 5
 Bubble sort: Arrange elements in the list according to
their values, in ascending order.
 Method: Compare the two adjacent numbers, and
put the larger number on the latter position.
 Step 1: Read n numbers into the array a.
27
5/14/2024
One-dimensional Arrays - Program 5
 Bubble sort: Arrange elements in the list according to
their values, in ascending order.
 Method: Compare the two adjacent numbers, and
put the larger number on the latter position.
 Step 2: Compare the 1st and the 2nd number, and
put the larger on the latter position. Then compare
the 2nd and the 3rd number, and put the larger on
the latter position ... In the same way, until we have
compared and put the (n-1)th and the nth number –
we put the largest number on the last position.
28
5/14/2024
One-dimensional Arrays - Program 5
 Bubble sort: Arrange elements in the list according to
their values, in ascending order.
 Method: Compare the two adjacent numbers, and
put the larger number on the latter position.
 Step 3: In the same way, we do with the former n-1
numbers, and the second largest number is put on
the last second position.
 Step 4: Repeat this process until all the numbers
are sorted.
29
5/14/2024
49 38 65 97 76 13 27 30
38 49 65 76 13 27 30 97
38
49
38 49 65
49
49 65 97
65
65 97 76
97
76 97 13
97
13 97 27
97
27 97 30
97
30 97
49
38
38 49 65
49
49 65 76
65
65 76 13
76
13 76 27
76
27 76 30
76
30 76
38 49 65 13 27 30 76 97
49
38
38 49 65
49
49 65 13
65
13 65 27
65
27 65 30
65
30 65
38 49 13 27 30 65 76 97
49
38
38 49 13
49
13 49 27
49
27 49 30
49
30 49
38 13 27 30 49 65 76 97
13
38
13 38 27
38
27 38 30
38
30 38
13 27 30 38 49 65 76 97
27
13
13 27 30
27
27 30
13 27 30 38 49 65 76 97
27
13
13 27
13 27 30 38 49 65 76 97
Comparison
7 times
6 times
5 times
4 times
3 times
2 times
1 time
One-dimensional Arrays - Program 5
a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7]
1st sorting
2nd sorting
3rd sorting
4th sorting
5th sorting
6th sorting
7th sorting
result
• To n numbers, altogether times of sorting.
• In ith sorting, altogether times of comparison.
n-1
"i" represents times of sorting: 1 <= i <= n-1
n-i
"j" represents the subscript of the compared
element : 1 <= j <= n-i
Every time, a[j-1] and a[j] are compared.
30
5/14/2024
One-dimensional Arrays - Program 5
#define N 10
main ( )
{ int a[N], i, j, t;
printf ( "Input %d numbers:n", N );
for ( i = 0; i <= N-1; i++) scanf ( "%d", &a[i] );
for ( i = 1; i <= N-1; i++ )
{ for ( j = 1; j <= N - i; j++ )
{ if ( a[j-1] > a[j] )
{ t = a[j-1];
a[j-1] = a[j];
a[j] = t; }
}
}
for ( i = 0; i < N; i++ ) printf ( "%d ",a[i] );
}
31
5/14/2024
One-dimensional Arrays - Program 6
 Selection sort: Arrange elements in the list according
to their values, in ascending order.
 Method: First select the smallest number and put it
on the first position. Then select the second
smallest number and put it on the second position...
In the same way, place all the numbers in ascending
order.
32
5/14/2024
49 38 65 97 76 13 27 30
a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] Comparison
7 times
6 times
5 times
4 times
3 times
2 times
1 time
result
One-dimensional Arrays - Program 6
13 38 65 97 76 49 27 30
13 38 65 97 76 49 27 30
13 27 65 97 76 49 38 30
13 27 65 97 76 49 38 30
13 27 30 97 76 49 38 65
13 27 30 97 76 49 38 65
13 27 30 38 76 49 97 65
13 27 30 38 76 49 97 65
13 27 30 38 49 76 97 65
13 27 30 38 49 65 97 76
13 27 30 38 49 76 97 65
13 27 30 38 49 65 97 76
13 27 30 38 49 65 76 97
13 27 30 38 49 65 76 97
1th sorting
2nd sorting
3rd sorting
4th sorting
5th sorting
6th sorting
7th sorting
• To n numbers, altogether times of sorting.
• In ith sorting, altogether times of comparison.
n-1
"i" represents times of sorting: 1 <= i <= n-1
n-i
"j" represents the subscript of the compared
element : i <= j <= n-1
Every sorting, the smallest element in the compared
elements is exchange with the element a[i-1].
33
5/14/2024
One-dimensional Arrays - Program 6
#define N 10
main()
{ int a[N], i, j, t, k;
printf ( "Input %d numbers:n", N );
for ( i = 0; i < N; i++ ) scanf ( "%d", &a[i] );
for( i = 1; i < N; i++ )
{ k = i - 1;
for ( j = i; j <= N - 1; j++ )
if ( a[k] > a[j] ) k = j;
if(k!=i-1)
{ t = a[k]; a[k] = a[i-1]; a[i-1] = t; }
}
for ( i = 0; i < N; i++ ) printf ( "%d ", a[i] );
}
34
5/14/2024
One-dimensional Arrays - Program 7
 Read 10 integers into an array, and then rearrange
the elements of this array in the inverted order.
 Step 1: Read 10 integers into the array a.
 Step 2: Exchange a[0] and a[9], a[1] and a[8]....
a[4] and a[5].
 Step 3: Output this rearranged array.
90
80
70
60
50
40
30
20
10
0 0
80
70
60
50
40
30
20
10
90 0
10
70
60
50
40
30
20
80
90 0
10
20
60
50
40
30
70
80
90 0
10
20
30
50
40
60
70
80
90 0
10
20
30
40
50
60
70
80
90
35
5/14/2024
One-dimensional Arrays - Program 7
main()
{ int a[10], i, j, t;
printf ( "Please input the 10 integers:" );
for ( i = 0; i < 10; i++ ) scanf ( "%d", &a[i] );
printf ( "The old sequence is: " );
for ( i = 0; i < 10; i++ ) printf ( "%d ", a[i] );
printf("n");
for ( i = 0, j = 9; i < j; i++, j-- )
{ t = a[i]; a[i] = a[j]; a[j] = t; }
printf("The new sequence is: ");
for ( i = 0; i < 10; i++ ) printf ( "%d ", a[i] );
}
 Consider: How to rearrange
n numbers in the inverted order?
for ( i = 0; i < 5; i++ )
{ t = a[i]; a[i] = a[9-i]; a[9-i] = t; }
36
5/14/2024
One-dimensional Arrays - Program 8
 10 candidates participate in an election campaign, and
100 persons vote for them. Calculate the ballot of each
candidate, and output the result with the histogram
expressed by "*".
 Step 1: Declare and initialize the array:
int count[11] = {0};
 Step 2: Read in the vote. If one vote is for
candidate n, add 1 to count[n].
 Step 3: Output the result and the corresponding “*”.
37
5/14/2024
One-dimensional Arrays - Program 8
main()
{ int count[11] = {0}, i, j, n;
printf ( "Please input the 100 votes:" );
for ( i = 0; i < 100; i++ )
{ scanf ( "%d", &n );
count[n]++;
}
printf ( "Grade:tCounttGraphn" );
for ( i = 1; i <= 10; i++ )
{ printf ( "Candidate%3d: %d ballotst", i, count[i] );
for ( j = 0; j < count[i]; j++ ) printf ( "*" );
printf ( "n" );
}
}
Candidates : Count Graph
Candidate 1: 5 ballots *****
Candidate 2: 6 ballots ******
Candidate 3: 10 ballots **********
Candidate 4: 12 ballots ************
Candidate 5: 2 ballots **
Candidate 6: 7 ballots *******
Candidate 7: 18 ballots ******************
Candidate 8: 28 ballots ****************************
Candidate 9: 8 ballots ********
Candidate 10: 4 ballots ****
38
5/14/2024
Question
 Questions:
In maths, we often use the matrix, such as Am×n.
How do we express it in our program?

















25
19
2
7
5
13
0
6
3
5
3
1
3
4
A














43
42
41
33
32
31
23
22
21
13
12
11
3
4
a
a
a
a
a
a
a
a
a
a
a
a
A
Two-dimensional array!
39
5/14/2024
 The general form of array declaration:
type array_name [row_size] [column_size];
e.g. int a[4][3];
 The storage of the elements :
 The memory is one-dimensional.
 In memory: After all the elements of one line are
stored, another line is stored.
int a[3][2]
5
4
3
2
1
0










]
1
][
2
[
]
0
][
2
[
]
1
][
1
[
]
0
][
1
[
]
1
][
0
[
]
0
][
0
[
a
a
a
a
a
a
Two-dimensional Arrays
a[2][1]
a[2][0]
a[1][1]
a[1][0]
a[0][1]
a[0][0]
int c[2][3][4]
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 c[1][2][3]
c[1][2][2]
c[1][2][1]
c[1][2][0]
c[1][1][3]
c[1][1][2]
c[1][1][1]
c[1][1][0]
c[1][0][3]
c[1][0][2]
c[1][0][1]
c[1][0][0]
c[0][2][3]
c[0][2][2]
c[0][2][1]
c[0][2][0]
c[0][1][3]
c[0][1][2]
c[0][1][1]
c[0][1][0]
c[0][0][3]
c[0][0][2]
c[0][0][1]
c[0][0][0]
40
5/14/2024
Two-dimensional Arrays
int a[3][4];
Each element a[i] is a
one-dimensional array
containing 4 elements.
0 a[0][0]
1 a[0][1]
2 a[0][2]
3 a[0][3]
4 a[1][0]
5 a[1][1]
6 a[1][2]
7 a[1][3]
8 a[2][0]
9 a[2][1]
10 a[2][2]
11 a[2][3]
a[0]
a[1]
a[2]
a[0][0]
a[0][1]
a[0][2]
a[0][3]
a[1][0]
a[1][1]
a[1][2]
a[1][3]
a[2][0]
a[2][1]
a[2][2]
a[2][3]
a[0][0] a[0][1] a[0][2] a[0][3]
a[1][0] a[1][1] a[1][2] a[1][3]
a[2][0] a[2][1] a[2][2] a[2][3]
The two-dimensional array a can
be regarded as a one-dimensional
array containing 3 elements.
a[2]
a[1]
a[0]
41
5/14/2024
Two-dimensional Arrays - Initialization
 A two-dimensional array can be initialized row by row.
int a[2][3] = { {1,2,0}, {4,0,0} };
Complete initialization
a[0][0] 1
a[0][1] 2
a[0][2] 0
a[1][0] 4
a[1][1] 0
a[1][2] 0
int a[2][3] = { {1,2}, {4} };
Partial initialization
42
5/14/2024
Two-dimensional Arrays - Initialization
 A two-dimensional array can be initialized row by row.
a[0][0] 1
a[0][1] 2
a[0][2] 0
a[1][0] 4
a[1][1] 0
a[1][2] 0
int a[ ][3] = { {1,2}, {4} };
The size of the first
dimension can be omitted.
Notice: The size of the second dimension
can't be omitted!
e.g. int a[2][ ] = {{1,2}, {4}} is wrong!
43
5/14/2024
Two-dimensional Arrays - Initialization
 A two-dimensional array can be initialized
with a list of initial values. a[0][0] 1
a[0][1] 2
a[0][2] 0
a[1][0] 4
a[1][1] 0
a[1][2] 0
int a[2][3] = { 1, 2, 0, 4, 0, 0 };
Complete initialization
int a[2][3] = { 1, 2, 0, 4 };
Partial initialization
44
5/14/2024
Two-dimensional Arrays - Initialization
 A two-dimensional array can be initialized
with a list of initial values. a[0][0] 1
a[0][1] 2
a[0][2] 0
a[1][0] 4
a[1][1] 0
a[1][2] 0
int a[ ][3] = { 1, 2, 0, 4};
The size of the first
dimension can be omitted.
Notice: The size of the second dimension
can't be omitted!
e.g. int a[2][ ] = { 1, 2, 0, 4 }; is wrong!
45
5/14/2024
 Calculate each row sum of the matrix a[3][4].
 Step 1: Declare and initialize a two-dimensional
array a[3][4].
 Step 2: Declare a one-dimensional array s[3] to
store each row sum.
 Step 3: Calculate the sum of the ith row, and store
it to the element s[i].
 Step 4: Output the value of the array s.
Two-dimensional Arrays - Program 1
46
5/14/2024
Two-dimensional Arrays - Program 1
main()
{ int a[3][4] = { {1,2,3,4}, {5,6,7}, {8,9,10} }, s[3];
int i, j, sum=0;
for ( i = 0; i < 3; i++, sum = 0)
{ for ( j = 0; j < 4; j++ )
sum = sum + a[i][j];
s[i] = sum;
}
for ( i = 0; i < 3; i++ )
printf("The row sum %d is: %dn",i+1,s[i]);
}
The row sum 1 is: 10
The row sum 2 is: 18
The row sum 3 is: 27
a[0][0] 1
a[0][1] 2
a[0][2] 3
a[0][3] 4
a[1][0] 5
a[1][1] 6
a[1][2] 7
a[1][3] 0
a[2][0] 8
a[2][1] 9
a[2][2] 10
a[2][3] 0
47
5/14/2024
Find the largest element from a matrix A3×4. Output
this element and its row number and column number.
 Step 1: Declare and initialize a two-dimensional
array a[3][4].
 Step 2: Declare and initialize the variable
max=a[0][0], row=0, column=0.
 Step 3: Find the largest element and use above 3
variables to store the largest element, the row
number and column number.
 Step 4: Output the values of max, row and column.
Two-dimensional Arrays - Program 2
48
5/14/2024
Two-dimensional Arrays - Program 2
main()
{ int a[3][4] = { {1,2,3,4}, {9,8,7,6}, {-10,10,-5,2} };
int max=a[0][0], row=0, colum=0, i, j;
for ( i = 0; i < 3; i++ )
for ( j = 0; j < 4; j++ )
if ( a[i][j] > max )
{ max = a[i][j];
row = i;
colum = j;
}
printf ( "max=%d, row=%d, column=%d",
max, row+1, colum+1 );
}
max=10, row=3, column=2
a[0][0] 1
a[0][1] 2
a[0][2] 3
a[0][3] 4
a[1][0] 9
a[1][1] 8
a[1][2] 7
a[1][3] 6
a[2][0] -10
a[2][1] 10
a[2][2] -5
a[2][3] 2
49
5/14/2024
Two-dimensional Arrays - Program 3
Initialize a matrix with 2 rows and 3 columns and
output it. Then transpose this matrix and output it.








6
5
4
3
2
1
3
2
A












6
3
5
2
4
1
2
3
B
transpose
50
5/14/2024
Two-dimensional Arrays - Program 3
Initialize a matrix with 2 rows and 3 columns and
output it. Then transpose this matrix and output it.
 Step 1: Declare and initialize 2 two-dimensional
arrays: a[2][3] and b[3][2].
 Step 2: Output the two-dimensional array a.
 Step 3: b[j][i] = a[i][j];
 Step 4: Output the two-dimensional array b.
51
5/14/2024
Two-dimensional Arrays - Program 3
main()
{ int a[2][3] = { 1, 2, 3, 4, 5, 6 }, b[3][2], i, j;
for ( i = 0; i < 2; i++ )
{ for ( j = 0; j < 3; j++ )
{ printf ( "%5d", a[i][j] );
b[j][i] = a[i][j];
}
printf ( "n" );
} printf ( "n" );
for ( i = 0; i < 3; i++ )
{ for ( j = 0; j < 2; j++ )
printf ( "%5d", b[i][j] );
printf ( "n" ); }
}
1 2 3
4 5 6
1 4
2 5
3 6
52
5/14/2024
Homework
 Review Questions P212
 7.1, 7.3~7.8 (Write down in your exercise book)
 Programming Exercises

Array Array Array Array Array Array Arra

  • 1.
  • 2.
    2 5/14/2024 Question  Questions: How dowe read in 100 scores and decide the every grade of the score according to the average score?  If a score is 10 or above greater than the average, it is classified as grade 'A';  If a score is 10 or above less than the average, it is classified as grade 'C';  Others are classified as grade 'B'.
  • 3.
    3 5/14/2024 Question main() { float s1,s2, …, s100, ave; char grade1, grade2, ..., grade100; scanf ( "%f", &s1 ); scanf ( "%f", &s2 ); …… ave = (s1 + s2 + … + s100) /100; if ( s1 >= ave + 10 ) grade1 = 'A'; else if ( s1 < ave – 10 ) grade1 = 'C'; else grade1 = 'B'; if ( s2 >= ave + 10 ) grade2 = 'A'; …… }  This program uses 100 variables s1, s2, ..., s100 to deal with similar data and do similar operations.  We can use a structure to organize those similar variables – Array. Array is a derived data type. An array is a fixed-size sequenced collection of elements of the same data type.
  • 4.
    4 5/14/2024 Question main() { float s[100],ave = 0; int i; char grade[100]; for ( i = 0; i < 100; i ++ ) { scanf ( "%f", &s[i] ); ave = ave + s[i]; } ave = ave / 100; for ( i = 0; i < 100; i++ ) if ( s[i] >= ave + 10 ) grade[i] = 'A'; else if ( s[i] < ave – 10 ) grade[i] = 'C'; else grade[i] = 'B'; }
  • 5.
    5 5/14/2024 Chapter 6  Inthis chapter, we will learn:  One-dimensional arrays  Two-dimensional arrays
  • 6.
    6 5/14/2024 valid identifier the typeof the elements One-dimensional Arrays - Declaration  The general form of array declaration: type array_name [size]; e.g. int a[6]; An integer constant, the maximum number of elements can be stored inside the array a a[5] 5 a[4] 4 a[3] 3 a[2] 2 a[1] 1 a[0] 0 Array name is the start address of this array in memory, and its value is an address constant. The spaces of elements are continuous. The first element is a[0]. The subscripts begin with 0. The array "a" contains 6 elements. The last element is a[5]. The subscripts of array a[n] end with n-1. Don't use a[6] !!!
  • 7.
    7 5/14/2024 main() { int i,s = 10; float f[s]; f[0] = 0; for ( i = 1; i < s; i ++ ) f[i] = f[i - 1] + 1; …… } main() { int i, s = 10; float f[10]; f[0] = 0; for ( i = 1; i < s; i ++ ) f[i] = f[i - 1] + 1; …… } #define SIZE 10 main() { int i; float f[SIZE]; f[0] = 0; for ( i = 1; i < SIZE; i ++ ) f[i] = f[i - 1] + 1; …… } One-dimensional Arrays - Declaration  When the elements of an array are used, the subscripts of an array can be integer constants or variables or expressions.  However, in the declaration of an array, the size can be only an integer constant expression. Error … : Constant expression required in function ...
  • 8.
    8 5/14/2024  Like primaryvariables, an array must be declared before it is used.  We can only use the elements one by one, and can't use the whole array. (Notice: The array name is the start address of this array, but it can't represent the overall elements.) e.g. int a[6]; …… printf ( "%d", a ); One-dimensional Arrays - Usage of Elements  e.g. int a[6], i; …… for ( i = 0; i <= 5; i++ ) printf( "%d", a[i] );
  • 9.
    9 5/14/2024 One-dimensional Arrays -Usage of Elements #define SIZE 10 main() { int i; float f[SIZE]; f[0] = 0; for ( i = 1; i < SIZE; i ++ ) f[i] = f[i - 1] + 0.1; for ( i = 0; i < SIZE; i = i + 2 ) printf ( "%4.1f", f[i] ); } 0.0 0.2 0.4 0.6 0.8 The declaration of array The usage of array elements
  • 10.
    10 5/14/2024 One-dimensional Arrays -Usage of Elements e.g. int a[6]; a[6] = 10;  a a[5] 5 a[4] 4 a[3] 3 a[2] 2 a[1] 1 a[0] 0 The elements are from a[0] to a[5]. a[6] is invalid.  The array name "a" is the start address of this array, which is the address of the first element a[0].  a[i] is the (i+1)th element, the subscript i represents the offset from the start address of the array.  When a element is used, the compiler calculate its address according to the start address and the subscript, and then take out the data from the address.
  • 11.
    11 5/14/2024 One-dimensional Arrays -Usage of Elements main() { int a[2]; a[0] = 1; a[1] = 2; printf ( "%dn", a[0] ); printf ( "%dn", a[1] ); printf ( "%d %x", a, a ); } 1 2 - 42 ffd6
  • 12.
    12 5/14/2024 One-dimensional Arrays -Usage of Elements main() { int a[2]; scanf ( "%d", &a[0] ); a[1] = a[0] * 2; printf ( "%dn", a[0] ); printf ( "%dn", a[1] ); } 5 5 10 scanf ( "%d", a ); scanf ( "%d", a + 1 );
  • 13.
    13 5/14/2024 One-dimensional Arrays -Initialization 1. Like primary variables, we can initialize the elements of an array when the array is declared.  The general form of initialization of array is: type array-name[size] = { list of values } The values in the list are separated by commas. e.g. int a[5] = { 1, 2, 3, 4, 5 }; Equivalent to: int a[5]; a[0] = 1; a[1] = 2; a[2] = 3; a[3] = 4; a[4] = 5;
  • 14.
    14 5/14/2024 One-dimensional Arrays -Initialization 2. Initialization may be partial.  The number of initializers may be less than the declared size. In such cases, the remaining elements are initialized to zero. e.g. int a[5] = { 1, 2, 3 }; Equivalent to: int a[5]; a[0] = 1; a[1] = 2; a[2] = 3; a[3] = a[4] = 0;
  • 15.
    15 5/14/2024 One-dimensional Arrays -Initialization 2. If the number of initializers is more than the declared size, the complier will produce an error. e.g. int a[3] = { 1, 2, 3, 4, 5 }; Error … : Too many initializers in function ...
  • 16.
    16 5/14/2024 One-dimensional Arrays -Initialization 3. The size may be omitted.  In such cases, the compiler allocates enough space for all initialized elements. If the number of initializers is n, the declared size of the array is n. e.g. int a[ ] = { 1, 2, 3, 4, 5 }; Equivalent to: int a[5] = { 1, 2, 3, 4, 5 };
  • 17.
    17 5/14/2024 One-dimensional Arrays -Initialization 4. If the elements are not initialized before they are used in an expression, the result of this expression is unexpected.  Before the elements are used, they must be initialized, whether in compile time or in run time. e.g. int a[3]; printf ( "%d, %d, %d", a[0], a[1], a[2] ); 0, 64, 3117
  • 18.
    18 5/14/2024 One-dimensional Arrays -Program 1  Read in 10 integers, and find out the maximum and the minimum and the average.  Step 1: Read 10 numbers into array "a".  Step 2: max = min = sum = a[0];  Step3: for ( i = 1; i < 10; i++ ) { if ( a[i] > max ) max = a[i]; if ( a[i] < min ) min = a[i]; sum = sum + a[i]; }  Step 4: ave = sum / 10;  Step 5: Output the values of max, min and ave. N-S Flow Chart for i = 0 to 9 Read in a[i] max = min = sum = a[0]; ave = (float) sum / 10 for i = 1 to 9 max = a[i] Y N max<a[i] min = a[i] Y N min>a[i] sum = sum + a[i] Output: max, min, ave
  • 19.
    19 5/14/2024 #define N 10 main() {int a[N], max, min, sum, i; float ave; printf ( "Enter %d integers:n", N ); for ( i = 0; i < N; i++ ) scanf ( "%d", &a[i] ); max = min = sum = a[0]; for ( i = 1; i < N; i++) { if ( a[i] > max ) max = a[i]; if ( a[i] < min ) min = a[i]; sum = sum + a[i]; } ave = (float) sum / N; printf ("max = %d, min = %d, ave = %.1f", max, min, ave ); } #define N 10 main() { int a[N], max, min, sum, i; float ave; printf ( "Enter %d integers:n", N ); for ( i = 0; i < N; i++ ) scanf ( "%d", a + i ); max = min = sum = a[0]; for ( i = 1; i < N; i++) { if ( a[i] > max ) max = a[i]; if ( a[i] < min ) min = a[i]; sum = sum + a[i]; } ave = (float) sum / N; printf ("max = %d, min = %d, ave = %.1f", max, min, ave ); } One-dimensional Arrays - Program 1 Enter 10 integers: 1 2 3 4 5 6 7 8 9 10 max = 10, min = 1, ave = 5.5
  • 20.
    20 5/14/2024 One-dimensional Arrays -Program 2  Read in 10 scores, and decide the every grade of the score according to the average score.  If a score is 10 or above greater than the average, it is classified as grade 'A';  If a score is 10 or above less than the average, it is classified as grade 'C';  Others are classified as grade 'B'.  Step 1: Read in 10 scores (float type).  Step 2: Calculate the average score.  Step 3: Compare every score with the average to decide the every grade, and then output it.
  • 21.
    21 5/14/2024 One-dimensional Arrays -Program 2 #define N 10 main() { float score[N], ave, sum = 0; int i; char grade; for ( i = 0; i < N; i++ ) { printf ( "Please input the score of student %d:", i+1 ); scanf ( "%f", &score[i] ); sum = sum + score[i]; } printf("The average is : %.1fn", ave = sum / N); for ( i = 0; i < N; i++ ) { if ( score[i] - ave >= 10 ) grade = 'A'; else if ( score[i] - ave <= -10 ) grade = 'C'; else grade = 'B'; printf( "The grade of student %d is: %cn", i+1, grade); } }
  • 22.
    22 5/14/2024 One-dimensional Arrays -Program 3  Output the former 40 numbers of the Fibonacci sequence.  Fibonacci sequence: 1, 1, 2, 3, 5, 8, 13, 21, 34, …… F1 = 1 (n = 1) F2 = 1 (n = 2) Fn = Fn-1 + Fn-2 (n≥3) f[39] 39 f[5] 5 f[4] 4 f[3] 3 f[2] 2 f[1] 1 1 f[0] 1 0 2 3 5 8 ……
  • 23.
    23 5/14/2024 One-dimensional Arrays -Program 3 main() { int i; long f[40] = { 1, 1 }; for ( i = 2; i < 40; i++ ) f[i] = f[i-2] + f[i-1]; for ( i = 0; i < 40; i++ ) { if ( i%4 == 0) printf ( "n" ); printf ( "%12ld", f[i] ); } } main() { long f1 = 1, f2 = 1; int i; for ( i = 1; i <= 20; i++ ) { printf ( "%12ld %12ld", f1, f2 ); if ( i%2 == 0) printf ( "n" ); f1 = f1 + f2; f2 = f2 + f1; } }
  • 24.
    24 5/14/2024 One-dimensional Arrays -Program 4  Read in 20 characters, find out the character specified by user, and output its position.  Step 1: Read 20 characters into array "c".  Step 2: Read in the character which the user wants to search, and assign to variable ch.  Step3: Compare ch with the array "c" from c[0] to [9] to test whether the array contains this character or not.
  • 25.
    25 5/14/2024 main() { char c[20],ch; int i; printf ( "Please input the 20 characters:n" ); for ( i = 0; i < 20; i++ ) scanf ( "%c", &c[i] ); printf("What do you want to search?"); scanf("%c", &ch); for ( i=0; i<20; i++) { if ( c[i] == ch ) { printf("'%c' at the position %d.", ch, i+1 ); break; } else if ( i == 19 ) printf ( "No found!" ); } } One-dimensional Arrays - Program 4 main() { char c[20], ch; int i; printf ( "Please input the 20 characters:n" ); for ( i = 0; i < 20; i++ ) scanf ( "%c", &c[i] ); scanf ( "%c", &ch ); printf("What do you want to search?"); scanf("%c", &ch); for ( i=0; i<20; i++) { if ( c[i] == ch ) { printf("'%c' at the position %d.", ch, i+1 ); break; } else if ( i == 19 ) printf ( "No found!" ); } } Please input the 20 characters: abcdefghijklmnopqrst What do you want to search?No found! Please input the 20 characters: abcdefghijklmnopqrst What do you want to search? 'd' at the positon 4. d
  • 26.
    26 5/14/2024 One-dimensional Arrays -Program 5  Bubble sort: Arrange elements in the list according to their values, in ascending order.  Method: Compare the two adjacent numbers, and put the larger number on the latter position.  Step 1: Read n numbers into the array a.
  • 27.
    27 5/14/2024 One-dimensional Arrays -Program 5  Bubble sort: Arrange elements in the list according to their values, in ascending order.  Method: Compare the two adjacent numbers, and put the larger number on the latter position.  Step 2: Compare the 1st and the 2nd number, and put the larger on the latter position. Then compare the 2nd and the 3rd number, and put the larger on the latter position ... In the same way, until we have compared and put the (n-1)th and the nth number – we put the largest number on the last position.
  • 28.
    28 5/14/2024 One-dimensional Arrays -Program 5  Bubble sort: Arrange elements in the list according to their values, in ascending order.  Method: Compare the two adjacent numbers, and put the larger number on the latter position.  Step 3: In the same way, we do with the former n-1 numbers, and the second largest number is put on the last second position.  Step 4: Repeat this process until all the numbers are sorted.
  • 29.
    29 5/14/2024 49 38 6597 76 13 27 30 38 49 65 76 13 27 30 97 38 49 38 49 65 49 49 65 97 65 65 97 76 97 76 97 13 97 13 97 27 97 27 97 30 97 30 97 49 38 38 49 65 49 49 65 76 65 65 76 13 76 13 76 27 76 27 76 30 76 30 76 38 49 65 13 27 30 76 97 49 38 38 49 65 49 49 65 13 65 13 65 27 65 27 65 30 65 30 65 38 49 13 27 30 65 76 97 49 38 38 49 13 49 13 49 27 49 27 49 30 49 30 49 38 13 27 30 49 65 76 97 13 38 13 38 27 38 27 38 30 38 30 38 13 27 30 38 49 65 76 97 27 13 13 27 30 27 27 30 13 27 30 38 49 65 76 97 27 13 13 27 13 27 30 38 49 65 76 97 Comparison 7 times 6 times 5 times 4 times 3 times 2 times 1 time One-dimensional Arrays - Program 5 a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] 1st sorting 2nd sorting 3rd sorting 4th sorting 5th sorting 6th sorting 7th sorting result • To n numbers, altogether times of sorting. • In ith sorting, altogether times of comparison. n-1 "i" represents times of sorting: 1 <= i <= n-1 n-i "j" represents the subscript of the compared element : 1 <= j <= n-i Every time, a[j-1] and a[j] are compared.
  • 30.
    30 5/14/2024 One-dimensional Arrays -Program 5 #define N 10 main ( ) { int a[N], i, j, t; printf ( "Input %d numbers:n", N ); for ( i = 0; i <= N-1; i++) scanf ( "%d", &a[i] ); for ( i = 1; i <= N-1; i++ ) { for ( j = 1; j <= N - i; j++ ) { if ( a[j-1] > a[j] ) { t = a[j-1]; a[j-1] = a[j]; a[j] = t; } } } for ( i = 0; i < N; i++ ) printf ( "%d ",a[i] ); }
  • 31.
    31 5/14/2024 One-dimensional Arrays -Program 6  Selection sort: Arrange elements in the list according to their values, in ascending order.  Method: First select the smallest number and put it on the first position. Then select the second smallest number and put it on the second position... In the same way, place all the numbers in ascending order.
  • 32.
    32 5/14/2024 49 38 6597 76 13 27 30 a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] Comparison 7 times 6 times 5 times 4 times 3 times 2 times 1 time result One-dimensional Arrays - Program 6 13 38 65 97 76 49 27 30 13 38 65 97 76 49 27 30 13 27 65 97 76 49 38 30 13 27 65 97 76 49 38 30 13 27 30 97 76 49 38 65 13 27 30 97 76 49 38 65 13 27 30 38 76 49 97 65 13 27 30 38 76 49 97 65 13 27 30 38 49 76 97 65 13 27 30 38 49 65 97 76 13 27 30 38 49 76 97 65 13 27 30 38 49 65 97 76 13 27 30 38 49 65 76 97 13 27 30 38 49 65 76 97 1th sorting 2nd sorting 3rd sorting 4th sorting 5th sorting 6th sorting 7th sorting • To n numbers, altogether times of sorting. • In ith sorting, altogether times of comparison. n-1 "i" represents times of sorting: 1 <= i <= n-1 n-i "j" represents the subscript of the compared element : i <= j <= n-1 Every sorting, the smallest element in the compared elements is exchange with the element a[i-1].
  • 33.
    33 5/14/2024 One-dimensional Arrays -Program 6 #define N 10 main() { int a[N], i, j, t, k; printf ( "Input %d numbers:n", N ); for ( i = 0; i < N; i++ ) scanf ( "%d", &a[i] ); for( i = 1; i < N; i++ ) { k = i - 1; for ( j = i; j <= N - 1; j++ ) if ( a[k] > a[j] ) k = j; if(k!=i-1) { t = a[k]; a[k] = a[i-1]; a[i-1] = t; } } for ( i = 0; i < N; i++ ) printf ( "%d ", a[i] ); }
  • 34.
    34 5/14/2024 One-dimensional Arrays -Program 7  Read 10 integers into an array, and then rearrange the elements of this array in the inverted order.  Step 1: Read 10 integers into the array a.  Step 2: Exchange a[0] and a[9], a[1] and a[8].... a[4] and a[5].  Step 3: Output this rearranged array. 90 80 70 60 50 40 30 20 10 0 0 80 70 60 50 40 30 20 10 90 0 10 70 60 50 40 30 20 80 90 0 10 20 60 50 40 30 70 80 90 0 10 20 30 50 40 60 70 80 90 0 10 20 30 40 50 60 70 80 90
  • 35.
    35 5/14/2024 One-dimensional Arrays -Program 7 main() { int a[10], i, j, t; printf ( "Please input the 10 integers:" ); for ( i = 0; i < 10; i++ ) scanf ( "%d", &a[i] ); printf ( "The old sequence is: " ); for ( i = 0; i < 10; i++ ) printf ( "%d ", a[i] ); printf("n"); for ( i = 0, j = 9; i < j; i++, j-- ) { t = a[i]; a[i] = a[j]; a[j] = t; } printf("The new sequence is: "); for ( i = 0; i < 10; i++ ) printf ( "%d ", a[i] ); }  Consider: How to rearrange n numbers in the inverted order? for ( i = 0; i < 5; i++ ) { t = a[i]; a[i] = a[9-i]; a[9-i] = t; }
  • 36.
    36 5/14/2024 One-dimensional Arrays -Program 8  10 candidates participate in an election campaign, and 100 persons vote for them. Calculate the ballot of each candidate, and output the result with the histogram expressed by "*".  Step 1: Declare and initialize the array: int count[11] = {0};  Step 2: Read in the vote. If one vote is for candidate n, add 1 to count[n].  Step 3: Output the result and the corresponding “*”.
  • 37.
    37 5/14/2024 One-dimensional Arrays -Program 8 main() { int count[11] = {0}, i, j, n; printf ( "Please input the 100 votes:" ); for ( i = 0; i < 100; i++ ) { scanf ( "%d", &n ); count[n]++; } printf ( "Grade:tCounttGraphn" ); for ( i = 1; i <= 10; i++ ) { printf ( "Candidate%3d: %d ballotst", i, count[i] ); for ( j = 0; j < count[i]; j++ ) printf ( "*" ); printf ( "n" ); } } Candidates : Count Graph Candidate 1: 5 ballots ***** Candidate 2: 6 ballots ****** Candidate 3: 10 ballots ********** Candidate 4: 12 ballots ************ Candidate 5: 2 ballots ** Candidate 6: 7 ballots ******* Candidate 7: 18 ballots ****************** Candidate 8: 28 ballots **************************** Candidate 9: 8 ballots ******** Candidate 10: 4 ballots ****
  • 38.
    38 5/14/2024 Question  Questions: In maths,we often use the matrix, such as Am×n. How do we express it in our program?                  25 19 2 7 5 13 0 6 3 5 3 1 3 4 A               43 42 41 33 32 31 23 22 21 13 12 11 3 4 a a a a a a a a a a a a A Two-dimensional array!
  • 39.
    39 5/14/2024  The generalform of array declaration: type array_name [row_size] [column_size]; e.g. int a[4][3];  The storage of the elements :  The memory is one-dimensional.  In memory: After all the elements of one line are stored, another line is stored. int a[3][2] 5 4 3 2 1 0           ] 1 ][ 2 [ ] 0 ][ 2 [ ] 1 ][ 1 [ ] 0 ][ 1 [ ] 1 ][ 0 [ ] 0 ][ 0 [ a a a a a a Two-dimensional Arrays a[2][1] a[2][0] a[1][1] a[1][0] a[0][1] a[0][0] int c[2][3][4] 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 c[1][2][3] c[1][2][2] c[1][2][1] c[1][2][0] c[1][1][3] c[1][1][2] c[1][1][1] c[1][1][0] c[1][0][3] c[1][0][2] c[1][0][1] c[1][0][0] c[0][2][3] c[0][2][2] c[0][2][1] c[0][2][0] c[0][1][3] c[0][1][2] c[0][1][1] c[0][1][0] c[0][0][3] c[0][0][2] c[0][0][1] c[0][0][0]
  • 40.
    40 5/14/2024 Two-dimensional Arrays int a[3][4]; Eachelement a[i] is a one-dimensional array containing 4 elements. 0 a[0][0] 1 a[0][1] 2 a[0][2] 3 a[0][3] 4 a[1][0] 5 a[1][1] 6 a[1][2] 7 a[1][3] 8 a[2][0] 9 a[2][1] 10 a[2][2] 11 a[2][3] a[0] a[1] a[2] a[0][0] a[0][1] a[0][2] a[0][3] a[1][0] a[1][1] a[1][2] a[1][3] a[2][0] a[2][1] a[2][2] a[2][3] a[0][0] a[0][1] a[0][2] a[0][3] a[1][0] a[1][1] a[1][2] a[1][3] a[2][0] a[2][1] a[2][2] a[2][3] The two-dimensional array a can be regarded as a one-dimensional array containing 3 elements. a[2] a[1] a[0]
  • 41.
    41 5/14/2024 Two-dimensional Arrays -Initialization  A two-dimensional array can be initialized row by row. int a[2][3] = { {1,2,0}, {4,0,0} }; Complete initialization a[0][0] 1 a[0][1] 2 a[0][2] 0 a[1][0] 4 a[1][1] 0 a[1][2] 0 int a[2][3] = { {1,2}, {4} }; Partial initialization
  • 42.
    42 5/14/2024 Two-dimensional Arrays -Initialization  A two-dimensional array can be initialized row by row. a[0][0] 1 a[0][1] 2 a[0][2] 0 a[1][0] 4 a[1][1] 0 a[1][2] 0 int a[ ][3] = { {1,2}, {4} }; The size of the first dimension can be omitted. Notice: The size of the second dimension can't be omitted! e.g. int a[2][ ] = {{1,2}, {4}} is wrong!
  • 43.
    43 5/14/2024 Two-dimensional Arrays -Initialization  A two-dimensional array can be initialized with a list of initial values. a[0][0] 1 a[0][1] 2 a[0][2] 0 a[1][0] 4 a[1][1] 0 a[1][2] 0 int a[2][3] = { 1, 2, 0, 4, 0, 0 }; Complete initialization int a[2][3] = { 1, 2, 0, 4 }; Partial initialization
  • 44.
    44 5/14/2024 Two-dimensional Arrays -Initialization  A two-dimensional array can be initialized with a list of initial values. a[0][0] 1 a[0][1] 2 a[0][2] 0 a[1][0] 4 a[1][1] 0 a[1][2] 0 int a[ ][3] = { 1, 2, 0, 4}; The size of the first dimension can be omitted. Notice: The size of the second dimension can't be omitted! e.g. int a[2][ ] = { 1, 2, 0, 4 }; is wrong!
  • 45.
    45 5/14/2024  Calculate eachrow sum of the matrix a[3][4].  Step 1: Declare and initialize a two-dimensional array a[3][4].  Step 2: Declare a one-dimensional array s[3] to store each row sum.  Step 3: Calculate the sum of the ith row, and store it to the element s[i].  Step 4: Output the value of the array s. Two-dimensional Arrays - Program 1
  • 46.
    46 5/14/2024 Two-dimensional Arrays -Program 1 main() { int a[3][4] = { {1,2,3,4}, {5,6,7}, {8,9,10} }, s[3]; int i, j, sum=0; for ( i = 0; i < 3; i++, sum = 0) { for ( j = 0; j < 4; j++ ) sum = sum + a[i][j]; s[i] = sum; } for ( i = 0; i < 3; i++ ) printf("The row sum %d is: %dn",i+1,s[i]); } The row sum 1 is: 10 The row sum 2 is: 18 The row sum 3 is: 27 a[0][0] 1 a[0][1] 2 a[0][2] 3 a[0][3] 4 a[1][0] 5 a[1][1] 6 a[1][2] 7 a[1][3] 0 a[2][0] 8 a[2][1] 9 a[2][2] 10 a[2][3] 0
  • 47.
    47 5/14/2024 Find the largestelement from a matrix A3×4. Output this element and its row number and column number.  Step 1: Declare and initialize a two-dimensional array a[3][4].  Step 2: Declare and initialize the variable max=a[0][0], row=0, column=0.  Step 3: Find the largest element and use above 3 variables to store the largest element, the row number and column number.  Step 4: Output the values of max, row and column. Two-dimensional Arrays - Program 2
  • 48.
    48 5/14/2024 Two-dimensional Arrays -Program 2 main() { int a[3][4] = { {1,2,3,4}, {9,8,7,6}, {-10,10,-5,2} }; int max=a[0][0], row=0, colum=0, i, j; for ( i = 0; i < 3; i++ ) for ( j = 0; j < 4; j++ ) if ( a[i][j] > max ) { max = a[i][j]; row = i; colum = j; } printf ( "max=%d, row=%d, column=%d", max, row+1, colum+1 ); } max=10, row=3, column=2 a[0][0] 1 a[0][1] 2 a[0][2] 3 a[0][3] 4 a[1][0] 9 a[1][1] 8 a[1][2] 7 a[1][3] 6 a[2][0] -10 a[2][1] 10 a[2][2] -5 a[2][3] 2
  • 49.
    49 5/14/2024 Two-dimensional Arrays -Program 3 Initialize a matrix with 2 rows and 3 columns and output it. Then transpose this matrix and output it.         6 5 4 3 2 1 3 2 A             6 3 5 2 4 1 2 3 B transpose
  • 50.
    50 5/14/2024 Two-dimensional Arrays -Program 3 Initialize a matrix with 2 rows and 3 columns and output it. Then transpose this matrix and output it.  Step 1: Declare and initialize 2 two-dimensional arrays: a[2][3] and b[3][2].  Step 2: Output the two-dimensional array a.  Step 3: b[j][i] = a[i][j];  Step 4: Output the two-dimensional array b.
  • 51.
    51 5/14/2024 Two-dimensional Arrays -Program 3 main() { int a[2][3] = { 1, 2, 3, 4, 5, 6 }, b[3][2], i, j; for ( i = 0; i < 2; i++ ) { for ( j = 0; j < 3; j++ ) { printf ( "%5d", a[i][j] ); b[j][i] = a[i][j]; } printf ( "n" ); } printf ( "n" ); for ( i = 0; i < 3; i++ ) { for ( j = 0; j < 2; j++ ) printf ( "%5d", b[i][j] ); printf ( "n" ); } } 1 2 3 4 5 6 1 4 2 5 3 6
  • 52.
    52 5/14/2024 Homework  Review QuestionsP212  7.1, 7.3~7.8 (Write down in your exercise book)  Programming Exercises