L19ArraysII.ppt

Download Report

Transcript L19ArraysII.ppt

Arrays II
Using arrays with functions
CMSC 104
1
Array Declarations



int array [5] ;
This declaration sets aside a chunk of
memory that’s big enough to hold 5 integers.
Besides the space needed for the array,
there is also a variable declared which has
the name of the array. This variable holds the
address of the beginning of the array.
array
FE00
CMSC 104
FE00
--
--
--
--
--
0
1
2
3
4
2
Array Name Holds an Address
#include <stdio.h>
main( )
{
int score[5] = {97, 68, 55, 73, 84} ;
printf (“score[0] = %d\n”, score[0]);
printf (“score = %X\n”, score);
printf (“&score[0] = %X\n”, &score[0]);
}
output
CMSC 104
score[0] = 97
score = FE00
&score[0] = FE00
3
How Indexing Works


array [2] = 7 ;
The value assigned, 7, is stored in a memory
location that is calculated using the following
formula :
Target = beginning address + index * sizeof ( array type )
Target location = FE00 + 2 * sizeof ( int )
array
FE00
CMSC 104
FE00 FE04 FE08 FE0C FE10
97
68
7
73
84
0
1
2
3
4
4
Indexing Arrays


CMSC 104
As long as we know
– the beginning location of an array
– the type being held in the array
– the size of the array
then we can access or modify any of its
elements using indexing.
The array name alone (without [ ] ) is just a
variable that contains the starting address of
the block of memory where the array is held.
5
Call by Value




CMSC 104

Until now, we have passed values to
functions.
The function has a local variable to hold its
own copy of the value passed in.
When we make changes to this copy, the
original (back in the calling function) remains
unchanged.
If we want the calling function to know the
changes we’ve made, we have to return the
value.
This is known as calling by value.
6
Passing Arrays to Functions

The function prototype :
void FillArray ( int array[ ], int numElems);

The function definition header:
void FillArray ( int array[ ], int numElems)

The function call:
FillArray ( array, SIZE);

CMSC 104
Notice that we are passing only the name of the array
(the address) and that we aren’t returning anything
(the function is void), because we will be modifying
the original array from within the function.
7
Call by Reference
We can pass addresses to functions.
 When the function is passed an address
it can make changes to the original.
There is no copy made.
 This is great for arrays, because arrays
are usually VERY LARGE. We really
don’t want to make a copy of an array.
It would use too much memory.

CMSC 104
8
Passing an Array to a Function
A Short Example
#include <stdio.h>
#define SIZE 4
void FillArray (int array[ ], int numElems) ;
main ( )
{
int array [SIZE];
FillArray ( array, SIZE);
/* Print the elements of the array */
for ( i = 0; i < SIZE; i++)
{
printf (array[%d] = %d\n”,
i, array[ i ] );
}
}
for ( i = 0; i < numElems; i++)
{
array [i] = i;
}
}
output
CMSC 104
/*******************************************
FillArray is a function that will fill each
element of any integer array passed
to it with a value that is the same as
that element’s subscript.
*******************************************/
void FillArray (int array[ ],
int numElems)
{
int i;
array[0] = 0
array[1] = 1
array[2] = 2
array[3] = 3
9
Grades Example
Using Passing Arrays to Functions
#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) ;
void InitArray (int array [ ], int size) ;
void FillArray (int array [ ], int size) ;
double ProcessGrades (int score [ ], int size, int gradeCount [ ] ) ;
double FindAverage (double sum, int num) ;
void PrintResults (double average, int gradeCount [ ] ) ;
CMSC 104
10
Grades Example
continued (page 2)
main ( )
{
int i, score [SIZE], gradeCount [GRADES] ;
double average;
PrintInstructions ( ) ;
InitArray (gradeCount, GRADES) ;
FillArray (score, SIZE) ;
average = ProcessGrades (score, SIZE, gradeCount ) ;
PrintResults (average, gradeCount) ;
}
CMSC 104
11
Grades Example
continued (page 3)
/* PrintInstructions prints the greeting for the user, explaining the program’s
* purpose, input and output */
void PrintInstructions (void)
{
printf (“This program calculates the average score for a class of \n” ) ;
printf (“%d students. It also reports the number of A’s, B’s, etc. \n“, SIZE) ;
printf (“You will be asked to enter the individual scores.\n”) ;
}
/* InitArray initializes any integer array to hold all zeros when passed the
* array and its size */
void InitArray (int array [ ], int size)
{
for ( i = 0; i < size; i++ )
{
array [ i ] = 0 ;
}
}
CMSC 104
12
Grades Example
continued (page 4)
/* FillArray fills an integer array with valid values that are input by the user.
* This function assures the values are between MIN and MAX, which must
* be #defined in this file. The array and its size are passed in. */
void FillArray (int array [ ], int size)
{
for ( i = 0 ; i < size ; i++)
{
printf (“Enter next value : ”) ;
scanf (“%d “, &array [ i ] ) ;
while ( array [ i ] > MAX || array [ i ] < MIN )
{
printf (“Values must be between %d and %d\n ”, MIN, MAX) ;
printf (“Enter next value : ”) ;
scanf (“%d “, &array [ i ] ) ;
}
}
}
CMSC
104
13
Grades Example
continued (page 5)
/* ProcessGrades takes the array of scores, its size, and the array of
* grade counters as arguments. It counts the grades, modifying the
* gradeCount array, and returns the class average. A, B, C, D & F
* must be #defined in this file */
double ProcessGrades (int score [ ], int size, int gradeCount [ ] )
{
int total = 0;
double average;
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 ;
CMSC 104
14
Grades Example
continued (page 6)
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 = FindAverage (total, size) ;
return average;
}
CMSC 104
15
Grades Example
continued (page 7)
/* FindAverage finds and returns the average when passed the sum of
* some items and the number of items. If there are no items, 0 is returned. */
double FindAverage (double sum, int num)
{
double average;
if (num != 0)
{
average = sum / num ;
}
else
{
average = 0 ;
}
return average;
}
CMSC 104
16
Grades Example
continued (page 8)
/* PrintResults prints the class average and the grades distribution for
* the class. A, B, C, D, & F must be #defined in this file */
void PrintResults (double average, int gradeCount [ ] )
{
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] ) ;
}
CMSC 104
17