Ok so I got this to compile but now all it prints out to the screen is the "*" in my createHistogram function why doesnt it print the mean,median and mode with a histogram of the data?
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>
using namespace std;
// global constant declaration
const int MAX_SIZE = 50;
const int NUM_RANGE = 25;
int getNumbers( ofstream& report, int numbers[], int size, int range);
//void printResults( float mean, float median, float mode );
void createFrequency(int scores [] , int size, int frequency[], int range);
void createHistogram(int frequency[], int range);
//float calcMean(ofstream& report, int numbers, int size );
float calcMean(ofstream& report, int numbers[], int size );
void sort(int list[], int last);
void bubbleUp(int list[], int current, int last);
float calcMedian(ofstream& report, int list[], int size);
float calcMode(ofstream& report, int freq[], int size);
void printResults(float mean, float median, float mode);
int main()
{
float mean = 0;
float median = 0;
float mode= 0;
int frequency[NUM_RANGE+1];
int numbers[ MAX_SIZE ], size;
int list[50];
int scores[50];
int last;
int current;
ifstream inFile;
ofstream report;
report.open("scores.txt");
if (!report)
{
cerr <<"\aerror opening output file\n";
exit(102);
}
size= getNumbers(report,numbers,MAX_SIZE,NUM_RANGE);
mean = calcMean(report,numbers,size);
createFrequency(scores,size,frequency,NUM_RANGE);
createHistogram(frequency, NUM_RANGE);
sort ( list, size);
bubbleUp( list, current, last);
median = calcMedian(report, list, size);
mode= calcMode(report, frequency, NUM_RANGE+1);
printResults(mean, median, mode );
report.close();
system("Pause");
return 0;
}
//===============getNumbers========================
int getNumbers( ofstream& report,int numbers[ ], int size ,int NUM_RANGE)
{
ifstream inFile;
inFile.open( "scores.txt" );
if ( !inFile )
{
cerr << "scores.txt cannot be opened" << endl;
exit( 100 );
}
ofstream OutFile;
OutFile.open("Reordersheet.txt");
if(!OutFile)
{
cerr<<"\a Error opening Reordersheet\n";
system("Pause");
exit(102);
}//if open fails
int dataIn;
int i = 0;
while (i < size && (inFile >> dataIn))
if (dataIn >= 0 && dataIn <= NUM_RANGE)
numbers[i++] = dataIn;
else
cout << "Data point " << dataIn
<< " invalid. Ignored. \n";
// Test to see what stopped while
if (i == size && (inFile>> dataIn))
// More data in file
cout << "\nToo much data. Process what read.\n";
while ( ( size < MAX_SIZE ) && ( inFile >> numbers[ size ] ) )
{
++size;
}
inFile.close( );
return i;
}//end of getData
//===================Mean=======================
// finds the average of the numbers
float calcMean(ofstream &report, int numbers[ ], int size)
{
float total=0;
float mean;
for (int i=0;i<size;i++)
total= total+ numbers[i ];
mean=total/size;
return mean;
}//End of mean
//===============createFrequency========================
//Creates the frequency of scores
void createFrequency(int numbers [] , int size, int frequency[],int range)
{
for(int i = 0;i <range;i++)
frequency [i]=0;
for (int i = 0; i < size; i++)
frequency [numbers [i]]++;
return;
}//end createFrequency
//===================createHistogram=======================
//Creates Histogram of scores
void createHistogram(int frequency[], int range)
{
for(int i = 0;i<=range;i++)
{
cout<<setw(3)<<i<<setw(3)<<frequency[i];
for(int j = 1;j<=frequency[i];j++)
{
cout<<"*";
cout<<endl;
}
}
return;
}//end createHistogram
//==========================calcMode==============================
float calcMode(ofstream& report, int frequency[], int size)
{
int largest=0, mode;
for(int i = 0; i < size; i++)
{
if(frequency[i] > largest)
{
largest = frequency[i];
}
}
for(int i = 0; i < size; i++)
{
if(frequency[i] == largest)
{
mode = i;
}
}
return mode;
}
//===================sort=======================
void sort(int list[],int last)
{
for(int current=0; current< last; current++)
bubbleUp(list, current, last);
return;
}//sort
//===============bubbleUp===============================
void bubbleUp(int list[], int current, int last)
{
for( int walker = last; walker > current; walker--)
if(list[walker] < list[walker-1])
{
int temp = list[walker];
list[walker] = list[walker-1];
list[walker-1] = temp;
}
return;
}//bubble sort
//===================Median=======================
//find the median of scores
float calcMedian(ofstream& report, int list[], int size)
{
double median;
sort(list, size); // this will sort the list
if (size%2 ==0)
{
median = ((list[(size/2)+1]) + (list[size/2]))/2;
median = floor(median);
}
else
{
median = (float) (list[size/2]);
median = ceil(median);
}
return median;
}
//==============printResults==============
//prints out results
void printResults( float mean, float median, float mode)
{
cout<<"The mean is"<<setw(4)<<mean<<endl;
cout<<"The median is"<<setw(4)<<median<<endl;
cout<<"The mode is"<<setw(4)<<mode<<endl;
return;
}