Vous êtes sur la page 1sur 29

Arrays

ARRAY

Lets analyze this situation


Imagine creating a program for a single student, input two quizzes and find their sum and average. How would your program look?

ARRAY

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); }

Is this what you had in mind?

ARRAY

Lets change the scenario


If there were 20 quizzes, how would your program look?

ARRAY

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); }
ARRAY

Lets make life a little complex if I had 40 students, where each had 20 quizzes, how would your declaration look like?

ARRAY

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; .
ARRAY

Solution A (for 20 quizzes of a single student)


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);
ARRAY

Solution A (20 quizzes of 42 students)


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); }
ARRAY

A loop can do the trickbut will it be enough?

ARRAY

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 re-compute the average of student 05? What if, you would like to remember all the value of the quizzes? And etc. of what if...

ARRAY

Solution B

Lets use

ARRAYS

ARRAY

ARRAY

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

ARRAY DECLARATION

int quiz[10]; quiz is the name of the array of integers. 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
ARRAY

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; This is an integer quiz[2] = 66; array quiz[3] = 44; quiz[4] = 80; quiz[5] = 90; quiz[6] = 55; quiz[7] = 89; quiz[8] = 12; quiz[9] = 90;
ARRAY

For character array, we use


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;
ARRAY

Example declaration
int num1[4] = {22, 88, 66, 44}; int num2[4 ];

Declarations like this needs to populate the array within the program.
ARRAY

?
? ?

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. ?
? ?

?
ARRAY

We cant do these to 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
ARRAY

Use for LOOP TO TRAVERSE AN ARRAY


int num[4] = {22, 88, 66, 44}; int i; for (i =0;i <4;i++) printf(%d, num*i+);

ARRAY

To COPY an ARRAY
int number[4] = {1,3,5,7}; int number1[4]; int i; for( i=0; i<4; i++) number1[i] = number[i];

ARRAY

Lets go back to our previous problem Solving it using SOLUTION B: ARRAYS

ARRAY

A single student with 20 quizzes


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);
ARRAY

Now you try it for 40 students, each student have 20 quizzes

ARRAY

Statements that can 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 printf(%.1f, x[0]); x[3] = 25.0; sum = x[0] + x[1]; sum += x[2]; x[3] += 1.0; x[2] = x[0] + x[1]; The new ARRAY content: 16.0 12.0 28.0 26.0 2.5 Explanation Displays the value of x[0], which is 16.0. Stores the value 25.0 to x[3]. Adds the value of x[0] and x[1] then stores the result which is 28.0 at variable sum. Adds x[2] to sum. The new sum is 34.0 Adds 1.0 to x[3]. The new x[3] is 26.0 Adds the value of x[0] and x[1] then stores the result which is 28.0 to x[2]. 12.0
ARRAY

14.0

-54.5

Array Subscript and the printf function


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 i=5; printf(%d %.1f, 4, x[4]); printf(%d %.1f, i, x[i]); printf(%.1f, x[i] + 1); printf(%.1f, x[i] + i); printf(%.1f, x[i+1]); printf(%.1f, x[i+i ]); printf(%.1f, x[2*i]); printf(%.1f, x[2*i-3]); Explanation

Displays 4 and 2.5 (value of x[4]) Displays 5 and 12.0 (value of x[5]) Displays 13.0 (value of x[5] plus 1) Displays 17.0 (value of x[5] plus 5) Displays 14.0 (value of x[6]) Invalid. Attempt to display x[10] Invalid. Attempt to display x[10] Displays -54.5 (value of x[7])

ARRAY

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 i=5; printf(%.1f, x[(int)x[4]]); printf(%.1f, x[i++]); printf(%.1f, x[--i] ); x[i-1] = x[i]; x[i] = x [ i+1]; x[i] - 1 = x [i]; Explanation Displays 6.0 (value of x[2]) Displays 12.0 (value of x[5]) then assigns 6 to i; Assign 5 ( 6-1) to i and then displays 12.0 (value of x[5]) Assigns 12.0 (value of x[5] to x[4]) Assigns 14.0 (value of x[6] to x[5]) Illegal assignment statement

ARRAY

Example:
int id[50]; double 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 id[1] 4556 id[2] 5691 id[3] 9099 id[49] 9146

gpa[0] gpa[1] gpa[2] gpa[3]

2.71 3.09 2.98 3.50

gpa[49] 1.92
ARRAY

Parallel Arrays
Two or more arrays with the same number of elements used for storing related information about a collection of data objects.

ARRAY

Vous aimerez peut-être aussi