Lab 7

Part 1

Page 250 of your C book has a program that reports the smallest and largest elements in an array. Implement it (you can type it from page 250 if you want, or you can copy it from Dr. King's website). Compile it, and run it. Then describe the "max_min" function: what does it do line-by-line, how does it work? (You can type this in a file, then use "cat" to display it on the log).

If you were to change a value in the array after the call to max_min(), would it affect the results? Why or why not? That is, suppose we add the line

b[0] = 4;

right above the line that reads

printf("Largest: %d\n", big);

What affect would we see? This will require a bit of testing on your part, to confirm your reasoning. Also, what if we instead put

a[0] = 4;

right above the line that reads

printf("Largest: %d\n", big);

What would happen and why?

Part 2

Next, make a copy of it to a file called "maxmin_N.c", which will do the same thing, with one improvement: it should allow any number of integers up to the length N. Thus, if you give it the numbers 1, 2, 3, it should work (example: echo "1 2 3" | ./maxmin_N). This means that you will need to alter the part with the "scanf" function, to check the value returned from it, like we did in an earlier assignment. Also, make N a much larger value: 200 + the number of letters in your first name + the number from your GSU e-mail. Add a comment with this information, so that anyone who sees it can verify that the N value is correct.

Part 3

Now suppose that instead of the maximum and minimum values, you want to find the value closest to one entered. To accomplish this, have your program prompt the user to enter a number to find, then accept the list of numbers up to EOF. Thus, the first number is the one to find the closest value of, and should not be stored as part of the array. As before, you should not expect the list of numbers to be sorted. (Use pointers to accomplish this rather than a sorted array.)

For example, if the first value is 5, and the list is 1,3,5,7,9,11, then it should output 5 since it's on the list (and therefore, the closest). If the first value is 22, and the numbers are 101, 6, 99, 2, it should output 6, since that's the closest number to 22 in the list. What if there are two numbers equally close? When the first value is 6, and the list is 1,3,5,7,9,11, it should output both 5 and 7 as the closest to 6.

Make a copy of "maxmin_N.c", call it something like "closest.c", and change the "max_min" function accordingly. Since this has a different goal than "max_min", you should give it a different name. Like all variables and function names, choose it well.

Turning this in

Turn this in like the previous labs.

Make sure you use proper inline comments while writing solution code. Comments should to be succinct (short and useful).

Make an appointment to demo this to your TA. Appointments should be either during their office hours, or at a time that you arrange with your TA in advance. Your code must be turned in before the appointment.