Você está na página 1de 7

Arrays

Background include<stdio.h> main(){ int q1, q2, sum=0, avg=0; printf(Enter value for quiz1 -> ); scanf(%d,&q1); printf(Enter value for quiz2 -> ); scanf(%d,&q2); sum = q1 + q2; avg = sum/2; printf(Average is -> %d. , avg); } include<stdio.h> main(){ int q1, q2, q3, q4, q20, sum=0, avg=0; printf(Enter value for quiz1 -> ); scanf(%d,&q1); . printf(Enter value for quiz20 -> ); scanf(%d,&q20); sum = q1 + q2 + q3 + q20; avg = sum/20; printf(Average is -> %d.,avg); } The declaration will be int s1q1, s1q2, s1q3, s1q4, s1q5, s1q20; int s2q1, s2q2, s2q3, s2q4, s2q5, s2q20; int s42q1, s42q2, s42q3, s42q4, s42q20; int sum1=0, sum2=0, sum42=0; int avg1=0, avg1=0, . Avg42=0; printf(Enter value for student 1 quiz1 -> ); scanf(%d, &s1q1); sum1 = s1q1 + s1q2 + s1q20; avg1 = sum1 / 20; printf(Enter value for student 42 quiz1 ->); scanf(%d, &s42q1); sum42 = s42q1 + s42q2 + s42q20; avg42 = sum42 / 20; .

Arrays

Arrays

Solution A int q1,sum=0, avg=0, i; for (i = 1; i <= 20; i ++){ printf(Enter value for quiz %d -> , i); scanf(%d, &q1); sum = sum + q1; } //for avg = sum/20; printf(Average is -> %d. ,avg); Solution A int q1,sum=0; avg=0; int s, q; for ( s = 1; s <= 42; s++){ for ( q = 1; q <= 20; q ++){ printf(Enter value for Student %d, quiz %d -> , s, q); scanf(%d, &q1); sum = sum + q1; } avg = sum/20; printf(Average of the %d student is %d -> , s, avg); } Limitations of Solution A What if, you would like to retrieve the quiz number 1 of student 11? What if, you would like to edit the quiz number 10 of student 22? What if, you would like to know the quiz number 6 of student 31? What if, you would like to recompute the average of student 05? What if, you would like to remember all the value of the quizzes? And etc. of what if... Solution B - Using Arrays An array is a sequence of elements of the same type, which are stored contiguously in memory and which can be accessed by means of an integer subscript. An array is declared by specifying its element type followed by its name followed by the number of elements enclosed in brackets [ ]. The elements are numbered consecutively, starting with 0. The elements of the array are accessed by specifying the array name followed by the element number enclosed in brackets.

ARRAY DECLARATION int quiz[10]; quiz is the name of the array of integers.
2

Arrays

Arrays

It contains 10 elements, which can be accessed using the index values 0 through 9 inclusive. To access the element number use the index or the subscript. quiz[1] accessing the second element quiz[0] accessing the first element INITIALIZING AN ARRAY (One-Dimensional Array) int quiz[10] = {22, 88, 66, 44, 80, 90, 55, 89, 90}; or int quiz[10]; quiz[0] = 22; quiz[1] = 88; quiz[2] = 66; quiz[3] = 44; quiz[4] = 80; quiz[5] = 90; quiz[6] = 55; quiz[7] = 89; quiz[8] = 12; quiz[9] = 90; INITIALIZING AN ARRAY (One-Dimensional Array) char word[5] = { H, e, l, l, o}; or char word[5]; word[0] = H; word[1] = e; word[2] = l; word[3] = l; word[4] = o; Example declaration int num1[4] = {22, 88, 66, 44}; int num2[4 ]; Unlike the fundamental (atomic) types (char, int, float, double, etc.), arrays are composites, consisting of several values. Consequently, many of the operations used on fundamental types do not work as expected with arrays: int num2[ ] = num1; //ILLEGAL initialization num2 = num1; //ILLEGAL initialization! if (num2 == num1) //DOES not work as expected printf(%d, num1); //ILLEGAL extraction! output: [I@f7c3b4c1 num1 = num1 + 2; //ILLEGAL arithmetic for LOOP TO TRAVERSE AN ARRAY int num[4] = {22, 88, 66, 44}; int i; for (i =0;i <4;i++)
3

Arrays

Arrays

printf(%d, num[i]); COPYING AN ARRAY int number[4] = {1,3,5,7}; int number1[4]; int i; for( i=0; i<4; i++) number1[i] = number[i]; Solution B int quiz[20]; int sum=0, avg=0, i; for (i = 0; i < 20; i ++){ printf(Enter value for quiz %d -> , i+1); scanf(%d, &quiz[i]); sum = sum + quiz[i]; } //for avg = sum/20; printf(Average is -> %d. ,avg); The values of array is hard coded int quiz[20] = {90,89,95,12,50,60.90}; int sum=0, avg=0, i; for (i = 0; i < 19; i ++) sum = sum + quiz[i]; avg = sum/20; printf(Average is -> %d. ,avg); Statements that manipulate Array x double x[8]; x[0] x[1] x[2] x[3] x[4] x[5] x[6] x[7] 16.0 12.0 6.0 8.0 2.5 12.0 14.0 -54.5 Statement Explanation printf(%.1f, x[0]); Displays the value of x[0], which is 16.0. x[3] = 25.0; Stores the value 25.0 to x[3]. sum = x[0] + x[1]; Adds the value of x[0] and x[1] then stores the result which is 28.0 at variable sum. sum += x[2]; Adds x[2] to sum. The new sum is 34.0 x[3] += 1.0; Adds 1.0 to x[3]. The new x[3] is 26.0 x[2] = x[0] + x[1]; Adds the value of x[0] and x[1] then stores the result which is 28.0 to x[2]. 16.0 12.0 28.0 26.0 2.5 12.0 14.0 -54.5 Array Subscript double x[8]; x[0] x[1] x[2] x[3]

x[4]

x[5]

x[6]

x[7]
4

Arrays

Arrays

16.0 12.0 6.0 8.0 2.5 12.0 14.0 -54.5 Statement Explanation i=5; printf(%d %.1f, 4, x[4]); Displays 4 and 2.5 (value of x[4]) printf(%d %.1f, i, x[i]); Displays 5 and 12.0 (value of x[5]) printf(%.1f, x[i] + 1); Displays 13.0 (value of x[5] plus 1) printf(%.1f, x[i] + i); Displays 17.0 (value of x[5] plus 5) printf(%.1f, x[i+1]); Displays 14.0 (value of x[6]) printf(%.1f, x[i+i ]); Invalid. Attempt to display x[10] printf(%.1f, x[2*i]); Invalid. Attempt to display x[10] printf(%.1f, x[2*i-3]); Displays -54.5 (value of x[7]) Array Subscript double x[8]; x[0] x[1] x[2] x[3] x[4] x[5] x[6] x[7] 16.0 12.0 6.0 8.0 2.5 12.0 14.0 -54.5 Statement Explanation i=5; printf(%.1f, x[(int)x[4]]); Displays 6.0 (value of x[2]) printf(%.1f, x[i++]); Displays 12.0 (value of x[5]) then assigns 6 to i; printf(%.1f, x[--i] ); Assign 5 ( 6-1) to i and then displays 12.0 (value of x[5]) x[i-1] = x[i]; Assigns 12.0 (value of x[5] to x[4]) x[i] = x [ i+1]; Assigns 14.0 (value of x[6] to x[5]) x[i] - 1 = x [i]; Illegal assignment statement Parallel Arrays Two or more arrays with the same number of elements used for storing related information about a collection of data objects. Example: int id[50]; dobule gpa[50]; If you use these declarations in a problem to assess the range and distribution of grade point averages, you can store the first student I.D. in id[0], and store the same students gpa in gpa[0]. The data stored in id[j] and gpa[j] relate to jth student, the two arrays are called parallel arrays. Illustration id[0] 5505 gpa[0] id[1] 4556 gpa[1] id[2] 5691 gpa[2] id[3] 9099 gpa[3] id[49] 9146 gpa[49] 1.92

2.71 3.09 2.98 3.50

Arrays

Arrays

Practice Declare one array for storing the square of the integers from 1 through 5 and a second array for storing the cubes of the same integers. int square[5], cube[5], j=1, k=0; while(j<=5){ square[k]=j*j; cube[k]=j*j*j; j++; k++; } Multidimensional Arrays An array with two or more dimensions We will use two dimensional arrays to represent tables of data, matrices, and other two dimensional objects. It has two independent subscripts. INITIALIZING AN ARRAY (Two-Dimensional Array) int m[3][6]; m has 18 elements, arranged in 3 rows and 6 columns m 0 1 2 3 4 5 0 1 2 int m[3][6] = {{77,55,11,33,88,90},{33,90,55,89,90,91}, {90,56,4,6,7,8}}; or m[0][0] = 77; m[1][0] = 33; m[2][0] = 90; m[0][1] = 55; m[1][1] = 90; m[2][1] = 56; ... for LOOP TO TRAVERSE A Two-dim ARRAY int m[3][6] = {{77,55,11,33,88,90},{33,90,55,89,90,91}, {90,56,4,6,7,8}}; int j, k; for(j=0; j < 3; j++) for (k=0; k<6; k++) printf(%d, m[j][k]); Solution B int q1[42][20],sum=0; avg=0; int ave[42], sum[42], s, q; for ( s = 0; s < 42; s++){ for ( q = 0; q < 20; q ++){ printf(Enter value for Student %d, quiz %d -> , s+1, q+1); scanf(%d, &q1[s][q]); sum[s] = sum[s] + q1[s][q]; } avg[s] = sum[s]/20; printf(Average of the %d student is %d -> , s+1, avg[s]);
6

Arrays

Arrays

Arrays

Você também pode gostar