15 Arrays.pptx - Topic 15 Arrays 1 Displaying a Report 2 Problem We want to process 10 test scores and display a report A sample report is as follows

15 Arrays.pptx - Topic 15 Arrays 1 Displaying a Report 2...

This preview shows page 1 - 18 out of 64 pages.

Topic 15 Arrays 1
Image of page 1
Displaying a Report Problem: We want to process 10 test scores and display a report. A sample report is as follows: Score Grade 63 C 92 A 84 A . . . . . . 77 B 2
Image of page 2
Displaying a Report Suppose we do it using this method: cout << "Score\tGrade\n:"; for(int i = 1; i <= 10; i++) { cout << "Enter test score " << i << ": "; cin >> score; if(score >= 80) . . . cout << score << "\t" << grade << endl; } 3
Image of page 3
Displaying a Report The input/output for this program will be as follows: Enter test score 1: 63 63 C Enter test score 2: 92 92 A . . . This method cannot be used to display the report in the format we want. 4
Image of page 4
Displaying a Report The program must read and store all the test scores, then process them and display the report. We should use 10 variables as follows: 5
Image of page 5
Displaying a Report We read all the test scores and store them in the 1 0 variables. We cannot use a loop now. We must use cin object 1 0 times. cout << "Enter test score 1: "; cin >> score0 ; cout << "Enter test score 2: "; cin >> score1 ; . . . . cout << "Enter test score 10: "; cin >> score9 ; 6
Image of page 6
Displaying a Report After we get all the test scores, we process them and display the score and the grade. We have to repeat the same actions 1 0 times. if( score0 >= 80) . . . cout << score0 << "\t" << grade << endl; if( score1 >= 80) . . . cout << score1 << "\t" << grade << endl; . . . . 7
Image of page 7
Displaying a Report Now the input/output will be as follows: Enter test score 1: 63 Enter test score 2: 92 . . . Enter test score 10: 77 Score Grade 63 C 92 A . . . 77 B 8
Image of page 8
Array ConceptBut what if we have many test scores, say 50? We cannot use this method!A better way is to use an array.An arrayis a collection of elementsthe same data type.An array has a name and a size. of 9
Image of page 9
Array Concept Example: Array with 1 0 elements to store some data values. Array name – scores Array size – 1 0 Array name Array with 10 elements 10
Image of page 10
Declaring an ArrayAn array must be declared before it can be used.General format for array declaration:element_type array_name[ array_size ]; Example:int scores[10];array namearray size 11
Image of page 11
Declaring an Array int scores[10]; char name[10]; Array to store 10 integers Array to store 10 characters ( not a string ) 12 [9]
Image of page 12
Declaring an Array double gpa[40]; Note: All the elements are the same data type. Array to store 40 floating point numbers 13
Image of page 13
Accessing Elements in Arrays The elements in the array are arranged in a sequence. We can refer to the elements as the first element, the second element, and so forth until we get to the last element. 1 st element 10 th element 2nd element . . . . 14
Image of page 14
Accessing Elements Each element in the array has an index . The indexes are written in brackets ([ ]). The first element has index 0. The last element has index ( array_size - 1 ). inde x 15
Image of page 15
1 st element 10 th element 2nd element . . . . inde x Last element index is 9 First element index is 0 16
Image of page 16
Accessing Elements in Arrays We use the index to refer to an element in the array. Example: if we have an array declaration as follows: int scores[ 1 0]; Then we can use the indexes as follows: scores[0] – refers to the 1 st element.
Image of page 17
Image of page 18

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture