View the step-by-step solution to:

# Topics 8.1 Arrays Hold Multiple Values 8.2 Accessing Array Elements 8.3 Inputting and Displaying Array Contents 8.4 Array Initialization 8.5...

5)Loterry:
Write a program that simulates a lottery. The program should havean array of five integers named winningDigits, and should generate a random number in the range of 0 through 9 for each element in the array. The program should ask the user to enter five digits which should be stored in an second integer array named player. The program is to compare the corresponding elements in the two arrays and keep a count of the digits that match. For example, the following shows the winningDigits array and the Player array with sample numbers stored in each. There are two matching digits (elements 2 and 4).

lottery array: 7,4,9,1,3

user array: 4,2,9,7,3

Once the user has entered a set of number, the program should display the winning digits and the player's digits and tell how many digits matched.

Input Validation: Do not accept player inputs less than 0 or greater than 9.

Topics 8.1 Arrays Hold Multiple Values 8.2 Accessing Array Elements 8.3 Inputting and Displaying Array Contents 8.4 Array Initialization 8.5 Processing Array Contents 8.6 Using Parallel Arrays 8.7 The typedef Statement 8.8 Arrays as Function Arguments 8.9 Two-Dimensional Arrays 8.10 Arrays with Three or More Dimensions 8.11 Vectors 8.12 Arrays of Class Objects 8.1 Arrays Hold Multiple Values * Array : variable that can store multiple values of the same type *Values are stored in adjacent memory locations * Declared using [] operator const int ISIZE = 5; int tests[ISIZE]; Array Storage in Memory The definition int tests[ISIZE]; // ISIZE is 5 allocates the following memory Array Terminology In the definition int tests[ISIZE]; * int is the data type of the array elements * tests is the name of the array * ISIZE , in [ISIZE] , is the size declarator . It shows the number of elements in the array. *The size of an array is the number of bytes allocated for it (number of elements) * (bytes needed for each element) Array Terminology Examples Examples: Assumes int uses 4 bytes and double uses 8 bytes const int ISIZE = 5, DSIZE = 10; int tests[ISIZE]; // holds 5 ints, array // occupies 20 bytes double volumes[DSIZE]; // holds 10 doubles
// array is 80 bytes 8.2 Accessing Array Elements *Each array element has a subscript , used to access the element. *Subscripts start at 0 Accessing Array Elements Array elements (accessed by array name and subscript) can be used as regular variables tests[0] = 79; cout << tests[0]; cin >> tests[1]; tests[4] = tests[0] + tests[1]; cout << tests; // illegal due to // missing subscript 8.3 Inputting and Displaying Array Contents cout and cin can be used to display values from and store values into an array const int ISIZE = 5; int tests[ISIZE]; // Define 5-elt. array cout << "Enter first test score "; cin >> tests[0]; Array Subscripts *Array subscript can be an integer constant, integer variable, or integer expression *Examples: Subscript is cin >> tests[3]; int constant cout << tests[i]; int variable cout << tests[i+j]; int expression Inputting and Displaying All Array Elements To access each element of an array *Use a loop *Let the loop control variable be the array subscript * A different array element will be referenced each time through the loop for (i = 0; i < 5; i++) cout << tests[i] << endl; Getting Array Data from a File const int ISIZE = 5, sales[ISIZE]; ifstream dataFile;
Show entire document
Sign up to view the entire interaction

Dear Student, I have reviewed your assignment thoroughly, based on your assignment details and current... View the full answer

### Why Join Course Hero?

Course Hero has all the homework and study help you need to succeed! We’ve got course-specific notes, study guides, and practice tests along with expert tutors.

### -

Educational Resources
• ### -

Study Documents

Find the best study resources around, tagged to your specific courses. Share your own to gain free Course Hero access.

Browse Documents