This lab serves multiple goals:
- To reinforce your understanding of arrays of different datatypes,
- To reinforce your understanding of the
Length
property of arrays, - To introduce you to more advanced array manipulations,
- To introduce you to simple algorithms that search for values in two arrays, and
- (Optional) to introduce you to simple algorithms that merge arrays.
Operations on Numeric Arrays
This first part will ask you to declare and initialize an array and then to display, sum, count occurrences, and retrieve information from this array. In the second part, we will assess whether your solution is “universal”, that is, whether it produces correct results with any array.
Start by declaring and initializing an int
array called numbers
:
Displaying Values
After declaring and initializing the numbers
array, write statements
that do the following:
- Display every value in order, left to right,
- Display every value at an even index (skip odd indices), and
- Display all values that are greater than 5.
Counting Values
Next, write statements that do the following:
- Calculate the sum of all numbers in
numbers
, then display the result. The expected answer is 102. - Count how many times
7
occurs innumbers
, then display that count. The expected answer is 3.
Finding Values
Next, write statements that do the following:
- Find the index of the first
7
, then display that index. The expected answer is 4, but your statements should be such that if the value is not found, it would display-1
. - Find the greatest value in
numbers
. The expected answer is 11.
Evaluate Your Solution: Is It Universal?
After implementing these methods, and assuming your program obtained the
expected answers, ideally the solution still works even if the values
in the numbers
array change, or even if the array length changes.
To test your program, go back to the beginning where you declared the
numbers
array, then change the initialization so that the new array
values are:
Then re-execute the program.
Check that you obtain the expected values:
- the sum should now be
644
- since
7
does not occur in the array anymore,- the count should be
0
- the first index of
7
should be-1
- the count should be
- maximum value is now
92
Working With Two Arrays
For this part, declare and initialize the following two char
arrays:
Next, write statements that answer these two questions:
- Does the value
'w'
occur in both arrays? - What is the first value of the array
chars1
that also occurs in the second arraychars2
, searching from left to right? If none is found, displayno match
.
After completing these two problems, make sure the program answers these questions correctly. The expected results are:
- Does
'w'
occur in both arrays →false
- First value that occurs in both arrays →
'U'
Again, evaluate your work by changing the array initializations to:
Then execute the program again. Ideally, the program does not crash and should still produce correct answers:
- Does
'w'
occur in both arrays →true
- First value that occurs in both arrays →
'P'
If the program does not produce these expected answers after changing the array values, review your program and try to determine how to write a solution that works for any two char arrays.
Pushing Further (Optional)
This short exercise will require you to manipulate two arrays at the same time to construct a third one.
Start with two integer arrays:
Then, implement statements to merge those left
and right
arrays by
creating a new, larger array that holds both of their values, in this
order:
Then, change the values in the left
and right
arrays, and make sure
that your program still creates the correct array.