DOC PREVIEW
UMBC CMSC 104 - Arrays, Part 1 of 2

This preview shows page 1-2-23-24 out of 24 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 24 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 24 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 24 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 24 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 24 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Arrays, Part 1 of 2Data TypesArraysArray Declaration and InitializationAccessing Array ElementsAccessing Array Elements (con’t)Modifying ElementsFilling Large ArraysMore DeclarationsUsing #define for Array SizesExample Using Arrays“Clean” Example Using Arrays (con’t)Slide 13Slide 14Slide 15Slide 16Slide 17Improvements ?Improved ProgramImproved Program (con’t)Slide 21Slide 22Slide 23Other Improvements?CMSC 104, Version 9/01 1Arrays, Part 1 of 2Topics•Definition of a Data Structure•Definition of an Array•Array Declaration, Initialization, and Access•Program Example Using ArraysReading•Sections 6.1 - 6.5CMSC 104, Version 9/01 2Data Types•So far, we have seen only simple data types, such as int, float, and char.•Simple variables can hold only one value at any time during program execution, although that value may change.•A data structure is a data type that can hold multiple values at the same time. (Synonyms: complex data type, composite data type)•The array is one kind of data structure.CMSC 104, Version 9/01 3Arrays•An array is a group of related data items that all have the same name and the same data type.•Arrays can be of any data type we choose.•Arrays are static in that they remain the same size throughout program execution.•An array’s data items are stored contiguously in memory.•Each of the data items is known as an element of the array. Each element can be accessed individually.CMSC 104, Version 9/01 4Array Declaration and Initializationint numbers[5] ;•The name of this array is “numbers”.•This declaration sets aside a chunk of memory that is big enough to hold 5 integers.•It does not initialize those memory locations to 0 or any other value. They contain garbage.•Initializing an array may be done with an array initializer, as in : int numbers[5] = { 5, 2, 6, 9, 3 } ;5 2 6 9 3numbersCMSC 104, Version 9/01 5Accessing Array Elements•Each element in an array has a subscript (index) associated with it.•Subscripts are integers and always begin at zero.•Values of individual elements can be accessed by indexing into the array. For example,printf(“The third element = %d.\n”, numbers[2]);would give the outputThe third element = 6.5 2 6 9 3numbers0 1 2 3 4CMSC 104, Version 9/01 6Accessing Array Elements (con’t)•A subscript can also be an expression that evaluates to an integer.numbers[(a + b) * 2] ;•Caution! It is a logical error when a subscript evaluates to a value that is out of range for the particular array. Some systems will handle an out-of-range error gracefully and some will not (including ours).CMSC 104, Version 9/01 7Modifying Elements•Individual elements of an array can also be modified using subscripts. numbers[4] = 20 ; /*changes the value of the element found at subscript 4 to 20 */•Initial values may be stored in an array using indexing, rather than using an array initializer.numbers[0] = 5 ; numbers[1] = 2 ;numbers[2] = 6 ;numbers[3] = 9 ;numbers[4] = 3 ;CMSC 104, Version 9/01 8Filling Large Arrays•Since many arrays are quite large, using an array initializer can be impractical.•Large arrays are often filled using a for loop. for ( i = 0; i < 100; i++ ) { values [ i ] = 0 ; }would set every element of the 100 element array “values” to 0.CMSC 104, Version 9/01 9More 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 a class).•“gradeCount” contains 5 elements (one for each possible grade, A - F).CMSC 104, Version 9/01 10Using #define for Array Sizes#define SIZE 39#define GRADES 5int main ( ){ int score [SIZE] ; int gradeCount [GRADES] ;• • • }CMSC 104, Version 9/01 11Example Using ArraysProblem: Find the average test score and the number of A’s, B’s, C’s, D’s, and F’s for a particular class.Design:MainPrint UserInstructionsCalculateAverage ScoreCMSC 104, Version 9/01 12“Clean” Example Using Arrays (con’t)#include <stdio.h>#define SIZE 39 /* number of tests */#define GRADES 5 /* number of different grades: A, B, C, D, F */void printInstructions ( ) ;double findAverage (double sum, int quantity) ;int main ( ){ int i ; /* loop counter */ int total ; /* total of all scores */ int score [SIZE] ; /* student scores */ int gradeCount [GRADES] ; /* count of A’s, B’s, C’s, D’s, F’s */ double average ; /* average score */ /* Print the instructions for the user */  printInstructions ( ) ; CMSC 104, Version 9/01 13“Clean” Example Using Arrays (con’t) /* Initialize grade counts to zero */ for ( i = 0; i < GRADES; i++ ) { gradeCount [ i ] = 0 ; } /* Fill score array with scores */ for ( i = 0; i < SIZE; i++ ) { printf (“Enter next score: ”) ; scanf (“%d “, &score [ i ] ) ; }CMSC 104, Version 9/01 14“Clean” Example Using Arrays (con’t) /* Calculate score total and count number of each grade */ 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 ; case 7 : gradeCount [2]++ ; break ; case 6 : gradeCount [1]++ ; break ; default : gradeCount [0]++ ; } }CMSC 104, Version 9/01 15“Clean” Example Using Arrays (con’t) /* Calculate the average score */ average = findAverage (total, SIZE) ; /* Print the 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] ) ; return 0 ;} /* end main */CMSC 104, Version 9/01 16“Clean” Example Using Arrays (con’t)/******************************************************************* printInstructions - prints the user instructions** Inputs: None** Outputs:


View Full Document

UMBC CMSC 104 - Arrays, Part 1 of 2

Download Arrays, Part 1 of 2
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Arrays, Part 1 of 2 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Arrays, Part 1 of 2 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?