In the file named histogram-broken.cc, you’ll find code to produce a histogram f
ID: 3665672 • Letter: I
Question
In the file named histogram-broken.cc, you’ll find code to produce a histogram from a user specified list of integers.
//histogram-broken.cc
Here is an example of the proper usage:
UNIX$ ./histogram
How many data values? 10 Enter integer data 1 2 3 4 5 4 3 2 3 4
----------- Histogram ----------------
1: * 2: ** 3: *** 4: *** 5: *
The Histogram is supposed to print one * for each occurrence of the value in the input. Histograms are useful for visualizing the distribution of data. For example, from this histogram, we can see that 3 and 4 were the most common values in the input (there were three of each).
Unfortunately, the C++ file does not have a design document, and the implementation itself has many errors, as if written by a novice programmer who didn’t take the time to design the algorithm carefully, and so did not quite have enough time to finish.
All the given code in the main function is correct, so you should not modify the given code in the main function.
Instead, modify the other functions to fix them, or add additional tests to main. Your task is as follows:
1. Study the functions carefully. While there are some errors, there are parts of the functions that are very close to working. The purpose of this question is not to get you to invent anything tricky, but to get you to practice debugging.
2. Figure out what each part of the functions is supposed to do. The programmer left no helpful comments to assist your work. Sorry.
3. Add algorithm headers as comments to the code, indicating pre/post/return for every function. You’ll have to figure these out from the code, and maybe change them as you discover errors.
4. Fix the obvious errors as you see them. 5
5. Try to compile the program, and use the compiler’s error messages and warnings to track down bugs.
6. Add additional code to main() to test each of the functions. We recommend that you test the functions one at a time.
Don’t try to solve all the bugs all at once! Leave all your tests active all the time. Yes, it creates messy output. But there are bugs that you’ll fix that will cause new bugs in functions you thought were working. You need to see when this happens! Note: all of the given code in main should work as-is. So, you should add additional tests to main, but you shouldn’t have to correct the code already in main.
7. Fix the bugs, and get the implementation working.
//here is the histogram-broken.cc
#include <iostream>
using namespace std;
void getData (int size, int Arr){
cout << " Enter integer data one line at a time " << endl ;
for (int i=0; i < size; i++){
cin >> Arr[i];
}
}
void findMinAndMax(int array[], int size, int *min, int *max)
{
int smallest = array[0];
int largest = array[0];
min = &smallest;
max = &largest;
for (int i = 1; i < size; i++)
{
if (array[i] > largest){
&largest = array[i];
}
if (array[i] < smallest){
&smallest = array[i];
}
}
}
int *makeFrequency (int data[], int dSize, int *minDataValue, int *maxDataValue)
{
findMinAndMax(data, dSize, &minDataValue, &maxDataValue);
int fSize = minDataValue + maxDataValue;
int frequency = new int[fSize];
if (frequency == NULL)
{
return NULL;
}
for (int i = 0; i <= fSize; i++)
{
frequency[i] = 0;
}
for (int i = 0; i <= dSize; i++)
{
int j = data[i] - (minDataValue)+1;
frequency[j] = frequency[j] + 1;
}
return frequency;
}
void makeHistogram ( int *freq, int min, int max ){
cout << " ----------- Histogram ---------------- " << endl;
int size = min + max;
for (int i = 0; i <= size; i++){
if (freq[i] > 0){
cout << " " << min + i << ": ";
for (int j = 0; j < freq[i]; j++) {
cout << '*';
}
}
}
cout << endl << endl;
}
int main() {
int dSize;
int *ArrayOfInts;
cout << "How many data values? ";
cin >> dSize;
ArrayOfInts = new int [dSize];
getData (dSize, ArrayOfInts);
int *frequency,min,max;
frequency = makeFrequency (ArrayOfInts, dSize, &min, &max);
if (frequency == NULL) return -1;
makeHistogram (frequency, min, max);
delete [] frequency;
return 0;
}
Explanation / Answer
Program modified and solved errors.. and comments added
#include <iostream>
using namespace std;
//reading data from user
void getData (int size, int *Arr){
cout << " Enter integer data one line at a time " << endl ;
for (int i=0; i < size; i++){
cin >> Arr[i];
}
}
//method to find min and max of given array
void findMinAndMax(int array[], int size, int *min, int *max)
{
int smallest = array[0];
int largest = array[0];
min = &smallest;
max = &largest;
//getting min and max
for (int i = 1; i < size; i++)
{
if (array[i] > largest){
largest = array[i];
}
if (array[i] < smallest){
smallest = array[i];
}
}
}
//getting frequency of each value from array
void makeFrequency (int *frequency, int *data, int dSize, int *minDataValue, int *maxDataValue)
{
//getting min and max
findMinAndMax(data, dSize, minDataValue, maxDataValue);
for (int i = 0; i <= dSize; i++)
{
frequency[i] = 0;
}
for (int i = 0; i <= dSize; i++)
{
int j = data[i] - (minDataValue)+1;
frequency[j] = frequency[j] + 1;
}
}
//printng histogram
void makeHistogram ( int *freq, int *min, int *max ){
cout << " ----------- Histogram ---------------- " << endl;
int size = min + max;
for (int i = 0; i <= size; i++){
if (freq[i] > 0){
cout << " " << min + i << ": ";
for (int j = 0; j < freq[i]; j++) {
cout << '*';
}
}
}
cout << endl << endl;
}
int main() {
int dSize;
//reading size from user
cout << "How many data values? ";
cin >> dSize;
//arra declared
int ArrayOfInts[dSize];
//reading data from user
getData (dSize, ArrayOfInts);
int min=0,max=0;
int frequency[20];
//getting frequenct .. calling method
makeFrequency (frequency,ArrayOfInts,dSize,min, max);
//printing histogram method
makeHistogram (frequency, min, max);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.