Minggu, 13 April 2014

Multiple Subscripted Arrays

6.9 Multiple Subscripted Arrays

Array dalam C bisa saja memiliki subscript lebih dari 1. Array dengan lebih dari 1 subscript disebut dengan Multidimensional array. Array dengan 2 subscript memiliki baris dan kolom yang dapat membentuk suatu tabel. Subcript pertama biasanya dianggap sebagai baris dan subscript kedua dianggap sebagai kolom.



array int a[2] [2] bisa didefinisikan seperti contoh di bawah ini:

int a[ 2 ][ 2 ] = { { 1, 2 }, { 3, 4 } };
kurung kurawal pertama mendefinisikan nilai array mulai dari baris pertama, yaitu index baris ke 0. Kurung kurawal kedua menunjukan pendefinisian nilai array pada index berikutnya, yaitu index baris ke 1. Sehingga hasil yang diinputkan ke array tersebut adalah:
a[0][0] = 1 (index baris ke 0, index kolom ke 0)
a[0][1] = 2 (index baris ke 0, index kolom ke 1)
a[1][0] = 3 (index baris ke 1, index kolom ke 0)
a[1][1] = 4 (index baris ke 1, index kolom ke 1)
Bila nilai yang dituliskan kurang dari jumlah index yang disediakan, maka sisa elemen yang tidak memiliki nilai tersebut akan menjadi 0. Seperti misalnya:
int a[ 2 ][ 2 ] = { { 1 }, { 3, 4 } };
Maka elemen index yang tidak dituliskan tersebut akan bernilai 0.
a[0][0] = 1
a[0][1] = 0
a[1][0] = 3
a[1][1] = 4

berikut adalah contoh program untuk menggambarkan pendefinisian array:

/* Fig. 6.21: fig06_21.c
Initializing multidimensional arrays */
#include <stdio.h>

void printArray( const int a[][ 3 ] ); /* function prototype */

/* function main begins program execution */
int main( void )
{
/* 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 } };

printf( "Values in array1 by row are:\n" );
printArray( array1 );

printf( "Values in array2 by row are:\n" );
printArray( array2 );

printf( "Values in array3 by row are:\n" );
printArray( array3 );
getch();
return 0; /* indicates successful termination */
} /* end main */

/* function to output array with two rows and three columns */
void printArray( const int a[][ 3 ] )
{
int i; /* row counter */
int j; /* column counter */

/* loop through rows */
for ( i = 0; i <= 1; i++ ) {
/* output column values */
for ( j = 0; j <= 2; j++ ) {
printf( "%d ", a[ i ][ j ] );
} /* end inner for */
printf( "\n" ); /* start new line of output */
} /* end outer for */

} /* end function printArray */

Hasil output:



Array 3 baris 4 kolom juga bisa diterapkan seperti misalnya dalam memanipulasi data nilai siswa. 
Berikut contoh programnya:

/* Fig. 6.22: fig06_22.c
Double-subscripted array example */
#include <stdio.h>
#define STUDENTS 3
#define EXAMS 4

/* function prototypes */
int minimum( const int grades[][ EXAMS ], int pupils, int tests );
int maximum( const int grades[][ EXAMS ], int pupils, int tests );
double average( const int setOfGrades[], int tests );
void printArray( const int grades[][ EXAMS ], int pupils, int tests );

/* function main begins program execution */
int main( void )
{
int student; /* student counter */

/* initialize student grades for three students (rows) */
const int studentGrades[ STUDENTS ][ EXAMS ] =
{ { 77, 68, 86, 73 },
{ 96, 87, 89, 78 },
{ 70, 90, 86, 81 } };

/* output array studentGrades */
printf( "The array is:\n" );
printArray( studentGrades, STUDENTS, EXAMS );

/* determine smallest and largest grade values */
printf( "\n\nLowest grade: %d\nHighest grade: %d\n",
minimum( studentGrades, STUDENTS, EXAMS ),
maximum( studentGrades, STUDENTS, EXAMS ) );

/* calculate average grade for each student */
for ( student = 0; student < STUDENTS; student++ ) {
printf( "The average grade for student %d is %.2f\n",

student, average( studentGrades[ student ], EXAMS ) );

} /* end for */

getch();
return 0; /* indicates successful termination */
} /* end main */

/* Find the minimum grade */
int minimum( const int grades[][ EXAMS ], int pupils, int tests )
{
int i; /* student counter */
int j; /* exam counter */
int lowGrade = 100; /* initialize to highest possible grade */

/* loop through rows of grades */
for ( i = 0; i < pupils; i++ ) {

/* loop through columns of grades */
for ( j = 0; j < tests; j++ ) {

if ( grades[ i ][ j ] < lowGrade ) {
lowGrade = grades[ i ][ j ];
} /* end if */
} /* end inner for */
} /* end outer for */

return lowGrade; /* return minimum grade */
} /* end function minimum */

/* Find the maximum grade */
int maximum( const int grades[][ EXAMS ], int pupils, int tests )
{
int i; /* student counter */
int j; /* exam counter */
int highGrade = 0; /* initialize to lowest possible grade */

/* loop through rows of grades */
for ( i = 0; i < pupils; i++ ) {

/* loop through columns of grades */
for ( j = 0; j < tests; j++ ) {

if ( grades[ i ][ j ] > highGrade ) {
highGrade = grades[ i ][ j ];
} /* end if */
} /* end inner for */
} /* end outer for */

return highGrade; /* return maximum grade */
} /* end function maximum */

/* Determine the average grade for a particular student */
double average( const int setOfGrades[], int tests )
{
int i; /* exam counter */
int total = 0; /* sum of test grades */
/* total all grades for one student */
for ( i = 0; i < tests; i++ ) {
total += setOfGrades[ i ];
} /* end for */
return ( double ) total / tests; /* average */
} /* end function average */

/* Print the array */
void printArray( const int grades[][ EXAMS ], int pupils, int tests )
{
int i; /* student counter */
int j; /* exam counter */

/* output column heads */
printf( "\t\t[0]  [1]  [2]  [3]" );

/* output grades in tabular format */
for ( i = 0; i < pupils; i++ ) {

/* output label for row */
printf( "\nstudentGrades[%d] ", i );

/* output grades for one student */
for ( j = 0; j < tests; j++ ) {
printf( "%-5d", grades[ i ][ j ] );
} /* end inner for */
} /* end outer for */
} /* end function printArray */

Hasil output:


Tidak ada komentar:

Posting Komentar