0% found this document useful (0 votes)
24 views

Declaring and Referencing Arrays

An array is a collection of adjacent memory cells called elements. To declare an array, the name and number of elements must be specified. For example, "double x[8]" declares an array x with 8 elements of type double. Each element can be accessed using a subscript from 0 to the number of elements minus one. Parallel arrays store related data about a collection of objects, with corresponding elements relating to the same object.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Declaring and Referencing Arrays

An array is a collection of adjacent memory cells called elements. To declare an array, the name and number of elements must be specified. For example, "double x[8]" declares an array x with 8 elements of type double. Each element can be accessed using a subscript from 0 to the number of elements minus one. Parallel arrays store related data about a collection of objects, with corresponding elements relating to the same object.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

Declaring and referencing arrays

An array is a collection To set-up an array in The declaration


of two or more adjacent memory, we must

memory cells called declare both name of the double x[8];


array elements. array and the number of
cells associated with it.

instructs the compiler to associate eight memory cells with


the name x; these memory cells will be adjacent to each other
double x[8];
in memory. Each element of array x may contain a single type
double value.

CCC101 ARRAYS
Declaring and referencing arrays
double x[8];

used to reference the initial or


subscripted variable x[0] (read as x sub zero)
0th element of the array x
x[1] is the next element and x[7] is the last element.

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

printf("%.lf", x[0]); Displays the value of x[0], which is 16.0

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]

Statements that manipulate the array Explanation

printf("%.lf", x[0]); Displays the value of x[0], which is 16.0

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]

Statements that manipulate the array Explanation

x[3] = 25.0; ?????

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]

Statements that manipulate the array Explanation

x[3] = 25.0; Stores the value 25.0 in x[3].

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]

Statements that manipulate the array Explanation

sum = x[0] +x[1]; ????

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]

Statements that manipulate the array Explanation

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]

Statements that manipulate the array Explanation

sum += x[2]; ????????

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]

Statements that manipulate the array Explanation

sum += x[2]; Adds x[2] to sum. The new sum is 34.0

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]

Statements that manipulate the array Explanation

sum += x[2]; Adds x[2] to sum. The new sum is 34.0


Hence, current sum = 28.
Therefore 28 +=x[2] (x[2]) is 6.0.
That makes 28.0+6.0=34.0

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

x[3] +=1.0; ?????

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

x[3] +=1.0; Adds 1.0 to x[3]. The new x[3] is 26.0.

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

x[3] +=1.0; Adds 1.0 to x[3]. The new x[3] is 26.0.


Hence, current x[3]= 25.
Therefore x[3] +=1.0 = 26.0
That makes 25.0+1.0=26.0
DATA TYPE IS DOUBLE. THOU SHALL NOT NEGLECT THE DECIMAL
PLACEMENT. OTHERWISE, YOUR ANSWER IS WRONG.

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

x[2] = x[0]+x[1]; ???????

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

x[0] x[1] x[2] x[3] x[4] x[5] x[6] x[7]

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

Here we assume that NUM_STUDENTS has already appeared in a #define derivative

#define NUM_STUDENTS 50

Here we assume that NUM_STUDENTS has already appeared in a #define derivative

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.

double cactus[5],needle, pins[6];

int factor[12],n, index;

CCC101 ARRAYS
Array initialization
We can initialize a simple variable when we declare it:

int sum =0;

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.

int_prime_50 []= {2,3,5,7,11,13,17,19,23,29,31,37,41,43,47}

CCC101 ARRAYS
Array initialization
We can initialize a simple variable when we declare it:

int sum =0;

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.

int_prime_50 []= {2,3,5,7,11,13,17,19,23,29,31,37,41,43,47}

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

You might also like