Activity  9-3 - Searching Arrays


By now you should know how to write a C++ program to find the minimum and the maximum of a given set of numbers that are either read from a file or are entered from the keyboard.  In this case, you do not need to remember all the numbers at once but you can read the numbers one-by-one and compare each one with the temporary max or min and replace the temporary min and/or max with the number in hand, if needed, until the last number is read.

Let's practice some more the use of arrays....

Exercise 9.3
Write a program that reads a file of integers, fill up an array of 20 elements and searches for a number in the array. Call your program ex93.cpp.

 

Exercise 9.4

Suppose your were asked to write a C++ program to read 4 integer values from the keyboard then find the largest (max) and to display the list of all numbers on one column and their difference with the max on the second column. Call your program ex94.cpp.

Here is an example:

Input: 3 4 5 8
Output:
Max = 8

Num    Diff_from_Max
3            5
4            4
5            3
8            0

 
 

While I strongly enourage you to complete the next two sections of this lab, the rest of the Lab is optional and for available Extra Credit. If you are planning to stop here, be sure to scroll to the bottom of the last page of the post_lab to read the Instructions regarding the return of this Lab.

Congratulations you have completed another CS I Lab!