Transcript LECT22.PPT

Arrays I
Handling lists of data
CMSC 104
1
Arrays
A data structure is a group of related
data items.
 The array is one kind of data structure.
 An array is a group of related data
items that all have the same name and
the same data type.
 Arrays are static in that they remain
the same size throughout program
CMSC 104 execution.

2
Arrays
An array is a sequence of data items,
all of the same type that are stored
contiguously in memory.
 Each of the data items is known as an
element of the array
 We can access the individual elements
of the array using an indexing scheme
 Arrays can be of any type we choose.

CMSC 104
3
Array Declarations




int array [5] ;
This declaration sets aside a chunk of
memory that’s big enough to hold 5 integers.
It does not initialize those memory locations
to 0 or any other value.
Initializing an array may be done with an
array initializer, as in :
int array [5] = { 5, 2, 6, 9, 3 } ;
array
CMSC 104
5
2
6
9
3
0
1
2
3
4
4
Indexing Array Elements
Values of individual elements can be
found by indexing into the array. In our
example, array [0] is equal to 5 and
array [3] is equal to 9.
 The integer in square brackets is
called the subscript.
 The subscript could also be an
expression that evaluates to an integer.
 In our example, array is the name of the
CMSC 104
array.

5
Modifying Elements

Individual elements of the array can
also be modified using subscripts.
array [4] = 20 ; /*changes the value of
the element found at
subscript 4 to 20 */

CMSC 104
Values may be stored in an array using
indexing, rather than using the array
initializer.
6
Filling Arrays
Since many arrays are quite large,
using an array initializer is impractical.
 Large arrays are often filled using a for
loop.
for ( i = 0; i < 100; i++)
{
rolls [ i ] = 0 ;
}
would set every element of the 100
CMSC 104
element array, rolls, to 0.

7
More Declarations
int score [39] , gradeCount [5];

Declares two arrays of type int
Neither array has been initialized
 score contains 39 elements (one for
each student in the class)
 gradeCount contains 5 elements (one
for each possible grade, A-F)

CMSC 104
8
Using #define for array sizes

We often use the
#define to give the
sizes of arrays.
#define SIZE
39
#define GRADES 5
main ( )
{
int score [SIZE] ;
int gradeCount [GRADES] ;



}
CMSC 104
9
Grades Example
#include <stdio.h>
#define SIZE
39
#define GRADES 5
void PrintInstructions (void) ;
double FindAver (double sum, int num) ;
main ( )
{
int i, total, score [SIZE] ;
int gradeCount [GRADES] ;
double average;
PrintInstructions ( ) ;
/* Initialize gradeCount array to 0s */
for ( i = 0; i < GRADES; i++ )
{
gradeCount [ i ] = 0 ;
}
CMSC 104
/* Fill score array with scores */
for ( i = 0 ; i < SIZE ; i++)
{
printf (“Enter next score : ”) ;
scanf (“%d “, &score [ i ] );
}
/* Calculate total & count grades */
for ( i = 0 ; i < SIZE ; i++)
{
total += score [ i ] ;
switch ( score [ i ] / 10 )
{
case 10 :
case 9 : gradeCount [4]++ ;
break ;
case 8 : gradeCount [3]++ ;
break ;
10
Grades Example
continued
case 7 : gradeCount [2]++ ;
break ;
case 6 : gradeCount [1]++ ;
break ;
default : gradeCount [0]++ ;
}
}
average = FindAver (total, SIZE) ;
/* Print results */
printf (“The class average is %.2f\n”,
average ) ;
printf (“There were %2d As\n”,
gradeCount [4] ) ;
printf (“
%2d Bs\n”,
gradeCount [3] ) ;
printf (“
%2d Cs\n”,
gradeCount [2] ) ;
CMSC 104
printf (“
%2d Ds\n”,
gradeCount [1] ) ;
printf (“
%2d Fs\n”,
gradeCount [0] ) ;
}
/* PrintInstructions prints the greeting
* for the user, explaining the program’s
* purpose, input and output */
void PrintInstructions (void)
{
printf (“This program calculates ”) ;
printf (“the average score for a \n” ) ;
printf (“class of 39 students. It “) ;
printf (“also reports the number\n”) ;
printf (“of A’s, B’s, etc. You will be”) ;
printf (“asked to enter the\n”);
printf (“individual scores\n” ) ;
11
}
Grades Example
continued
/* FindAver finds and returns the
* average when passed the sum of
* some items and the number of
* items. */
double FindAver (double sum, int num)
{
double average;
average = sum / num ;
return average;
}
CMSC 104
12
Improvements ?

We’re trusting the user to enter valid grades.
Let’s add input error checking.

If we aren’t handling our array correctly, it’s
possible that we may be evaluating garbage
rather than valid scores. We’ll handle this by
adding all the cases for Fs to our switch
structure and using the default case for
reporting errors.

We still have the “magic numbers” 4, 3, 2, 1,
and 0 that are the quality points associated
with grades. Let’s use constants for these
CMSC 104
13
Improved Grades Example
#include <stdio.h>
#define SIZE
39
#define GRADES 5
#define A
4
#define B
3
#define C
2
#define D
1
#define F
0
#define MAX
109
#define MIN
0
void PrintInstructions (void) ;
double FindAver (double sum, int num) ;
main ( )
{
int i, total, score [SIZE] ;
int gradeCount [GRADES] ;
double average;
PrintInstructions( ) ;
/* Initialize gradeCount array to 0s */
for ( i = 0; i < GRADES; i++ )
{
gradeCount [ i ] = 0 ;
}
/* Fill array with valid scores */
for ( i = 0 ; i < SIZE ; i++)
{
printf (“Enter next score : ”) ;
scanf (“%d “, &score [ i ] ) ;
while ( score [ i ] > MAX ||
score [ i ] < MIN )
{
printf (“Scores must be between”);
printf (“ %d and %d\n”, MIN, MAX)
printf (“Enter next score : ”) ;
scanf (“%d “, &score [ i ] ) ;
}
}
Improved Grades Example
continued
/* Calculate total & count grades */
for ( i = 0 ; i < SIZE ; i++)
{
total += score [ i ] ;
switch ( score [ i ] / 10 )
{
case 10 :
case 9 : gradeCount [A]++ ;
break ;
case 8 : gradeCount [B]++ ;
break ;
case 7 : gradeCount [C]++ ;
break ;
case 6 : gradeCount [D]++ ;
break ;
case 5 :
case 4 :
case 3 :
case 2 :
case 1 :
}
case 0 :gradeCount [F]++ ;
break;
default : printf (“Error in score\n”);
}
}
average = FindAver (total, SIZE) ;
/* Print results */
printf (“The class average is %.2f\n”,
average ) ;
printf (“There were %2d As\n”,
gradeCount [A] ) ;
printf (“
%2d Bs\n”,
gradeCount [B] ) ;
printf (“
%2d Cs\n”,
gradeCount [C] ) ;
printf (“
%2d Ds\n”,
gradeCount [D] ) ;
printf (“
%2d Fs\n”,
gradeCount [F] ) ;
Grades Example
continued
/* PrintInstructions prints the greeting
* for the user, explaining the program’s
* purpose, input and output */
void PrintInstructions (void)
{
printf (“This program calculates ”) ;
printf (“the average score for a \n” ) ;
printf (“class of %d students.“, SIZE) ;
printf (“ It also reports the number”) ;
printf (“\nof A’s, B’s, etc. You will”) ;
printf (“ be asked to enter the\n”);
printf (“individual scores\n” ) ;
}
CMSC 104
/* FindAver finds and returns the
* average when passed the sum of
* some items and the number of
* items. */
double FindAver (double sum, int num)
{
double average;
average = sum / num ;
return average;
}
16
Other Improvements ?



CMSC 104
Why is main so large ?
Couldn’t we write functions to :
o Initialize an array to hold all 0s ?
o Fill an array with values input by the user ?
o Count the grades, and find the class
average ?
o Print the results ?
We can as soon as we learn about
passing arrays to functions, in the next
lecture.
17