## Array exercises

1. Write a program that searches an array. For this you will use an inline initialization of an array (we'll do this together); then you will ask the user for a target value. Then you will search for this value in the array, and you will print ALL positions where you find it. The output should look as follows:

• (a) First, make your output as below:
```Here is the array you'll be woking with: 3 2 5 2 7 4 3 5

Enter target value: 3
Target 3 occurs in the list at position: 0
Target 3 occurs in the list at position: 6
Goodbye.
```
or
```Here is the array you'll be woking with: 3 2 5 2 7 4 3 5

Enter target value: 8
Goodbye.
```

• (b) Modify your solution above so that it keeps track whether the target is found or not, in order to be able to print a "not found" message at the end (If the target is found, nothing changes).
```Here is the array you'll be woking with: 3 2 5 2 7 4 3 5

Enter target value: 8
Target 8 is not in the list.
Goodbye.
```
Hint: One way to do it is to use a variable that counts the number of times the target is found. Another way is to use a boolean variable that keeps track whether the target has been found or not; maintain this variable through the search loop.
```
//this variable keeps track whether the target has been found or not
boolean isFound;

isFound = false;

//search loop
while (..) {

//if we find target set isFound to true
}
//at the end of the loop isFound should be true if and only if
//the target has been found at least once

if (isFound == false) {
//tell the user the target is not in the list
}
```

• (c) Refine your solution further so that it displays the results as below:
```Here is the array you'll be woking with: 3 2 5 2 7 4 3 5

Enter target value: 3
Target 3 occurs in the list at position: 0 6
Goodbye.
```
or
```Here is the array you'll be woking with: 3 2 5 2 7 4 3 5

Enter target value: 8
Target 8 is not in the list.
Goodbye.
```
Hint: use your boolean variable isFound from part (b). Based on it you can figure out if it is the first time you find the target, or not.
```
//this variable keeps track whether the target has been found or not
boolean isFound;

isFound = false;

//search loop
while (..) {
//compare target to element i
//if we find target then
//if its the first time we find it then
//print "The target occurs at position: "
//set isFound to true
//print position
//increment loop counter
}
//at the end of the loop isFound should be true if and only if
//the target has been found at least once

if (isFound == false) {
//tell the user the target is not in the list
}
```

2. Modify the search program so that it repeatedly asks the user whether she wants to search the array. If the user says yes, it then asks for a target, and prints all positions where it finds the target, as above. If the user says no, then it terminates.
```Here is the array you'll be woking with: 3 2 5 2 7 4 3 5

Do you want to search? (Press y for yes, n for n): y
Enter target value: 3
Target 3 occurs in the list at position 0 6

Do you want to search? (Press y for yes, n for n): y
Enter target value: 5
Target 5 occurs in the list at position 2 7

Do you want to search? (Press y for yes, n for n): y
Enter target value: 9
Target 9 is not in the list

Do you want to search? (Press y for yes, n for n): n
Goodbye.
```

3. Write a program that reads an array from the user, and then computes and prints out the largest number in the array, and its position.
```What size will your array be? 5
Enter element 0:  2
Enter element 1:  8
Enter element 2:  5
Enter element 3:  4
Enter element 4:  -1
Thank you.
The largest number is 8 at position 1.
Goodbye.
```
If the numbers in the list (array) are not unique, the largest number could occur more than once. Would your algorithm find the first occurence? The last? Explain.