Array
Array
ARRAY
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); }
ARRAY
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
scanf(%d, &q1); sum = sum + q1; } avg = sum/20; printf(Average of the %d student is %d -> , s, avg); }
ARRAY
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
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
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
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
ARRAY
ARRAY
14.0
-54.5
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[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