Arrays: Mirza Mohammad Lutfe Elahi
Arrays: Mirza Mohammad Lutfe Elahi
Outline
• Declaring, Initializing, and Indexing Arrays
• Array Arguments
What is an Array?
• Scalar data types, such as int, store a single value
• Sometimes, we need to store a collection of values
• An array is a collection of data items, such that:
– All data values are of the same type (such as int)
– Are referenced by the same array name
• Individual cells in an array are called array elements
• An array is called a data structure
– Because it stores many data items under the same name
• Example: using an array to store exam scores
CSE 115 Programming Language I ECE@NSU
4
Declaring an Array
• To declare an array, we must declare:
– The array name
– The type of array element
– The number of array elements
• Example: double x[8];
• Associate 8 elements with array name x
Initializing Arrays
• You can declare a variable without initialization
double average; /* Not initialized */
• You can also declare a variable with initialization
int sum = 0; /* Initialized to 0 */
• Similarly, you can declare arrays without initialization
double x[20]; /* Not initialized */
• You can also declare an array and initialize it
int prime[5] = {2, 3, 5, 7, 11};
• No need to specify the array size when initializing it
int prime[] = {2, 3, 5, 7, 11};
Memory
Array A Addresses
All arrays start at index 0 0 9 342900
1 5 342904
Array Index 2 -3 342908
Array Element 3 10 342912
4 27 342916
5 -8 342920
Array Indexing
double x[8];
• Each element of x stores a value of type double
• The elements are indexed starting with index 0
– An array with 8 elements is indexed from 0 to 7
Arrays of Characters
• You can declare and initialize a char array as
follows:
char vowels[] = {'A','E','I','O','U'};
• You can also use a string to initialize a char array:
char string[] = "This is a string";
• It is better to use a named constant as the array size:
#define SIZE 100
. . .
char name[SIZE]; /* Not initialized */
• You can declare arrays and variables on same line:
char name[SIZE], answer;
CSE 115 Programming Language I ECE@NSU
11
Array Input/Output
#include<stdio.h>
#define SIZE 5 /* array size */
int main(void) {
double x[SIZE];
int i;
return 0;
}
CSE 115 Programming Language I ECE@NSU
12
double sum = 0;
double sum_sqr = 0;
#include <stdio.h>
#include <math.h>
#define SIZE 8 /* array size */
int main(void) {
double x[SIZE], mean, st_dev, sum=0, sum_sqr=0;
int i;
return 0;
}
Sample Run…
Array Arguments
• Besides passing array elements to functions, we can
write functions that have arrays as arguments
• Such functions can compute some or all of the array
elements
• Unlike scalar variables where we have the option of
passing either the value or address of a variable to a
function, C only passes the address of an array to a
function array argument
• An array cannot be passed by value to a function
Array Arguments
fill_array(x, 5, 1)
Example: read_array
/* read n doubles from the keyboard */
/* return an array of n doubles */
void read_array(double list[], int n) {
int i;
printf("Enter %d real numbers\n", n);
printf("Separated by spaces or newlines\n");
printf("\n>");
for(i = 0; i < n; ++i)
scanf("%lf", &list[i]);
}
return 0;
}
CSE 115 Programming Language I ECE@NSU
30
Sample Run…
int main(void) {
double array[SIZE];
int count = read_file("scores.txt", array);
printf("Count of array elements = %d\n", count);
print_array(array, count);
return 0;
}
CSE 115 Programming Language I ECE@NSU
int read_file(const char filename[], double list[]) { 35
int count = 0;
FILE *infile = fopen(filename, "r");
if (infile == NULL) { /* failed to open file */
printf("Cannot open file %s\n", filename);
return 0; /* exit function */
}
int status = fscanf(infile, "%lf", &list[count]);
while(status == 1) { /* successful read */
count++; /* count element */
if(count == SIZE)
break; /* exit while */
status = fscanf(infile, "%lf", &list[count]);
}
fclose(infile);
return count; /* number of elements read */
}
Sample Run
Cannot read
abc as double
CSE 115 Programming Language I ECE@NSU