CSIS 10A Assignment 10Select 10 Points from below
Read: Hennefeld, Chapter 13 Arrays and Chapter 14 Sort
Item / PointsProgramming Problems
Download and complete file Lab10Arrays.cpp /
10
Lab10 Arrays
In this exercise we will practice working with arrays and sorting.
Activate each problem by adding a / before /*------Problem ------
When you finish a problem, remove the / you added and go to the next one.
1. Declare a) a 10 element array of float called stuff, and (on same line)
b) add initialization list to set up stuff with the following data:
{1, 5, 2, 9, 8, 4, 0, 3}
Expected Result (to test you must UNCOMMENT lines 22,23) :
1a: Size of stuff is 10
1b: Stuff
Index: 0 1 2 3 4 5 6 7 8 9
Value: 1 5 2 9 8 4 0 3 0 0
- Declare a) a 5-element array of char called word and (on same line)
b) initialize word with the following data:
word initiallized to {'W', 'a', 'i', 't'} or "Wait"
Expected Result:
a) 2a: Size of word is 5
b) 2b: Word:
Index: 0 1 23 4
Value:Wait
3. There is a 6 element array of int called data, already set to [0 1 2 3 4 5]. Using array assignment, a) Put a 10 in the first position of the array. b) put a 27 in the last position of the array. c) display the contents of position 2 of the array. d) then input a value from keyboard and store in cell number 4 (actually, it is the 5th cell starting from 0!).
Expected Result user input in bold:
3c: Position 2 contains: 2
3d: Input value for position 4: 7 user input
Data
Index:012345
Value:10123727
(7 is the value you entered for step 3 d)
4. Suppose you have a 15 element array of float called samples. And an int variable called index, which currently = 3; a) Put a 10 in the samples array at position index. b) put an 11 in the array at the position immediately following index (use index+1 in square brackets). c) put a 7 in the array at two positions before index ( use index-2 in square brackets).
Expected Result: (only displays first 5 elements of samples array)
4: Samples:
Index: 0 1 2 3 4
Value: 0 7 0 10 11
5. Using the same array from problem 4, make a for loop to fill all 15 slots of "samples" array with 10, 11, 12, 13, 14, ... 24 (Use For Loop). Expected Result:
5: Samples:
Index: 0 1 2 3 4 5 6 7 8 9
Value: 10 11 12 13 14 15 16 17 18 19 and so on
Problems 6, 7, and 8 lead you through the development of sorting data. They use the same variables. Leave each one active as you go through these.
- This problem creates an array called shuffled. When it is displayed, you can see its data is out of order. Using only the variables defined in 6 (use k for your counter), make a for loop that finds the smallest value and the corresponding position or cell number (stored in small_pos), in the shuffled array.
- Swap the value in the smallest slot with the value in slot 0 in the shuffled array (use swap function). This is what the sort algorithm does over and over again.
8. Sort the shuffled array (use sort function). Check that it is sorted when it is displayed again.
9. Comment out problems 6, 7, and 8. This problem will prepare you for Project2 part2, when you need to show the last five transactions of a customer at the ATM. In this case, you will show the last five data values in a simpler data file called scores.txt. In general, you want to pretend you don’t know the size of the data file, so you can declare your array to hold 100 values ( the actual number of data values in the file is smaller). Read the data into the array using an end of file loop …something like:
while(infile>score[k]) // read from file and store into cell k of array
k++; // add one to counter if read was successful
After the loop ends, keep track of the size of the array by setting size = k; Then reuse k as the counter, and size as a reference in a for loop that just shows the last five cells of the array.
10. Read file scores.txt into an array, display it, sort it, and display it again
11. This relates to Project2 part2 delete_account. A) load scores.txt into an array, B) display the array, C) let the user enter one of the values displayed (for example, 85), and D) write the array back to scores.txt with all occurences of that value deleted. Now there are no 85s in scores.txt