IC210 Lab 8 - Arrays and Functions

Pre-lab homework: Read the lab description below (parts A thru D). Then write down the requested protoyptes (but for now, ONLY required for parts A and B).

Overview: Recently we have learned about both functions and arrays, which are both vital to making most practical programs. In this lab we will practice combining these two concepts.

Instructions

For most of this lab, you will be gradually writing a program that reads in a text file that consists of a bunch of integers, then does various processing on those numbers. Here are the files you need:

And here are some general guidelines:

Here are two sample runs for the completed program (NOTE: the user input always follow a request to "enter" something, like the filename or a number to search for):

screenshot screenshot

Part A: You will need a function readDataFromFile() that will open the file and read it's contents into an array, then send back to main() a pointer to the array AND the size of the array. For instance, I should be able to use it like this in main():

    int *data;
    int N;
    data = readDataFromFile("numbers1.txt", N);
    cout << "File read, it has " << N << " numbers." << endl;
Specific steps to follow:
  1. Examine the code above to see what the function above must be doing.
  2. Write down on paper what the prototype for the function should be (based on the code above).
  3. Now click here to see the correct prototype (and a descriptive comment). Compare to what you wrote. If it's not the same, think about why. The exact types here are important!! (so ask your instructor if you are confused at all).
  4. Write a definition for this function.
  5. Write main() so that it uses this function to produce the same results as shown in part A of the screenshots.
  6. Compile and test your program.
  7. Show your instructor when this works.

Part B: You will need a function computeSum() that will add up all the numbers in the array. For instance, I should be able to use it like this in main():

    int sum = computeSum(data, N);
Specific steps to follow:
  1. Based on the code above, write down a prototype for the function.
  2. AFTER writing down your answer, compare with the correct prototype. Again, the exact types are important, so ask if you don't understand why something is there OR not there in the prototype.
  3. Write the function definition, then expand main() to use the function to produce the same results as shown in part B of the screenshots.
  4. Compile and test your program.
  5. Show your instructor when this works.

Part C: You will need a function printSomeData() that will print part of your array. For instance, I should be able to use it like this in main():

    printSomeData(data, startIndex, stopIndex);
Specific steps to follow:
  1. Based on the code above (and the screenshots), write down a prototype for the function.
  2. AFTER writing down your answer, compare with the correct prototype. Again, the exact types are important, so ask if you don't understand why something is there OR not there in the prototype.
  3. Write the function definition, then expand main() to use the function to produce the same results as shown in part C of the screenshots.
  4. Compile and test your program.
  5. Show your instructor when this works.

Part D: You will need a function isNumberInData() that checks if a certain "key" number is in the array or not. For instance, I can use it to ask "is the number 25 in this array?" Specific steps to follow:

  1. This time we are not showing you what main() looks like when calling the function. But think about what this function must do (also consult part D of the screenshots). What should be the input(s) and output(s) of this function? Now write down what the prototype should be.
  2. AFTER writing down your answer, compare with the correct prototype. Again, the exact types are important, so ask if you don't understand why something is there OR not there in the prototype.
  3. Write the function definition, then expand main() to use the function to produce the same results as shown in part D of the screenshots.
  4. Compile and test your program.
  5. Show your instructor when this works.

Part E: You will need a function computeHistogram() that will count how many times each number appears in the array (How many times does 7 appear? How many times does 8 appear?). It will store its results in a NEW array that we'll call histogram[] where, for instance, histogram[7] records the number of times that a 7 appears in the original array. Specifically, I should be able to use it like this in main():

    int* histogram;
    int maxNumber=26;
    histogram = computeHistogram(data, N, maxNumber);
Specific steps to follow:
  1. Based on the code above (and the screenshots), write down a prototype for the function.
  2. AFTER writing down your answer, compare with the correct prototype. Again, the exact types are important, so ask if you don't understand why something is there OR not there in the prototype.
  3. Write the function definition, then expand main() to use the function to produce the same results as shown in part E of the screenshots.
  4. Compile and test your program.
  5. Show your instructor when this works.

Part F: Look back at how you created the new histogram array. Students new to arrays often find this confusing or novel or surprising. Why? Well, so far we have mostly written code where we refer to all array elements in order (e.g., print out the values of array[i] where i changes from 0 to 99, using a loop, for an array of size 100). It's important to note that we do NOT have to access an array in order, and that sometimes the index into an array could be based on something entirely different. For instance, we might access data[index] where index is obtained by asking the user, by adding some numbers together, by reading in a number from a file, or by some other computation. Take a moment to look again at your solution to computing a histogram -- just recognizing the different kinds of indexing we can use is important! There is no code to submit for this part.

Going Beyond

If time permits, also work on these problems.

First, change your program so that it computes a histogram for all numbers in the range [0..32]. We won't have any numbers bigger than 26, but using a power of 2 here (32) simplifies things.

Next, write a function with the following description and prototype:

// This function takes an array 'data', with valid indices in range 0..maxNumber (inclusive)
// maxNumber must be even
// It returns a new array where
//   index 0 is the same
//   index 1 holds the sum of index 1 and 2 in the original array
//   index 2 holds the sum of index 3 and 4 in the original array
//   index 3 holds the sum of index 5 and 6 in the original array
//  and so forth.
// So if maxNumber is originally 8 (total size 9 in array), then
// the new array will have maxNumber of 4 (total size 5 in array)
int* smushArray(int *data, int &maxNumber);
The idea is to modify main() so that it can use this function to gradually "smush together" the histogram, so that after each "smush" the array has only half as many interesting entries. Here's are screenshots that show what this should look like when run on numbers1.txt (note: because index 0 is uninteresting, we don't bother printing it after the smush):

(start of screenshot) screenshot (screenshot continue, with some overlap from before)screenshot

Valid XHTML 1.1! This page last modified