array

29
ARRAY Arrays

Upload: pia-samantha-daseco

Post on 26-Sep-2015

6 views

Category:

Documents


2 download

DESCRIPTION

this is for your computer subjects

TRANSCRIPT

Group Orange TemplateLet’s analyze this situation…
*
printf(“Enter value for quiz1 -> ”);
scanf(“%d”,&q1);
scanf(“%d”,&q2);
sum = q1 + q2;
}
*
*
int q1, q2, q3, q4…, q20, sum=0, avg=0;
printf(“Enter value for quiz1 -> ”);
scanf(“%d”,&q1);
scanf(“%d”,&q20);
avg = sum/20;
ARRAY
*

int sum1=0, sum2=0, … sum42=0;
int avg1=0, avg1=0, …. Avg42=0;
printf(“Enter value for student 1 quiz1 -> “);
scanf(“%d”, &s1q1);
scanf(“%d”, &s42q1);
of a single student)
printf(“Enter value for quiz %d -> ”, i);
scanf(“%d”, &q1);
sum = sum + q1;
*
42 students)
int s, q;
printf(“Enter value for Student %d, quiz %d -> ”, s, q);
scanf(“%d”, &q1);
sum = sum + q1;
}
*
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
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
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 DECLARATION
ARRAY
int quiz[10] = {22, 88, 66, 44, 80, 90, 55, 89, 90};
or
*
or
int num2[4 ];
*
ARRAY
Unlike the fundamental (atomic) types (char, int, float, double, etc.), arrays are composites, consisting of several values.
?
?
?
?
?
?
?
?
?
?
?
*
printf(“%d”, num1);
int i;
number1[i] = number[i];
Solving it using SOLUTION B: ARRAYS
*
printf(“Enter value for quiz %d -> ”, i+1);
scanf(“%d”, &quiz[i]);
sum = sum + quiz[i];
*
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 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].
The new ARRAY content:
*
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 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)
*
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
*
int id[50];
double gpa[50];
 

*
Parallel Arrays
*