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:

  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.