CSCE 2004 - Laboratory Assignment 8

    The objective of this laboratory is to gain experience in using arrays in C++. You will do this by declaring and initializing arrays, passing arrays into functions, and performing a variety of array manipulations. This lab has the following steps:

  1. Getting Started
  2. Before we start this lab, use the class website to check your grades in this class so far. Go to the "Grades" link and type in your UAID and website password. If you have "-1" grades for labs or homeworks, it means that the GTA has not graded this yet. If you have "0" grades, it means that the GTA graded this lab or homework, but you did not hand anything in. If you find any errors, please inform your GTA.

  3. Array Declarations
  4. Arrays are used to store a collection of data values. You must specify the "data type" of the array and declare it before you can initialize the array with values. The examples below declare and initialize two arrays of integers. The first array declares an integer array having ten values. The second declares an integer array of three values and initializes the array. Notice how the array initialization is combined with its declaration statement in the second example.

    // first example
    int array1[10];
    // second example 
    int array2[3] = {3,1,10};

    Using these examples, declare an array of floats that can store five values and initialize the array with your own values.

  5. Initializing Data in Arrays
  6. For small arrays, it is possible to manually type in initial values, but for larger arrays it is easier to use a loop to set the initial values. In order to get access to each of the values in an array, we use an "index". For example, we can initialize the values in array1 to contain even numbers from 0,2,4,6,8,10,12,14,16,18 as follows.

    for (int index = 0; index < 10; index++)
       array1[index] = index * 2;

    Using this example, write a loop to initialize your float array with the following 5 values 1.1, 2.2, 3.3, 4.4, 5.5. To do this, you will need to invent a formula that uses the value of index in some way to get these values.

  7. Processing Arrays in Functions
  8. Consider the "Initialize" function below. Its purpose is to initialize an array of integers to random values between 0 and 9. The first parameter is the array of integers. Notice that we have used [] here after the name of the parameter to indicate that it is an array of any size. The second parameter to the function contains the size of the array.

    In order to create random integer values, the Initialize function calls the "rand()" function, which is part of the "cstdlib" library. The main program calls "srand()" to seed the random number generator. To make sure we get a different seed every time we run the program, we call the "time()" function from the "ctime" library. This function returns the number of seconds it has been since 01/01/1970 (which is about when the language C was invented at Bell Labs).

    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    using namespace std;
    void Initialize(int Array[], const int Size)
       for (int Index=0; Index < Size; Index++)
          Array[Index] = rand() % 10;
          cout << Array[Index] << " ";
       cout << endl;
    int main()
       // Initialize random number generator
       // Declare array
       // Initialize array

    Step 1: Copy and paste the program above into "array.cpp". Modify the main program to declare an integer constant with value of 20, and an array of 20 integers. You can use any names you like.

    Step 2: Now call the Initialize function with the appropriate parameters to fill your array with random values. Make sure your parameters have the same data types and order as specified in the definition of Initialize.

    Step 3: Compile and debug your program, and run this program several times to see what values are printed out. Now change the array size to 30 and see what happens. Being able to process different sized arrays in one function is a useful feature of C++.

    Copy and paste your array.cpp program below.

    Copy and paste samples of your program output below.

  9. More Array Processing in Functions
  10. Consider the following function. Its purpose is to count the number of times a given data value occurs in an integer array.

    int CountValue(const int Array[], const int Size, const int Value)
       int Counter = 0;	
       for (int Index=0; Index < Size; Index++) 
          if (Array[Index] == Value) 
       return Counter;

    The rand() function when seeded with srand() is supposed to yield uniformly distributed random values. Don't you think it would be interesting to see just how random your array of values is?

    Step 1: Add the CountValue function to your program above, and call this function in the main program after Initialize to see how many times the number 7 occurs. Run the program several times to see what you get. Do you get the same count every time?

    Step 2: Now extend your main program to add a loop that calls CountValue to find out how many times each of the values [0..9] occurs in the array. Print these counts out the the screen as you calculate them.

    Step 3: To get a better statistical sample, increase the size of your array to 100 and run your program to see what you get. In theory, you should now have about 10 of each of these values. How uniform did it look?

    Copy and paste your extended array.cpp program below.

    Copy and paste samples of your program output below.

  11. Implementing New Array Operations
  12. Now that you have seen two example functions which operate on arrays, it is time to write your own function.

    Step 1: Write a new function called "Average" that loops over the data in an array to calculate the average=sum/count of the values in the array. Have your function return a floating point value. To do this, you can use type casting or you can use float variables to store sum and/or count.

    Step 2: Now call your "Average" function in the main program and print out the result for your array of 100 integers. Run your program several times to see what your average values look like.

    Step 3: Now modify your program to run this experiment using an array of 1000 values. You should see that the average does not change as much when you run the program several times. This illustrates one of the principles of statistics that is essential to experimental data analysis -- the more data you have, the better you can estimate the mean/variance of the data.

    Copy and paste your final array.cpp program below.

    Copy and paste samples of your program output below.

  13. Submit Work
  14. This lab assignment will be submitted electronically to the TAs once you fill in the fields below and click on the "submit" button. You do NOT need to print a copy of this document to hand in.

    Your UAID number:
    Your website PASSWORD: