CIS 270 Lab 4

Points: 30 Due Date: Tuesday August 26, 2003

A common problem with data is that some of the values maybe outside the useful range. To handle this data, we need to change it so it is in the range. This is known as clipping. I want you to take an array of numbers and clip them. The array itself can be defined outside of main() as a global array. Then you need to fill it with random numbers. But how do you do that? Like this.

#include "stdafx.h"
#include <stdlib.h>
#include <time.h>
#include <iostream>
using namespace std;

const int DSIZE = 10;
int data[DSIZE];

int main(int argc, char* argv[])
{
	srand( (unsigned)time( NULL ) );

	for(int i = 0; i< DSIZE; i++)
		data[i] = rand() % 100;
	for(int j = 0; j< 10; j++)
		cout << " data[" << j << "] = " << data[j] << endl;
	return 0;
}
This little program will fill an array with numbers between 0 and 99. This sample above will tell you what include statements you need. The srand line just causes the rand() function to generate different numbers each time you run it. After loading the data, you should print the array.

Once you have generated the numbers, you need to check each one. For this you will need a function. This function will take three arguments. The first one is an array element from the data array. The main will have to loop over the array, passing each element into the function. The second argument is an integer and is the high value for clipping. The third argument is an integer and is the low value for clipping. It checks the first value and if it is higher than the high value, the first argument is changed to the high value. If it is less than the low value, it is changed to the low value. Remember how we used reference parameters to change arguments. It should return the amount that was clipped. Here is a table to show some examples.

First ArgHigh ValueLow ValueChanged ToReturned
506010500
9060106030
56010105
606010600

After checking and possible changing the array, the program should print the array. While checking, it should calculate the average amount of clipping, using the values returned by the clipping function.

Note: The clipping function MUST NOT have array references in the body of the function. Only main() is allowed to use them.

I have written a solution to this and it is available here. You should be able to just download and run this to see an example of what the the results should look like.

Deliverables:

You should turn in a listing of the program. Staple the cover sheet to that.

Notes

Think about how you will do this so you can ask questions.
ASK if you have any questions.