notes5

Out enter a string whats happening str1 is whats str2

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: ;, str1 ); Character Arrays: Example 14 /* output both strings */ 15 printf( "\nstr1 is: %s", str1 ); 16 printf( "\nstr2 is: %s", str2 ); 17 18 printf( "\nstr1 with spaces between characters is: " ); 19 for( i=0; str1[i] != '\0'; i++ ) { 20 printf( "%c ", str1[i] ); 21 } 22 23 printf( "\n\n" ); 24 25 return 0; /* indicate successful termination */ 26 27 } /* end main */ Character Arrays: Output % a.out Enter a string: What's happening? str1 is: What's str2 is: Hello there str1 with spaces between characters is: W h a t ' s % Multiple-Subscripted Arrays • Multiple subscripted arrays – – Tables with rows and columns (m x n array) Like matrices • • Col 0 Row 0 Row 1 Row 2 Row 3 a[0][0] a[1][0] a[2][0] a[3][0] First subscript = row Second subscript = column C ol 1 a[0][1] a[1][1] a[2][1] a[3][1] C ol 2 a[0][2] a[1][2] a[2][2] a[3][2] C ol 3 a[0][3] a[1][3] a[2][3] a[3][3] C ol 4 a[0][4] a[1][4] a[2][4] a[3][4] Multiple-Subscripted Arrays • Initialization – int b[2][3] = { { 1, 2, 3 }, { 4, 5, 6 } }; 1 4 2 5 3 6 – – Initializers grouped by row in braces If not enough initializers, unspecified elements set to zero int b[2][3] = { { 1 }, { 4, 5 } }; 1 4 0 5 0 0 • Referencing elements – Specify row, then column printf( “%d”, b[0][2] ); Multiple-Subscripted Arrays: Example 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 /* Double-scripted array example */ #include <stdio.h> int main() { int i, j; int temp; /* counters */ /* temporary storage location */ /* The following array lists grades received by 3 students on 4 homework assignments. We want to print a table with these scores, the average score on each assignment, and the average score for each student. We will also print the average of all scores. */ int studentGrades[ 3 ][ 4 ] = { { 77, 68, 86, 73 }, { 96, 87, 89, 78 }, { 70, 90, 86, 81 } }; Multiple-Subscripted Arrays: Example 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 float studentAverage[ 3 ]; /* each stud's average */ float hwAverage[ 4 ]; /* average for each hw */ float totalAverage; /* average of all scores */ /* calculate each student's average */ for( i = 0; i < 3; i++ ) { temp = 0; for( j = 0; j < 4; j++ ) { temp += studentGrades[ i ][ j ]; } /* end inner for */ studentAverage[ i ] = (float)temp / 4; } /* end outer for */ Multiple-Subscripted Arrays: Example 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 /* ca...
View Full Document

This note was uploaded on 03/02/2009 for the course ENGR 3 taught by Professor Ben-yaakov during the Fall '08 term at UCSB.

Ask a homework question - tutors are online