Problem Statement #1: Arrays
Problem Statement #1: Arrays
CS1010E Lecture 8
Arrays
int main(void)
{
int i, val, sum=0;
double ave;
Henry Chia
[email protected]
return 0;
}
CS1010E Lecture 8 p.1/34
Lecture Outline
Problem Statement #2
Read 100 values from the user and find the
number of values greater than the average.
One-Dimensional Arrays
Declaration and initialization
Array element access
Arrays and pointers
Arrays as function arguments
File Processing
File Input
File Output
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.2/34
One-Dimensional Arrays
Array Declaration
int *ptr[10]
(*ptr)[10]
which is a
s:
One-Dimensional Arrays
-1
15
s[0]
s[1]
s[2]
s[3]
s[4]
s[5]
// declare array
main
int main(void)
{
int s[6] = {0};
double t[10],
r = 1.0;
return 0;
s: 0 0 0 0 0 0
t: ? ? ? ? ? ? ? ? ? ?
r 1.0
Problem #2 Revisited
#include <stdio.h>
#define MAX 100
int main(void)
{
int count=0, sum=0, data[MAX], i;
double ave;
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
int main(void)
{
int s[6]={5,0,-1,2,15,2};
// swapped?
swap(s[1],s[4]);
printf("%d %d", s[1], s[4]);
return 0;
ptr1 @100
}
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
int main(void)
{
int s[6]={5,0,-1,2,15,2};
swap(s,1,4);
printf("%d %d", s[1], s[4]);
return 0;
}
void swap(int *list, int x, int y)
{
int temp;
// temp = *(list+x);
temp = list[x];
list[x] = list[y]; // *(list+x) = *(list+y);
list[y] = temp;
// *(list+y) = temp;
return;
}
return;
}
File Input
Arrays are often used to store large amounts
of information that is read from data files.
#include <stdio.h>
#define MAX 100
double getAverage(int data[], int numElem);
int countAboveAverage(int data[], int numElem, int ave);
int main(void)
{
int count=0, sum=0, data[MAX], i;
double ave;
for (i = 0; i < MAX; i++)
scanf("%d", &(data[i]));
ave = getAverage(data,MAX);
count = countAboveAverage(data,MAX,ave);
printf("%d values greater than %lf\n", count, ave);
return 0;
}
CS1010E Lecture 8 p.26/34
File Input
File Input
Reading the file for a pre-known number of
data records (eg. 100 records where each
record is an integer value),
#include <stdio.h>
int main(void)
{
FILE *inFile;
inFile = fopen("input.txt","r");
if (inFile == NULL)
{
printf("File open error!\n");
return 0;
}
return 0;
}
File Input
File Input
inFile = fopen("input.txt","r");
fclose(inFile)
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.