LECTURE 13: Arrays (Read Sections 8.1-8.3, 8.4)
So
far, we covered simple data types (int, double, char)
that
Often,
we would like to group similar data items together. For example, we want
to store and process exam scores of all students in the class. Arrays
address this requirement!
Array is a collection of two or more adjacent memory
locations, called array elements
Array declaration and use
double x[2]; // reserves two memory
locations to store two double values
x[0] = 5.0; // assigns value
5.0 to the first element of the array
x[1] = 3.0;
printf(“%f”,x[0]);
// prints out the value of the first element
x[1] = x[0] + x[1]; // updates
the value of the second array element to 8.0
Array initialization
int sum = 0; // initialization
of integer variable
double x[2] = {5.0, 3.0}; //
initialize values of the two array elements
Array subscripts
double x[10];
x[0] = 1.0;
for (i=2;
i<10; i=i+1)
x[i] = 2*x[i-1];
Question:
what will be the value of element x[9] after the loop is executed?
More
examples: look at Table 8.2 in the textbook
Example:
Enter exam
scores;
Determine
number of entries;
Calculate
average;
Find the
smallest element;
Search the
array for score 60;
Sort the
array;
Calculate
the median;
#include<stdio.h>
int main(void)
{
int scores[20];
int
i, flag;
int
array_size, array_sum, array_avg, array_min;
/* Populate the array */
i = 0;
flag = 0;
do {
printf(“\n Enter an integer
>= 0 (if integer < 0, stop)> “);
scanf(“%d”,&scores[i]);
printf(“... you entered %d”,
scores[i]);
if (scores[i] > 0)
i = i+1;
else
flag = 1;
} while(i < 20 && flag == 0);
array_size = i;
printf(“\n\n***Size of the array is %d”, array_size);
/* Find sum of all
entered integers */
array_sum = 0;
for (i=0; i < array_size;
i = i+1)
array_sum = array_sum + scores[i];
printf(“\n*** Sum of array
elements is %d”, array_sum);
/* Calculate the average of the entered integers */
array_avg = array_sum/array_size;
printf(“\n***Average of the array elements is %d”, array_size);
/* Find the
smallest element */
array_min = scores[0];
for (i = 1; i < array_size;
i++) {
if (scores[i] < array_min)
array_min =
scores[i];
}
printf(“\n***The smallest
element of the array is %d \n”, array_min);
/* Search the array
to check if there is score 60 */
flag = 0;
for (i = 1; i < array_size;
i++) {
if (scores[i] == 60)
flag = 1;
}
if (flag == 1)
printf(“\n***Element with
value 60 exists in the array \n”);
else
printf(“\n***Element with
value 60 does not exist in the array \n”);
/* Sort the array
using selection sort */
for (i = 0; i < array_size-1; i++) {
/* The following
loop finds the minimum element between i-th and the
last element */
tmp_min =
scores[i];
for (j = i+1; j
< array_size; j++) {
if (scores[j]
< tmp_min)
tmp_min =
scores[j];
tmp_min_pos
= j;
}
/* if the minimum element is not i-th element, swap i-th and tmp_min_pos elements */
if (tmp_min < scores[i]) {
scores[tmp_min_pos] = scores[i];
scores[i] = tmp_min;
}
}
/* Calculate the median of the array
*/
if (array_size%2 ==
1)
array_median = score[(array_size+1)/2];
else
array_median =
(score[array_size/2-1] + score[array_size/2]) / 2;
printf(“\n***The median of
the array is %d \n\n”, array_median);
return(0);
}
Miltidimensional Arrays
char tictactoe[3][3];
// declares two-dimensional array with 3 rows and 3 columns
tictactoe[0][2] = ‘x’; // assigns value
‘x’ to the element in the
//
first row and third column of tictactoe