CSCE 2004 - Laboratory Assignment 4

The objective of this laboratory is to gain experience in using arrays in C++ by familiarizing yourself with declaring arrays, array manipulations and passing arrays to functions. It has the following steps:

  1. Array declarations:
  2. 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.

    int exampleArray[10];

    int exampleArray[3] = {3,1,10};

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

    (You may place this declaration in comments at the top of Part 2's program.)

  3. Initializing Data in Arrays
  4. Consider the following function. Its purpose is to initialize an array of integers to random values between 0 and 9. You need to include 2 new libraries: cstdlib is the C language's standard library (needed to get the random number generator functions) and ctime is the C language's time library (needed to get the current time to use as a seed for the random number generator).

    #include <iostream>
    #include <cstdlib>
    #include <ctime>

    using namespace std;

    void initialize(int array1[20])
    for (int i = 0; i < 20; i++)
    array1[i] = rand()%10;

    Write a short C++ program which calls this function to initialize an array of 20 integers. Remember to declare your array in main() before passing it to the function. Print the values in the array you have passed to this function from main() after you have called the above function.

  5. Manipulating Data in Arrays
  6. Consider the following function. Its purpose is to count the number of times a data value occurs in an integer array.

    int countValue(const int array1[], const int value, const int size)
    int counter = 0;

    for (int i = 0; i < size; i++)
    if (array1[i] == 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 20 values is? Add a loop to your main() which calls countValue() to print out how many times each of the values [0..9] occurs in your array and outputs it to the screen. How uniform did it look?

  7. Implementing New Operations
  8. Now that you have seen two example functions which operate on arrays, it is time to write your own function. Write a new function which returns the "average" of the values in an array. Have your function return a floating point value. Output this value from main().
    NOTE: You will need to do a cast operation before finding the average in your function.

    Add this function call to your main program and see what the average of 20 random values between [0..9] is. Run your program several times. How uniform does this average look?

  9. Submit Work
  10. To submit your lab work, have your instructor check over any code/output that you've done during this lab session and record that you completed the lab assignment.

  11. Logout of the System

    After making sure that you submitted the code correctly and you have done the entire assignment, logout of the system by clicking on the menu button in the lower left hand side of the desktop. Then, click on Log Off. Wait a few seconds for a window to pop up and then click Log Off.