Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Arrays
Henry Chia
hchia@comp.nus.edu.sg
Lecture Outline
One-Dimensional Arrays
Declaration and initialization
Array element access
Arrays and pointers
Arrays as function arguments
File Processing
File Input
File Output
Problem Statement #1
Read 100 values and find the average.
Requires one pass through the values.
#include <stdio.h>
#define MAX 100
int main(void)
{
int i, val, sum=0;
double ave;
for (i = 1; i <= MAX; i++)
{
scanf("%d", &val);
sum += val;
}
ave = 1.0 * sum / MAX;
printf("The average is %lf\n", ave);
return 0;
}
CS1010E Lecture 8 p.3/34
Problem Statement #2
Read 100 values from the user and find the
number of values greater than the average.
Requires two passes through the values.
Pass #1: Find the average.
Pass #2: Count values greater than the average.
Program design:
Declare 100 variables for input values.
Read and compute the sum of all values.
Compute the average.
Determine how many variables have values greater
than the average using 100 if statements.
CS1010E Lecture 8 p.4/34
One-Dimensional Arrays
Work with a set of data values of the same
type without giving each value a separate
name.
An array is given an identifier name.
Subscripts distinguish between array values.
Subscripts start with 0 and increment by 1.
Memory assignment for an array is
guaranteed to be a sequential group of
memory locations.
One-Dimensional Arrays
@100
s:
@101 @102
@103 @104
@105
-1
15
s[0]
s[1]
s[2]
s[3]
s[4]
s[5]
Array Declaration
An array identifier is followed by an integer
expression in brackets that specifies the
maximum number of elements in the array.
Elements in an array must be the same data
type, eg.
int s[6];
double list[100];
struct Fraction fracArray[50];
int *ptr[10];
int *ptr[10]
(*ptr)[10]
which is a
s: 0 0 0 0 0 0
t: ? ? ? ? ? ? ? ? ? ?
r 1.0
// declare array
Problem #2 Revisited
#include <stdio.h>
#define MAX 100
int main(void)
{
int count=0, sum=0, data[MAX], i;
double ave;
for (i = 0; i < MAX; i++)
{
scanf("%d", &(data[i]));
sum += data[i];
}
ave = 1.0 * sum / MAX;
for (i = 0; i < MAX; i++)
if (data[i] > ave) count++;
printf("%d values greater than %lf\n", count, ave);
return 0;
}
CS1010E Lecture 8 p.13/34
s:
@100
@101
@102
@103
@104
@105
-1
15
ptr @100
ptr
ptr
s[0]
ptr[0]
*ptr
s[i]
ptr[i]
*(ptr+i)
&s[i]
&ptr[i]
ptr+i
ptr++;
or
++ptr;
@100
@101
@102
@103
@104
@105
-1
15
ptr @101
s:
@100
@101
@102
@103
@104
@105
-1
15
ptr2 @103
@1
s: 5 0 -1 2 15 2
swap
6
list
swap(s,1,4);
@1, 1, 4
@1
4
CS1010E Lecture 8 p.23/34
File Input
Arrays are often used to store large amounts
of information that is read from data files.
Typically, reading the file requires
opening the file
testing if the file was successfully opened
reading the file
closing the file
The template in the next slide is used to read
a file named input.txt into an array.
File Input
#include <stdio.h>
int main(void)
{
FILE *inFile;
inFile = fopen("input.txt","r");
if (inFile == NULL)
{
printf("File open error!\n");
return 0;
}
// read the file and continue processing.
fclose(inFile);
return 0;
}
File Input
inFile = fopen("input.txt","r");
File Input
Reading the file for a pre-known number of
data records (eg. 100 records where each
record is an integer value),
for (i = 0; i < 100; i++)
fscanf(inFile, "%d", &data[i]);
File Input
Read the file for an unknown number of
records,
i = 0;
while (fscanf(inFile, "%d", &data[i]) == 1)
i++;
File Output
int main(void)
{
FILE *outFile;
outFile = fopen("input.txt","w");
if (outFile == NULL)
{
printf("File open error!\n");
return 0;
}
// use "w"
Lecture Summary
One-dimensional arrays.
Declaration and initialization.
Array elements as single variables.
Arrays as function arguments
(pass-by-address).
File processing templates.
Count-controlled file input.
Sentinel-controlled file input.
File output.