I have a programming assignment and I need a little help with getitng it started
ID: 675508 • Letter: I
Question
I have a programming assignment and I need a little help with getitng it started. The following are the instructions:
Recall the M&M exercise that we did in the first week of class where everyone guessed the number of M&Ms in the jar. In addition to finding the maximum, minimum, and average values guessed, it is also interesting to see the distribution of the guessed values, In this assignment, you are asked to write a program that will print a histogram of the values submitted. Please name your program "histogram.c". Your input file will be named "input.txt". Your output file should be named "output.txt".
As with the previous homework, a list of everyone's guesses has been stored in a file called "input.txt". The data in this file is organized as follows:
(1) The first line contains an integer indicating the number of guesses in the file.
(2) Each additional line contains a single guess.
A histogram is a bar chart in which each bar represents a "bin" and the length of the bar indicates how many elements are in each "bin". For this program, each bin will store the count for ten numbers; that is, bin #0 will store the number of guesses in the range of 0 to 9 (inclusive), bin #1 will store the number of guesses in the range of 10 to 19, bin #2 will store the number of guesses in the range of 20-29, etc. You may assume that all guesses will be less than 100 which means that your histogram will have 10 bins.
For example, suppose we are given the following 34 guesses:
64, 28, 56, 26, 21, 65, 34, 66, 64, 37, 54, 79, 51, 33, 76, 25, 22, 76, 72, 63, 67, 15, 17, 23, 14, 37, 55, 32, 24, 63, 74, 65, 62, 7
Our bins would record the following values:
Bin counting values from 0-9 1 (due to the 7)
Bin counting values from 10-19 3 (due to the 15, 17, 14)
Bin counting values from 20-29 7 (due to the 28, 26, 21, 25, 22, 23, 24)
Bin counting values from 30-39 5 (due to the 34, 37, 33, 37, 32)
Bin counting values from 40-49 0 (due to no values in this range)
Bin counting values from 50-59 4 (due to the 56, 54, 51, 55)
Bin counting values from 60-69 9 (due to the 64, 65, 66, 64, 63, 67, 63, 65, 62)
Bin counting values from 70-79 5 (due to the 79, 76, 76, 72, 74)
Bin counting values from 80-89 0 (due to no values in this range)
Bin counting values from 90-99 0 (due to no values in this range)
The histogram that your program will print into the file "output.txt" will look as follows.
Values Histogram
0-9 *
10-19 ***
20-29 *******
30-39 *****
40-49
50-59 ****
60-69 *********
70-79 ****
Please note that leading and following empty bins are not printed, but empty bins that are flanked by non-empty bins are printed. Thus, not all bins of the histogram will be printed. Your program will need to determine the first and last bin to be printed in the histogram.
You may want to create additional test input files of your own with which to test that your program is behaving correctly.
Any help would be awesome! Thank you!
Explanation / Answer
#include <stdio.h>
int main()
{
FILE *inputFP, *outputFP;
int i, j, start, end, numOfGuesses, guessValue, guessesCount[10];
inputFP = fopen("input.txt", "r");
outputFP = fopen("output.txt", "w");
fscanf(inputFP, "%i", &numOfGuesses);
for(i = 0; i < 10; i++)
guessesCount[i] = 0;
for(i = 0; i < numOfGuesses; i++)
{
fscanf(inputFP, "%i", &guessValue);
guessesCount[guessValue / 10]++;
}
for(start = 0 ; guessesCount[start] == 0 && start < 10; start++);
for(end = 9; guessesCount[end] == 0 && end > -1; end-- );
for(i = start; i <= end; i++)
{
fprintf(outputFP, "BIN #%i: ",i);
for(j = 1; j <= guessesCount[i]; j++)
fprintf(outputFP, "*");
fprintf(outputFP, " ");
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.