Array Exercises - Towson University

advertisement
Array practice
1.
Write a program that inputs three test scores and computes the average.
2.
Write a program that inputs test scores until -1 and computes the average
3.
4.
Can you print all the test scores now?
Can you find the highest?
5.
To save test scores, use an array. Declare an array of up to 100 test scores.
6.
Write code to have the user enter all the test scores
7.
Write code to print all the test scores
8.
Write code to print all the test scores in reverse.
9.
Write code to increment all the test scores
10. Write code to compute the average test score.
11. Write code to find the highest test score.
12. Rewrite number 6 to read from a file, assuming there are 100 test scores.
13. Rewrite the previous problem, this time you can't assume there are 100.
14. Print the test scores now.
15. Rewrite each as a method.
Parallel arrays
16. Create an array of at most 100 cities.
17. Create an array of the average temp for each city.
18. Write the code to input 100 values into each array.
19. Write a method to find the highest temperature, return the index and print the corresponding
city.
20. Write a method to search for a city, return the index and print the corresponding temperature.
Array Exercises:
Declare an array: type[] name = new type [size];
Write array declarations for:
1.
2.
3.
4.
A 24 element double array named junk
A 500 element integer array named things
A 20 element char array named firstName
A one dimensional array called quizAvg of size CLASS_SIZE (where CLASS_SIZE is
50) and each component contains a double quiz average
5. A one dimensional array named quizAnswer that contains 12 components indexed by the
integers 0 through 11. The component type is boolean
6. A five-element one dimensional int array named oddNums and initialize it in the
declaration to contain the first five odd intgers
Each element of the array is treated exactly like a variable. Use an integer number of variable as
a subscript or index to access the element.
1.
2.
3.
4.
5.
6.
Assign a 0 to the first element of the junk array
Assign a -5 to the last element of the things array
Assign an ‘A’ in the first component of the firstName array
Assign a 95.5 to the second element of quizAvg
Initialize the quizAnswer Array to all trues in a declaration
Print out the first 2 elements of the oddNums array
To access an entire array, use a counted loop:
1.
2.
3.
4.
5.
6.
Initialize junk to all -1
Input from the user all the values for thing
Input from the user the string for firstName
Print all the values of quizAvg
Initialize the quizAnswer Array to all false using a for loop
Sum all the values of the oddNums array
Two dimensional arrays:
4
7
6
1.
Create a two-dimensional array for a 2 x 3 matrix.
2.
Write the code to input values for the array.
3.
Write the code to print the array
4.
Create a 2d array named stuff that stores the following values.
6
9
9
2
4
3
5
8
7
5.
Find the average value
6.
What is the value of stuff.length?
7.
What is the value of stuff[0].length?
8.
Find the number of values above 3.
9.
Print column 2 only using a loop.
Download