Presentation is loading. Please wait.

Presentation is loading. Please wait.

Arrays I Handling lists of data.

Similar presentations


Presentation on theme: "Arrays I Handling lists of data."— Presentation transcript:

1 Arrays I Handling lists of data

2 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 execution.

3 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.

4 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 5 2 6 9 3

5 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 array.

6 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 */ Values may be stored in an array using indexing, rather than using the array initializer.

7 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 element array, rolls, to 0.

8 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)

9 Using #define for array sizes
#define SIZE #define GRADES 5 main ( ) { int score [SIZE] ; int gradeCount [GRADES] ; } We often use the #define to give the sizes of arrays.

10 Grades Example /* Fill score array with scores */
for ( i = 0 ; i < SIZE ; i++) { printf (“Enter next score : ”) ; scanf (“%d “, &score [ i ] ); } /* Calculate total & count grades */ total += score [ i ] ; switch ( score [ i ] / 10 ) case 10 : case 9 : gradeCount [4]++ ; break ; case 8 : gradeCount [3]++ ; #include <stdio.h> #define SIZE #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 ; }

11 Grades Example continued
case 7 : gradeCount [2]++ ; break ; case 6 : gradeCount [1]++ ; 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] ) ; 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” ) ;

12 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; }

13 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 values.

14 Improved Grades Example
/* 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); #include <stdio.h> #define SIZE #define GRADES 5 #define A #define B #define C #define D #define F #define MAX #define MIN void PrintInstructions (void) ; double FindAver (double sum, int num) ; main ( ) { int i, total, score [SIZE] ; int gradeCount [GRADES] ; double average; PrintInstructions( ) ;

15 Improved Grades Example continued
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] ) ; /* 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]++ ; case 7 : gradeCount [C]++ ; case 6 : gradeCount [D]++ ; case 5 : case 4 : case 3 : case 2 : case 1 :

16 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” ) ; } /* 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; }

17 Other Improvements ? Why is main so large ? Couldn’t we write functions to : Initialize an array to hold all 0s ? Fill an array with values input by the user ? Count the grades, and find the class average ? Print the results ? We can as soon as we learn about passing arrays to functions, in the next lecture.


Download ppt "Arrays I Handling lists of data."

Similar presentations


Ads by Google