Vous êtes sur la page 1sur 9

CS F111

Lecture 8:
Data Structures: Arrays
Practice Problems
Rainfall Analysis
Possible things to report:
How many days worth of data are there?
How much rain fell on the day with the most
rain?
On how many days was there no rainfall?
What was the average rainfall over the period?
On how many days was the rainfall above
average?
What was the median rainfall?

Given a set of data of rainfall in a file input.txt


calculate the above values
Plot rain vs days using suitable axis limits.
2
Parallel Arrays
Make parallel arrays with
Names of all students in a class.
Associate with each student a unique identifier IDNo
Use this unique identifier to store/retrieve the information
about the student.
Three arrays corresponding to M = 3 courses. Each
containing marks of students in that course
Do data analysis and find the following:
Highest marks in each course and the name of the students.
Which student has the Highest Total
How many students have total marks below 0.25*Highest Total
Plot a Histogram. X axis Marks 0 to 100, Y axis how many students
at each mark.
Parallel Arrays
A set of arrays may be used in parallel when more than
one piece of information must be stored for each item.

Example: we are keeping track of a group of


students. For each item (student), we might
have several pieces of information such as
marks

4
Practice problem

Calculate the frequency of students getting


various marks in a subject out of 100 marks.
Plot this as a graph (Histogram)
Example program
main(){
int i, j, numstudents, freq[100];
for (i = 0; i < 100; i++) freq[i] = 0;

printf("No.of students in class \ n");


scanf("%d", &numstudents);

for (j = 0; j < numstudents; j++)


{ printf("Marks of %dstudent \n", j+1);
scanf("%d", &i);
++freq[i];
}
for (i = 0; i < 100; i++)
if(freq[i]) printf("Students with %d marks=%d\ n",i,freq[i]);
}
Two dimensional arrays
Use array of arrays or 2 Dimensional array to
store the marks for M courses and do the
calculations.
Is there an advantage in using 2 Dim vs
parallel 1 Dim arrays?
Arrays
Task : Write a C program to copy elements of
an array A into another array B so that
following sequences should be followed:
1. All even elements of A from left to right are
copied into C from left to right.
2. All odd elements of A from left to right are
copied into C from right to left.
Arrays
Task : Write a program to input marks of 5
students in 4 subjects each and display what
are the average marks of each student in 4
subjects and display what is the average of
each subject.
Print the array with highest in each course
with 00 in before the marks.

Vous aimerez peut-être aussi