| | |
help please
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Oct 2006
Posts: 35
Reputation:
Solved Threads: 0
Could somebody please me get this to compile and run
C++ Syntax (Toggle Plain Text)
#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; }
A number of errors in the above code:
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
C++ Syntax (Toggle Plain Text)
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
"Technological progress is like an axe in the hands of a pathological criminal."
All my posts may be freely redistributed under the terms of the MIT license.
All my posts may be freely redistributed under the terms of the MIT license.
•
•
•
•
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
Create an
int array called "frequency".Then when you call createFrequency, make sure it looks like this:
C++ Syntax (Toggle Plain Text)
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:
C++ Syntax (Toggle Plain Text)
void createFrequency(int scores [] , int size, int *frequency, int range)
Hope this helps
"Technological progress is like an axe in the hands of a pathological criminal."
All my posts may be freely redistributed under the terms of the MIT license.
All my posts may be freely redistributed under the terms of the MIT license.
•
•
•
•
Ok i fixed all that now i think, but i still got that problem with my ofstream&.
C++ Syntax (Toggle Plain Text)
mean=calcMean(ofstream& inFile, numbers, size );
C++ Syntax (Toggle Plain Text)
ofstream inFile; mean=calcMean(&inFile, numbers, size);
•
•
•
•
Oh and im not using pointers
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. C++ Syntax (Toggle Plain Text)
#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; }
However, this code uses memory addresses to do the job, and it will print out 500:
C++ Syntax (Toggle Plain Text)
#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; }
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
"Technological progress is like an axe in the hands of a pathological criminal."
All my posts may be freely redistributed under the terms of the MIT license.
All my posts may be freely redistributed under the terms of the MIT license.
•
•
Join Date: Oct 2006
Posts: 35
Reputation:
Solved Threads: 0
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); 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 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); mean=calcMean(&inFile, numbers, size); sort ( list, last); bubbleUp( list, current, last); median = calcMedian(&file, list, size); mode = calcMode(&report, frequency, 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; }
Last edited by ~s.o.s~; Dec 8th, 2006 at 12:38 pm. Reason: Added code tags.
![]() |
Other Threads in the C++ Forum
- Previous Thread: Help with storing to a variable please
- Next Thread: Problem with function arg pointer syntax
| Thread Tools | Search this Thread |
api array arrays based binary c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game generator givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






