DOC PREVIEW
IUPUI CSCI 23000 - Arrays Multidimensional Arrays

This preview shows page 1-2 out of 6 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 6 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 6 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 6 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Slide 1Multiple-Dimensional ArraysMultiple-Subscripted ArraysSlide 4Slide 5Slide 6Dale RobertsDepartment of Computer and Information Science,Department of Computer and Information Science,School of Science, IUPUISchool of Science, IUPUICSCI 230ArraysMultidimensional ArraysDale Roberts, LecturerDale Roberts, [email protected]@cs.iupui.eduDale RobertsMultiple-Dimensional ArraysMultiple-Dimensional ArraysMultiple subscripted arrays Multiple subscripted arrays Arrays require at least two subscripts to identify a Arrays require at least two subscripts to identify a particular elementparticular elementANSI C standard allows at least 12 array subscriptsANSI C standard allows at least 12 array subscripts2D Arrays2D ArraysTables with rows and columns (Tables with rows and columns (mm by by nn array) array)Like matrices: specify row, then column Like matrices: specify row, then column Row 0Row 1Row 2Column 0 Column 1 Column 2 Column 3a[ 0 ][ 0 ]a[ 1 ][ 0 ]a[ 2 ][ 0 ]a[ 0 ][ 1 ]a[ 1 ][ 1 ]a[ 2 ][ 1 ]a[ 0 ][ 2 ]a[ 1 ][ 2 ]a[ 2 ][ 2 ]a[ 0 ][ 3 ]a[ 1 ][ 3 ]a[ 2 ][ 3 ]Row subscriptArray nameColumn subscriptA: scalar A[0]: 1D array1-Dimensional vectorA[0][0]: (2 subscripts)2D array (matrix)2-dimensional vectorA common error is to use math matrix notation a(1,2)Dale RobertsMultiple-Subscripted ArraysMultiple-Subscripted ArraysInitializationInitializationint b[2][2]int b[2][2] == {{{{ 1,1, 22 },}, {{ 3,3, 44 }} };};int c[3][2] = {{int c[3][2] = {{ 1,1, 22 },}, {{ 3,3, 44 },}, {{ 5,5, 66 }};}}; Initializers grouped by row in braces Initializers grouped by row in braces If not enough, unspecified elements set to zeroIf not enough, unspecified elements set to zeroint b[int b[ 22 ][][ 22 ]] == {{ {{ 11 },}, {{ 3,3, 44 }} }; }; Referencing elementsReferencing elementsSpecify Specify rowrow, then , then columncolumnprintf(printf( "%d","%d", b[b[ 00 ][][ 11 ]] );); 1 23 41 03 41 23 45 61 2 3 4 5 6Actual storage in the memory - rows by rows -“row-major”Dale Roberts1 /* Fig. 6.22: fig06_22.c2 Double-subscripted array example */3 #include <stdio.h>4 #define STUDENTS 35 #define EXAMS 467 int minimum( const int [][ EXAMS ], int, int );8 int maximum( const int [][ EXAMS ], int, int );9 double average( const int [], int );10 void printArray( const int [][ EXAMS ], int, int );1112 int main()13 {14 int student;15 const int studentGrades[ STUDENTS ][ EXAMS ] = 16 { { 77, 68, 86, 73 },17 { 96, 87, 89, 78 },18 { 70, 90, 86, 81 } };1920 printf( "The array is:\n" );21 printArray( studentGrades, STUDENTS, EXAMS );22 printf( "\n\nLowest grade: %d\nHighest grade: %d\n",23 minimum( studentGrades, STUDENTS, EXAMS ),24 maximum( studentGrades, STUDENTS, EXAMS ) );2526 for ( student = 0; student <= STUDENTS - 1; student++ )27 printf( "The average grade for student %d is %.2f\n", 28 student, 29 average( studentGrades[ student ], EXAMS ) );3031 return 0;32 }Each row is a particular student, each column is the grades on the exam. Proper naming of the #defined constants is important to convey meaning.Initialize variablesDefine functions to take double scripted arraysInitialize studentgrades[][]Call functions minimum, maximum, and averageconst indicates that the function does not modify the array.Dale Roberts3334 /* Find the minimum grade */35 int minimum( const int grades[][ EXAMS ], 36 int pupils, int tests )37 {38 int i, j, lowGrade = 100;3940 for ( i = 0; i <= pupils - 1; i++ )41 for ( j = 0; j <= tests - 1; j++ )42 if ( grades[ i ][ j ] < lowGrade )43 lowGrade = grades[ i ][ j ];4445 return lowGrade;46 }4748 /* Find the maximum grade */49 int maximum( const int grades[][ EXAMS ], 50 int pupils, int tests )51 {52 int i, j, highGrade = 0;5354 for ( i = 0; i <= pupils - 1; i++ )55 for ( j = 0; j <= tests - 1; j++ )56 if ( grades[ i ][ j ] > highGrade )57 highGrade = grades[ i ][ j ];5859 return highGrade;60 }6162 /* Determine the average grade for a particular exam */63 double average( const int setOfGrades[], int tests )64 {Why is it important to initialize lowGrade to the highest possible grade, and highGrade to the lowest possible grade?Dale Roberts65 int i, total = 0;6667 for ( i = 0; i <= tests - 1; i++ )68 total += setOfGrades[ i ];6970 return ( double ) total / tests;71 }7273 /* Print the array */74 void printArray( const int grades[][ EXAMS ], 75 int pupils, int tests )76 {77 int i, j;7879 printf( " [0] [1] [2] [3]" );8081 for ( i = 0; i <= pupils - 1; i++ ) {82 printf( "\nstudentGrades[%d] ", i );8384 for ( j = 0; j <= tests - 1; j++ )85 printf( "%-5d", grades[ i ][ j ] );86 }87 }Define functionThe array is: [0] [1] [2] [3]studentGrades[0] 77 68 86 73 studentGrades[1] 96 87 89 78 studentGrades[2] 70 90 86 81 Lowest grade: 68Highest grade: 96The average grade for student 0 is 76.00The average grade for student 1 is 87.50The average grade for student 2 is 81.75Program


View Full Document

IUPUI CSCI 23000 - Arrays Multidimensional Arrays

Download Arrays Multidimensional Arrays
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Arrays Multidimensional Arrays and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Arrays Multidimensional Arrays 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?