Could somebody please me get this to compile and run

#include <iostream> 
#include <iomanip> 
#include <fstream> 
#include <cmath>
using namespace std; 
 
// global constant declaration 
const int MAX_SIZE = 50; 
const int NUM_RANGE = 25; 
void getNumbers( int numbers[ ], int& size, int MAX_SIZE ); 
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& inFile, int numbers, int size ); 
void sort (int list[], int last); 
void bubbleUp(int list[], int current, int last); 
float calcMedian(ofstream&, int list[], int); 
float calcMode(ofstream& report, int freq[], int size);   
 
 
int main( ) 
{ 
   float mean = 0;
   float median = 0;
   float mode= 0;
   int numbers[ MAX_SIZE ], size; 
 
 
   getNumbers( numbers, size,MAX_SIZE); 
 
   createFrequency(size,frequency[],NUM_RANGE); 
   createHistogram(frequency, NUM_RANGE); 
 
   mean=calcMean(ofstream& inFile, numbers, size ); 
 
   sort (int list[], int last);
   bubbleUp(int list[], int current, int last); 
   median = calcMedian(ofstream&, int list[], int); 
   mode = calcMode(ofstream& report, int freq[], int size); 
   printResults(mean, median, mode );
 
   return 0; 
} 
//===============getNumbers======================== 
void getNumbers( int numbers[ ], int& size,int MAX_SIZE ) 
{ 
   ofstream inFile; 
    size = 0; 
    inFile.open( "scores.txt" ); 
    if ( !inFile ) 
       { 
         cerr << "scores.txt cannot be opened" << endl; 
          exit( 100 ); 
       } 
     while ( ( size < MAX_SIZE ) && ( inFile >> numbers[ size ] ) ) 
       { 
            ++size; 
       } 
         inFile.close( ); 
    return; 
}//end of getData 
 
//===================Mean=======================
// finds the average of the numbers
float calcMean(ofstream &  inFile,  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 scores [] , int size, int frequency[], 
                      int range) 
{ 
    for(int i = 0;i < size;i++) 
       frequency [scores[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 
 
//===================Median=======================
//find the median of scores
float calcMedian(ofstream&  file, 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;
}//end Median
 
//===================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
//=================mode=====================//
float mode (ofstream&  file, 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 ; 
}
 
//==============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;    
}

Recommended Answers

All 11 Replies

Post your errors and highlight the lines on which they occur...that way it would be easier for us to spot them...

A number of errors in the above code:

float mean = 0;
   float median = 0;
   float mode= 0;
   int numbers[ MAX_SIZE ], size; 
 
 
   getNumbers( numbers, size,MAX_SIZE); 
 
   createFrequency(size,frequency[],NUM_RANGE); // you never declared "frequency", so why are you trying to use it?
   createHistogram(frequency, NUM_RANGE); 
 
   mean=calcMean(ofstream& inFile, numbers, size ); // you can't just declare a variable right when you're calling the function... declare it beforehand
 
   sort (int list[], int last); // read previous comment on this...
   bubbleUp(int list[], int current, int last); // ^_^
   median = calcMedian(ofstream&, int list[], int); // crazy, too
   mode = calcMode(ofstream& report, int freq[], int size); // DECLARE YOUR VARIABLES BEFOREHAND!

Hopefully you understand... you're trying to declare a lot of variables as you're calling functions, which is bad for 2 reasons: 1, you never initalized the variables; 2, you just can't do that.

Hope this helps

ok yeah i see what your saying i think but im also getting a lot of
expected primary-expression before ']' token on the createFrequency function for example how is that fixed

ok yeah i see what your saying i think but im also getting a lot of
expected primary-expression before ']' token on the createFrequency function for example how is that fixed

I tried to explain.

Create an int array called "frequency".

Then when you call createFrequency, make sure it looks like this:

createFrequency(size,frequency,NUM_RANGE);

Do you see what I'm getting at? The compiler has no idea what frequency is. Until you declare it as an identifier, the compiler will have no idea what to do when it encounters "frequency". So to prevent this error, you must create a variable to pass to the function.

Another problem you are going to have is scope. I haven't checked your other functions, but take createFrequency() for example. When you pass it a variable that it's supposed to modify, it modifies it, and then the variable goes out of scope. So it's completely useless unless you use a pointer. See more on scope here:
http://www.imada.sdu.dk/~svalle/courses/dm14-2005/mirror/c/subsection3_9_1.html#SECTION0009100000000000000

So if you're using a pointer, this is how your function prototype for createFrequency should look like:

void createFrequency(int scores [] , int size, int *frequency, 
                      int range)

Hope this helps

Ok i fixed all that now i think, but i still got that problem with my ofstream&. Oh and im not using pointers

Ok i fixed all that now i think, but i still got that problem with my ofstream&.

You mean this line, correct?

mean=calcMean(ofstream& inFile, numbers, size );

I've told you already, don't declare variables when you're calling a function. Instead, declare them beforehand. So it would look something like this:

ofstream inFile;
mean=calcMean(&inFile, numbers, size);

Oh and im not using pointers

Just to clarify, when I said you had to use pointers, you can also declare parameters like int &variable . Same idea; you're passing the memory address of a main variable, and passing it to a function. But if you don't pass a pointer, and you pass the value instead, it will not work. Let me give you an example.

#include <iostream>
using namespace std;

void change_x(int x);

int main() {

    int x=0;
    cout << "Inital value of x is " << x << ".\n";
    change_x(x);
    cout << "Value of x after change_x() is " << x << ".\n";
}

void change_x(int x) {

    x = 500;
}

Innoncent newbies would expect this code to print out 500 as the changed x value. But x has gone out of scope, and so it will print out 0 even after change_x().

However, this code uses memory addresses to do the job, and it will print out 500:

#include <iostream>
using namespace std;

void change_x(int *x);

int main() {

    int x=0;
    cout << "Inital value of x is " << x << ".\n";
    change_x(&x);
    cout << "Value of x after change_x() is " << x << ".\n";
}

void change_x(int *x) { // note that we could also use "int &x"

    *x = 500;
}

If we use int &x instead of int *x when declaring change_x(), then x could actually be used as a regular variable, and a * would not have to be appended before changing the value of the variable.

Hope this helps

I was declaring it the wrong way. Thanks

Ok this is what I got now. I declared them the way you showed me I think there are still a few problems yet. Am i going out of scope still? There is somthing wrong with my function and their declarations I'm not in to pointers yet so dont worry about them now. The problems are Bolded

#include <iostream> 
#include <iomanip> 
#include <fstream> 
#include <cmath>
using namespace std; 
 
// global constant declaration 
const int MAX_SIZE = 50; 
const int NUM_RANGE = 25; 
void getNumbers( int numbers[ ], int& size, int MAX_SIZE ); 
void printResults( float mean, float median, float mode ); 
void createFrequency(int scores [] , int size, int frequency[], int range); 
void createHistogram(int frequency[], int range); 
[B]float calcMean(ofstream& inFile, int numbers, int size ); [/B]
void sort (int list[], int last); 
void bubbleUp(int list[], int current, int last); 
[B]float calcMedian(ofstream&, int list[], int); [/B]
[B]float calcMode(ofstream& report, int freq[], int size); [/B]
 
 
int main( ) 
{ 
float mean = 0;
float median = 0;
float mode= 0;
int frequency[50];
int numbers[ MAX_SIZE ], size; 
int list[50];
int scores[50];
int last;
int current;
ofstream inFile;
ofstream report;
ofstream file;
 
getNumbers( numbers, size,MAX_SIZE); 
 
createFrequency(scores,size,frequency,NUM_RANGE); 
createHistogram(frequency, NUM_RANGE); 
 
[B]mean=calcMean(&inFile, numbers, size);[/B] 
sort ( list, last);
bubbleUp( list, current, last); 
[B]median = calcMedian(&file, list, size); [/B]
[B]mode = calcMode(&report, frequency, size); [/B]
printResults(mean, median, mode );
 
return 0; 
} 
//===============getNumbers======================== 
void getNumbers( int numbers[ ], int& size,int MAX_SIZE ) 
{ 
ofstream inFile; 
size = 0; 
inFile.open( "scores.txt" ); 
if ( !inFile ) 
{ 
cerr << "scores.txt cannot be opened" << endl; 
exit( 100 ); 
} 
[B]while ( ( size < MAX_SIZE ) && ( inFile >> numbers[ size ] ) ) [/B]{ 
++size; 
} 
inFile.close( ); 
return; 
}//end of getData 
 
//===================Mean=======================
// finds the average of the numbers
float calcMean(ofstream & inFile, 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 scores [] , int size, int frequency[], 
int range) 
{ 
for(int i = 0;i < size;i++) 
frequency [scores[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 
 
//===================Median=======================
//find the median of scores
float calcMedian(ofstream& file, 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;
}//end Median
 
//===================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
//=================mode=====================//
float mode (ofstream& file, 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 ; 
}
 
//==============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; 
}

Sorry i Forgot code tags

Sorry i Forgot code tags

Hmm, you forget the code tags, I see that you've edited your post, but you haven't added the code tags! I know that you haven't been locked out of editing, as it's only 15 minutes since you posted. Anyway....

Ran your code through my compiler. Here are some errors.

In your prototype of calcMean(), you forgot the [] after numbers :

float calcMean(ofstream& inFile, int numbers /* this is what's messing you up */, int size );

Also, I can't figure out why you pass inFile to calcMean(), since it never uses it...

In those calc...() functions, change ofstream & to ofstream *. It has some sort of problem passing non-const types or something; I'm not really sure...

Ah! I've found the problem here:

while ( ( size < MAX_SIZE ) && ( inFile >> numbers[ size ] ) ) {

There's absoloutely nothing wrong with this line of code; I proved it to myself by writing little test snippets using inFile. The problem is that inFile is not really an in file:

void getNumbers( int numbers[ ], int& size,int MAX_SIZE )
{
ofstream inFile;

Pretty hard to read data from an ofstream... ;)

Hope this helps

Alright I got it to compile but now all it does is print the asterisks in the createHistogram function and nothing else to the screen what am I doing wrong now.

#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;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.