Question: C language in Linux Computing a histogram For this lab you will be computing a histogram. Specifically, you will be loading in a set of
C language in Linux
Computing a histogram
For this lab you will be computing a histogram. Specifically, you will be loading in a set of quiz scores from a file, counting the frequency each number occurs, sorting the results, and finally displaying them. I will be describing a very specific way below that I want this done. Obviously there are many different ways some of these things could be done, but I want you to follow my function signatures for this lab. I am giving you the specific function names. But I am not giving you the full signatures. You will need to come up with what the signatures need to be by reading what I say about the functions. If you are unclear as to what the parameters need to be make sure you ask me if what you have is correct or not. This will be a bit confusing at the start, but having to work through exactly what it is you will need to pass in terms of their types, etc is part of the learning for this lab.
As a restriction for this lab, you are also to not use any global variables. That is all variables must be declared inside a function. You should also place your functions below the main, meaning that you will need function prototypes in order for it to compile.
Reading from a stdin
You should create a function called readScores. This function should return nothing (void). When reading in numbers you don't know how many numbers you are going to have. But arrays need to be created with a fixed size. So you can assume for this lab that there are no more than 100 numbers which will be read in. That means in turn you will also need a variable to hold the actual number stored in the array. Thus, two pieces of information need to be "passed back" from the readScores function - the array of numbers and the count of how many are read in. I already stated that I wanted the function to return void. Thus, you will need to "pass back" this information through the parameters. This can be done by creating the array and count integers in the main and passing them in as pointers so changes made to them in the function will be reflected in the main.
In order to read in from stdin you should use the scanf function. It works almost the same as the printf function. The first parameter (i.e. "%d") will specify what type of number to read in. And the other parameters are the pointers to where to store that information. scanf will also return a value. If you try to read in the end of the file it will return a special result called EOF. So you can check to see if what it returns is == or != to EOF in order to stop your loop. You do need to have a loop since you don't know how many you will be reading in. For development purposes, it might be useful to just do a scanf or two and make sure you can read in the first couple of values. After that, you can read in a fixed exact number you are expecting. Then finally you can generalize it to read in until it hits EOF.
Lastly, scanf as I have described it will let you type at the keyboard to enter numbers. Instead I want you to create a file of numbers and redirect input using < on the command line when you run the program in order to have it grab the numbers from the file instead. The file I want you to use for testing is the following:
10 10 8 7 5 4 10 2 2 8 7 7
Displaying the file results
After reading in the numbers your main should then call the function displayScores which should simply display the results to the screen. This should produce the following results for the test file:
score 0: 10 score 1: 10 score 2: 8 score 3: 7 score4: 5 score 5: 4 score6: 10 score7: 2 score 8: 2 score 9: 8 score 10: 7 score 11: 7
Calculating the histogram frequencies
A histogram is simply a count of how frequently each number occurs. So if 10 occurs 3 times then you will need to remember that. For this section I want you to create a structure that binds the number and the frequency together as a pair. Call this structure struct freq. You will need an array of this structures to represent your histogram. The same issues apply to this calcHistogram function as they did to the readScores function. You will need to "pass back" both the array of histograms as well as the count of how many distinct entries there are in the histogram array. Note that in the worst case, each value occurs once so that will be a good limit on the max size of the histogram array. But since you need to "pass back" two values I am once again going to have you pass them back as parameters via pointers. The calcHistogram method should itself return void.
Displaying the histogram frequencies
This function is similar to the displayScores. It should be called after calculating the histograms and should be called displayHistogram. The results of this for the test file should look like:
value 10: freq 3 value 8: freq 2 value 7: freq 3 value 5: freq 1 value 4: freq 1 value 2: freq 2
Sorting the histogram frequencies
Next after calculating the histogram and displaying it, you should call a function called sortHistogram. This should modify the histogram array it is given to be in order by frequency. You all have written some type of sort before so any sort is acceptable. I simply used a selection sort. After sorting the main should then call the displayHistogram method again to see the sorted results for the test file:
value 10: freq 3 value 7: freq 3 value 8: freq 2 value 2: freq 2 value 4: freq 1 value 5: freq 1
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
