DOC PREVIEW
UD CISC 181 - Introduction to Computer Science

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

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 5 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 5 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 5 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

1CISC181 Introduction to Computer ScienceDr McCoy1Dr. McCoyLecture 12October 8, 200924.9 Multiple-Subscripted Arrays• Multiple subscripts – a[ i ][ j ]– Tables with rows and columns– Specify row, then column– “Array of arrays”0if4l 2003 Prentice Hall, Inc. All rights reserved.•a[0]is an array of 4 elements• a[0][0] is the first element of that arrayRow 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 subscript34.9 Multiple-Subscripted Arrays• To initialize– Default of 0– Initializers grouped by row in bracesint b[ 2 ][ 2 ] = { { 1, 2 }, { 3, 4 } };1 23 4Row 0 Row 1 2003 Prentice Hall, Inc. All rights reserved.int b[ 2 ][ 2 ] = { { 1 }, { 3, 4 } }; 1 03 444.9 Multiple-Subscripted Arrays• Referenced like normalcout << b[ 0 ][ 1 ];– Outputs 0– Cannot reference using commascout << b[ 0, 1 ];•Syntax error1 03 4 2003 Prentice Hall, Inc. All rights reserved.•Syntax error• Function prototypes– Must specify sizes of subscripts• First subscript not necessary, as with single-scripted arrays– void printArray( int [][ 3 ] );NOTE PROBLEM WITH NEXT FUNCTION• Normally when an array is passed as an argument to a function, you also pass the number of elements in that array.•When an array with multiple subscripts is•When an array with multiple subscripts is passed to a function – it is passed as an array (giving no “number” of elements) of arrays (whose size it given).• But, in the following function the max index is left off (the function only works for one size array.5Outline6fig04_22.cpp(1 of 2)1 // Fig. 4.22: fig04_22.cpp2 // Initializing multidimensional arrays.3 #include <iostream>4 5 using std::cout;6 using std::endl;7 8 void printArray( int [][ 3 ] );9 10 int main()11 {12 int array1[ 2 ][ 3 ] = { { 1, 2, 3 }, { 4, 5, 6 } };13 int array2[ 2 ][ 3 ] = { 1, 2, 3, 4, 5 }; 14 int array3[ 2 ][ 3 ] = { { 1, 2 }, { 4 } }; 15 Note the various initialization styles. The elements in array2 are assigned to the first row and then the second.Note the format of the prototype. 2003 Prentice Hall, Inc.All rights reserved.16 cout << "Values in array1 by row are:" << endl;17 printArray( array1 );18 19 cout << "Values in array2 by row are:" << endl;20 printArray( array2 );21 22 cout << "Values in array3 by row are:" << endl;23 printArray( array3 );24 25 return 0; // indicates successful termination26 27 } // end main2Outline7fig04_22.cpp(2 of 2)fig04_22.cppoutput (1 of 1)28 29 // function to output array with two rows and three columns 30 void printArray( int a[][ 3 ] ) 31 { 32 for ( int i = 0; i < 2; i++ ) { // for each row 33 34 for ( int j = 0; j < 3; j++ ) // output column values35 cout << a[ i ][ j ] << ' '; 36 37 cout << endl; // start new line of output 38 39 } // end outer for structure 40 41 } // end function printArray For loops are often used to iterate through arrays. Nested loops are helpful with multiple-subscripted arrays. 2003 Prentice Hall, Inc.All rights reserved.Values in array1 by row are:1 2 34 5 6Values in array2 by row are:1 2 34 5 0Values in array3 by row are:1 2 04 0 084.9 Multiple-Subscripted Arrays• Next: program showing initialization– After, program to keep track of students grades– Multiple-subscripted array (table)– Rows are students– Columns are gradesQuiz1Quiz2 2003 Prentice Hall, Inc. All rights reserved.95 8589 80Student0Student1NOTE POOR PROGRAMMING STYLE • Lack of documentation with function prototypes.• Lack of variable names in function prototypesprototypes.• NOTE: Can’t at all tell what function does by looking at the prototype. That is WRONG.9Outline10fig04_23.cpp(1 of 6)1 // Fig. 4.23: fig04_23.cpp2 // Double-subscripted array example.3 #include <iostream>4 5 using std::cout;6 using std::endl;7 using std::fixed;8 using std::left;9 10 #include <iomanip>11 12 using std::setw;13 using std::setprecision;14 15 constintstudents= 3; // number of students 2003 Prentice Hall, Inc.All rights reserved.16 const int exams = 4; // number of exams17 18 // function prototypes19 int minimum( int [][ exams ], int, int );20 int maximum( int [][ exams ], int, int );21 double average( int [], int );22 void printArray( int [][ exams ], int, int );23 Outline11fig04_23.cpp(2 of 6)24 int main()25 {26 // initialize student grades for three students (rows)27 int studentGrades[ students ][ exams ] = 28 { { 77, 68, 86, 73 },29 { 96, 87, 89, 78 },30 { 70, 90, 86, 81 } };31 32 // output array studentGrades33 cout << "The array is:\n";34 printArray( studentGrades, students, exams );35 36 // determine smallest and largest grade values37 cout << "\n\nLowest grade: "38 << minimum( studentGrades, students, exams )  2003 Prentice Hall, Inc.All rights reserved.39 << "\nHighest grade: "40 << maximum( studentGrades, students, exams ) << '\n';41 42 cout << fixed << setprecision( 2 );43 Outline12fig04_23.cpp(3 of 6)44 // calculate average grade for each student45 for ( int person = 0; person < students; person++ )46 cout << "The average grade for student " << person 47 << " is " 48 << average( studentGrades[ person ], exams ) 49 << endl;50 51 return 0; // indicates successful termination52 53 } // end main54 55 // find minimum grade56 int minimum( int grades[][ exams ], int pupils, int tests )57 {58 intlowGrade = 100; // initialize to highest possible gradeDetermines the average for one student. We pass the array/row containing the student’s grades. Note that studentGrades[0] is itself an array. 2003 Prentice Hall, Inc.All rights reserved.59 60 for ( int i = 0; i < pupils; i++ ) 61 62 for ( int j = 0; j < tests; j++ ) 63 64 if ( grades[ i ][ j ] < lowGrade )65 lowGrade = grades[ i ][ j ];66 67 return lowGrade;68 69 } // end function minimum3Outline13fig04_23.cpp(4 of 6)70 71 // find maximum grade72 int maximum( int


View Full Document
Download Introduction to Computer Science
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 Introduction to Computer Science 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 Introduction to Computer Science 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?