Escolar Documentos
Profissional Documentos
Cultura Documentos
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 -> );
Is this what
scanf(%d,&q2);
you had in
sum = q1 + q2;
mind?
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
ARRAY
Solution A (for 20
quizzes
of a single student)
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
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;
This is an integer
quiz[1] = 88;
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
?
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
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
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
double x[8];
function
x[0] x[1] x[2]
x[3]
16.0 12.0 6.0
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]);
8.0
x[4]
x[5]
x[6]
x[7]
2.5
12.0 14.0 -54.5
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 gpa[0] 2.71
id[1] 4556 gpa[1] 3.09
id[2] 5691 gpa[2] 2.98
id[3] 9099 gpa[3] 3.50
Parallel Arrays
Two or more arrays with the
same number of elements
used for storing related
information about a collection
of data objects.
ARRAY