2
5/14/2024
Question
Questions:
How do we read in 100 scores and decide the every
grade of the score according to the averagescore?
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 arrayis a fixed-size sequenced collection
of elementsof 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 identifierthe 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 6elements.
The last element is a[5].
The subscripts of array a[n]
end withn-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
subscriptsof an array
can be integer
constants or variables
or expressions.
However, in the
declaration of an array,
the sizecan be only an
integer constant
expression.
Error … : Constant expression
required in function ...
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
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 addressof this array,
which is the address of the first element a[0].
a[i]is the (i+1)
th
element, the subscriptirepresents the
offsetfrom 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 ( "%d\n", a[0] );
printf ( "%d\n", 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 ( "%d\n", a[0] );
printf ( "%d\n", a[1] );
}
5
5
10
scanf ( "%d", a );
scanf ( "%d", a + 1 );
13
5/14/2024
One-dimensional Arrays -Initialization
1.Like primaryvariables, 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 sizemay 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 1Enter 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 : %.1f\n", ave = sum / N);
for ( i = 0; i < N; i++ )
{if ( score[i] -ave >= 10 ) grade = 'A';
else if ( score[i] -ave <= -10 ) grade = 'C';
elsegrade = 'B';
printf( "The grade of student %d is: %c\n", 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, ……
F
1= 1 (n = 1)
F
2= 1 (n = 2)
F
n= F
n-1 + F
n-2(n≥3)
f[39]39
f[5]5
f[4]4
f[3]3
f[2]2
f[1]11
f[0]10
2
3
5
8
……
23
5/14/2024
One-dimensional Arrays -Program 3
main()
{
int i;
longf[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 1
st
and the 2
nd
number, and
put the larger on the latter position. Then compare
the 2
nd
and the 3
rd
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 n
th
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
4938659776132730
3849657613273097
3849
3849
6549
4965
9765
6597
7697
7697
1397
1397
2797
2797
3097
3097
4938
3849
6549
4965
7665
6576
1376
1376
2776
2776
3076
3076
38496513273076974938384965494965136513652765276530653065
384913273065769749383849134913492749274930493049
3813273049657697
1338
1338
2738
2738
3038
3038
13273038496576972713
1327
3027
2730
13273038496576972713
1327
1327303849657697
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]
1
st
sorting
2
nd
sorting
3
rd
sorting
4
th
sorting
5
th
sorting
6
th
sorting
7
th
sorting
result
•To nnumbers, altogether times of sorting.
•In i
th
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
4938659776132730
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
1338659776492730
13386597764927301327659776493830
13276597764938301327309776493865
13273097764938651327303876499765
13273038764997651327303849769765
1327303849659776
13273038497697651327303849659776
1327303849657697
1327303849657697
1
th
sorting
2
nd
sorting
3
rd
sorting
4
th
sorting
5
th
sorting
6
th
sorting
7
th
sorting
•To nnumbers, altogether times of sorting.
•In i
th
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 invertedorder.
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.
9080706050403020100 0807060504030201090 0107060504030208090 0102060504030708090 0102030504060708090 0102030405060708090
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 numbersin 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 “*”.
38
5/14/2024
Question
Questions:
In maths, we often use the matrix, such as A
m×n.
How do we express it in our program?
25192
7513
063
531
34A
434241
333231
232221
131211
34
aaa
aaa
aaa
aaa
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[
aa
aa
aa
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.
0a[0][0]
1a[0][1]
2a[0][2]
3a[0][3]
4a[1][0]
5a[1][1]
6a[1][2]
7a[1][3]
8a[2][0]
9a[2][1]
10a[2][2]
11a[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 arrayacan
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 seconddimension
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 seconddimension
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 i
th
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: %d\n",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 A
3×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 transposethis matrix and output it.
654
321
32A
63
52
41
23
B
transpose
50
5/14/2024
Two-dimensional Arrays -Program 3
Initialize a matrix with 2 rows and 3 columns and
output it. Then transposethis 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.