Lecture3-2D Arrays Online
Lecture3-2D Arrays Online
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
2
#include "stdafx.h"
#define SIZE 10
Outline
void main()
{
int a[SIZE] = { 25,14,56,15,36 }, x, loc, i;
for (i = 0; i < 5; i++)
printf("%3d", a[i]);
printf("\n\nenter element:");
scanf_s("%d", &x);
printf("enter location:");
scanf_s("%d", &loc);
printf("\n\n");
a[loc] = x;
for (i = 0; i <=5; i++)
printf("%3d", a[i]);
printf("\n\n");
}
Program Output
25 14 56 15 36
enter element:35
enter location:2
25 14 35 56 15 36
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
3
Column subscript
Array
name Row subscript
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
4
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
5
int b[ 2 ][ 2 ] = { 1 , 3, 4 }; 1 3
4 0
• Referencing elements
– Specify row, then column
printf( "%d", b[ 0 ][ 1 ] );
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
6
/* EX1: printing array elements*/
#include "stdafx.h" Outline
void printArray(int a[][3]);
int main()
{
/* initialize array1, array2, array3 */
int array1[ 2 ][ 3 ] = { { 1, 2, 3 }, { 4, 5, 6 } };
int array2[ 2 ][ 3 ] = { 1, 2, 3, 4, 5 };
int array3[ 2 ][ 3 ] = { { 1, 2 }, { 4 } };
return 0;
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
7
void printArray(int a[][3])
{
Outline
for (int i = 0; i < 2; i++)
{
fig06_21.c (Part 2
for (int j = 0; j < 3; j++)
of 2)
printf("%3d", a[i][j]);
printf("\n");
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
/* EX2: printing array elements*/
#include "stdafx.h"
#define WIDTH 5
#define HEIGHT 3
void main()
{
int jimmy[HEIGHT][WIDTH];
int n, m;
for (n = 0; n < HEIGHT; n++)
{
for (m = 0; m < WIDTH; m++)
{
jimmy[n][m] = (n + 1)*(m + 1);
printf("%3d", jimmy[n][m]);
}
printf("\n");
}
}
1 2 3 4 5 Program Output
2 4 6 8 10
3 6 9 12 15
// EX3: filling an array with a random number
#include "stdafx.h"
#include "stdlib.h"
#include "time.h"
void main()
{
int a[3][2], r, c;
srand(time(NULL));
for (r = 0; r<3; r++)
{
for (c = 0; c<2; c++)
{
a[r][c] = rand() % 9 + 2;
printf("%3d", a[r][c]);
}
printf("\n");
}
}
8 6 Program Output
9 9
2 3
// EX4: filling an array with a random number and finding sum of each row
#include "stdafx.h"
#include "stdlib.h"
#include "time.h"
void main()
{
int a[2][4], sum[2] = { 0 }, r, c;
srand(time(NULL));
for (r = 0; r<2; r++)
{
for (c = 0; c<4; c++)
{
a[r][c] = rand() % 10 + 1;
printf("%3d", a[r][c]);
sum[r] += a[r][c];
}
printf(" = %d", sum[r]);
printf("\n");
}
}
10 5 6 3 = 24 Program Output
7 5 6 10 = 28
1 /*
EX5: 8
2 Double-subscripted array example */ Outline
3 #include <stdafx.h>
4 #define STUDENTS 3
5 #define EXAMS 4 fig06_22.c (Part 1
6 of 6)
7 /* function prototypes */
8 int minimum(int grades[][ EXAMS ], int pupils, int tests );
9 int maximum(int grades[][ EXAMS ], int pupils, int tests );
10 double average(int setOfGrades[], int tests );
11 void printArray(int grades[][ EXAMS ], int pupils, int tests );
12
13 /* function main begins program execution */
14 int main()
15 {
16 int student; /* counter */
17
18 /* initialize student grades for three students (rows) */
19 int studentGrades[ STUDENTS ][ EXAMS ] =
20 { { 77, 68, 86, 73 },
21 { 96, 87, 89, 78 },
22 { 70, 90, 86, 81 } };
23
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
24 /* output array studentGrades */
10
25 printf( "The array is:\n" );
26 printArray( studentGrades, STUDENTS, EXAMS );
Outline
27
28 /* determine smallest and largest grade values */
32
33 /* calculate average grade for each student */
for ( student = 0; student <= STUDENTS - 1; student++ ) {
34
printf( "The average grade for student %d is %.2f\n",
35
student, average( studentGrades[ student ], EXAMS ) );
36
} /* end for */
37
38
return 0; /* indicates successful termination */
39
40
} /* end main */
41
42
/* Find the minimum grade */
43
int minimum( int grades[][ EXAMS ], int pupils, int tests )
44
{
45
int i; /* counter */
46
int j; /* counter */
47
int lowGrade = 100; /* initialize to highest possible grade */
48
49
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
50 /* loop through rows of grades */
11
51 for ( i = 0; i < pupils; i++ ) {
Outline
52
53 /* loop through columns of grades */
54 for ( j = 0; j < tests; j++ ) { fig06_22.c (Part 3
55 of 6)
56 if ( grades[ i ][ j ] < lowGrade ) {
57 lowGrade = grades[ i ][ j ];
58 } /* end if */
59
60 } /* end inner for */
61
62 } /* end outer for */
63
64 return lowGrade; /* return minimum grade */
65
66 } /* end function minimum */
67
68 /* Find the maximum grade */
69 int maximum(int grades[][ EXAMS ], int pupils, int tests )
70 {
71 int i; /* counter */
72 int j; /* counter */
73 int highGrade = 0; /* initialize to lowest possible grade */
74
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
75 /* loop through rows of grades */
12
76 for ( i = 0; i < pupils; i++ ) {
Outline
77
78 /* loop through columns of grades */
79 for ( j = 0; j < tests; j++ ) { fig06_22.c (Part 4
80 of 6)
81 if ( grades[ i ][ j ] > highGrade ) {
82 highGrade = grades[ i ][ j ];
83 } /* end if */
84
85 } /* end inner for */
86
87 } /* end outer for */
88
89 return highGrade; /* return maximum grade */
90
91 } /* end function maximum */
92
93 /* Determine the average grade for a particular student */
94 double average(int setOfGrades[], int tests )
95 {
96 int i; /* counter */
97 int total = 0; /* sum of test grades */
98
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
99 /* total all grades for one student */
13
100 for ( i = 0; i < tests; i++ ) {
total += setOfGrades[ i ];
Outline
101
102 } /* end for */
103 fig06_22.c (Part 5
104 return ( double ) total / tests; /* average */ of 6)
105
106 } /* end function average */
107
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
123 /* output grades for one student */
14
124 for ( j = 0; j < tests; j++ ) {
Outline
125 printf( "%-5d", grades[ i ][ j ] );
126 } /* end inner for */
127 fig06_22.c (Part 6
128 } /* end outer for */ of 6)
129
130} /* end function printArray */
Lowest grade: 68
Highest grade: 96
The average grade for student 0 is 76.00
The average grade for student 1 is 87.50
The average grade for student 2 is 81.75
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.