Declaring and Referencing Arrays
Declaring and Referencing Arrays
CCC101 ARRAYS
Declaring and referencing arrays
double x[8];
the integer '8' enclosed in a bracket is the array subscript, and its
double x[8]; value must be in the range from zero to one less than the number
of memory cells in the array.
CCC101 ARRAYS
Declaring and referencing arrays
double x[8]; -54.
Array x 16.0 12.0 6.0 8.0 2.5 12.0 14.0 5
x[0] x[1] x[2] x[3] x[4] x[5] x[6] x[7]
8 elements
2nd array element last array element
x[3] = 25.0;
sum += x[2];
x[3] +=1.0;
x[2] = x[0] + x[1];
CCC101 ARRAYS
Declaring and referencing arrays
-54.
16.0 12.0 6.0 8.0 2.5 12.0 14.0 5
x[0] x[1] x[2] x[3] x[4] x[5] x[6] x[7]
CCC101 ARRAYS
Declaring and referencing arrays
-54.
16.0 12.0 6.0 8.0 2.5 12.0 14.0 5
x[0] x[1] x[2] x[3] x[4] x[5] x[6] x[7]
CCC101 ARRAYS
Declaring and referencing arrays
-54.
16.0 12.0 6.0 25 2.5 12.0 14.0 5
x[0] x[1] x[2] x[3] x[4] x[5] x[6] x[7]
CCC101 ARRAYS
Declaring and referencing arrays
-54.
16.0 12.0 6.0 25 2.5 12.0 14.0 5
x[0] x[1] x[2] x[3] x[4] x[5] x[6] x[7]
CCC101 ARRAYS
Declaring and referencing arrays
-54.
16.0 12.0 6.0 25.0 2.5 12.0 14.0 5
x[0] x[1] x[2] x[3] x[4] x[5] x[6] x[7]
sum = x[0] +x[1]; Stores the sum of x[0] and x[1], which is
28.0 in the variable sum.
CCC101 ARRAYS
Declaring and referencing arrays
-54.
16.0 12.0 6.0 25.0 2.5 12.0 14.0 5
x[0] x[1] x[2] x[3] x[4] x[5] x[6] x[7]
CCC101 ARRAYS
Declaring and referencing arrays
-54.
16.0 12.0 6.0 25.0 2.5 12.0 14.0 5
x[0] x[1] x[2] x[3] x[4] x[5] x[6] x[7]
CCC101 ARRAYS
Declaring and referencing arrays
-54.
16.0 12.0 6.0 25.0 2.5 12.0 14.0 5
x[0] x[1] x[2] x[3] x[4] x[5] x[6] x[7]
CCC101 ARRAYS
Declaring and referencing arrays
-54.
16.0 12.0 6.0 25.0 2.5 12.0 14.0 5
x[0] x[1] x[2] x[3] x[4] x[5] x[6] x[7]
Statement Explanation
CCC101 ARRAYS
Declaring and referencing arrays
-54.
16.0 12.0 6.0 26.0 2.5 12.0 14.0 5
x[0] x[1] x[2] x[3] x[4] x[5] x[6] x[7]
Statement Explanation
CCC101 ARRAYS
Declaring and referencing arrays
-54.
16.0 12.0 6.0 26.0 2.5 12.0 14.0 5
x[0] x[1] x[2] x[3] x[4] x[5] x[6] x[7]
Statement Explanation
CCC101 ARRAYS
Declaring and referencing arrays
-54.
16.0 12.0 6.0 26.0 2.5 12.0 14.0 5
x[0] x[1] x[2] x[3] x[4] x[5] x[6] x[7]
Statement Explanation
CCC101 ARRAYS
Declaring and referencing arrays
-54.
16.0 12.0 28.0 26.0 2.5 12.0 14.0 5
x[0] x[1] x[2] x[3] x[4] x[5] x[6] x[7]
Statement Explanation
Stores the sum of x[0] and x[1] in x[2].
x[2] = x[0]+x[1];
The new x[2] is 28.0
Hence, current x[2]= 6.0.
Therefore x[2] =x[0]+x[1];
That makes x[2]=16.0+12=28.0
CCC101 ARRAYS
Declaring and referencing arrays
-54. statements
16.0 12.0 6.0 8.0 2.5 12.0 14.0 5 BEFORE
x[0] x[1] x[2] x[3] x[4] x[5] x[6] x[7] printf("%.lf", x[0]);
x[3] = 25.0;
sum += x[2];
-54. x[3] +=1.0;
16.0 12.0 28.0 26.0 2.5 12.0 14.0 5 AFTER x[2] = x[0] + x[1];
CCC101 ARRAYS
Declaring and referencing arrays
We declare two arrays for a student records program as follows:
int id[NUM_STUDENTS];
double gpa[NUM_STUDENTS];
#define NUM_STUDENTS 50
CCC101 ARRAYS
Declaring and referencing arrays
#define NUM_STUDENTS 50
int id[NUM_STUDENTS];
double gpa[NUM_STUDENTS];
The arrays id and gpa each have 50 elements. Each element of array if can be used to
store an integer value; each element of array gpa can be used to store a value of
type double.
CCC101 ARRAYS
Declaring and referencing arrays
#define NUM_STUDENTS 50
int id[NUM_STUDENTS];
double gpa[NUM_STUDENTS];
If you use declarations in a problem to assess the range and distribution of grade
point averages, you can store the first student's id in id[0], and store the same
student's gpa in gpa[0].
The data stored in id[i] and gpa[i] relate to the ith student.
CCC101 ARRAYS
Declaring and referencing arrays
#define NUM_STUDENTS 50
int id[NUM_STUDENTS];
double gpa[NUM_STUDENTS];
If you use declarations in a problem to assess the range and distribution of grade
point averages, you can store the first student's id in id[0], and store the same
student's gpa in gpa[0].
The data stored in id[i] and gpa[i] relate to the ith student.
CCC101 ARRAYS
Declaring and referencing arrays
#define NUM_STUDENTS 50
id[0] 5503 gpa[0] 2.71
int id[NUM_STUDENTS];
double gpa[NUM_STUDENTS];
id[1] 4556 gpa[1] 3.09
Parallel arrays
Two or more arrays with the id[2] 5691 gpa[2] 2.98
same number of elements
used for storing related ... ...
information about a
id[49] 9416 gpa[49] 1.92
collection of data objects.
CCC101 ARRAYS
Declaring and referencing arrays
You can declare more than one array in a single type declaration.
CCC101 ARRAYS
Array initialization
We can initialize a simple variable when we declare it:
We can also initialize an array in its declaration. We can omit the size
of an array that is being fully initialized since the size can be
deduced from the initialization list. Ex: we initialize a 25-element
array with prime number less than 50.
CCC101 ARRAYS
Array initialization
We can initialize a simple variable when we declare it:
We can also initialize an array in its declaration. We can omit the size
of an array that is being fully initialized since the size can be
deduced from the initialization list. Ex: we initialize a 25-element
array with prime number less than 50.
CCC101 ARRAYS
Using loops for sequential access
The following array square will be The for loop
used to store the squares of the
integers 0 through 10. Example: for (i=0;i<SIZE;i++)
square[0] is 0, square [10] is 100. square[i] =i*i;
We assume that the name SIZE
has been defined to be 11. initializes this array, as shown
Array Square
int square [SIZE] i;
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10]
0 1 4 9 16 25 36 49 64 81 100
CCC101 ARRAYS
ACTIVITY in the main function, ask input from user (use
for loop)
Write a program that computes the in the main function, compute the sum and
mean and standard deviation of an the sum of squares of all data
array of data and displays the in the main function, compute and print the
difference between each value and mean and standard deviation
the mean.
in the main function, display the difference
Hint: You may use the following: between each item and the mean
mean=sum/MAX_ITEM
#define MAX_Item 8 /*maximum number of items in list of data*/
standard_dev= SquareRoot
in the main function, declare all the
necessary variables and array (sumOfSquaresofData/MAX_ITEM - mean
variables *mean);
CCC101 ARRAYS
Enter 8 numbers separated by blanks:
ACTIVITY
>16 12 6 8 2.5 12 14 -54.5
The mean is 2.00
The standard deviation is 21.75
Write a program that computes the
mean and standard deviation of an
Table of differences between data values and mean.
array of data and displays the
difference between each value and Index Item Difference
the mean. 0 16.00 14.00
1 12.00 10.00
2 6.00 4.00
3 8.00 6.00
4 2.50 0.50
5 12.00 10.00
6 14.00 12.00
7 -54.50 -56.50
CCC101 ARRAYS