help please

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Oct 2006
Posts: 35
Reputation: JS1988 is an unknown quantity at this point 
Solved Threads: 0
JS1988 JS1988 is offline Offline
Light Poster

help please

 
0
  #1
Dec 6th, 2006
Could somebody please me get this to compile and run


  1.  
  2. #include <iostream>
  3. #include <iomanip>
  4. #include <fstream>
  5. #include <cmath>
  6. using namespace std;
  7.  
  8. // global constant declaration
  9. const int MAX_SIZE = 50;
  10. const int NUM_RANGE = 25;
  11. void getNumbers( int numbers[ ], int& size, int MAX_SIZE );
  12. void printResults( float mean, float median, float mode );
  13. void createFrequency(int scores [] , int size, int frequency[], int range);
  14. void createHistogram(int frequency[], int range);
  15. float calcMean(ofstream& inFile, int numbers, int size );
  16. void sort (int list[], int last);
  17. void bubbleUp(int list[], int current, int last);
  18. float calcMedian(ofstream&, int list[], int);
  19. float calcMode(ofstream& report, int freq[], int size);
  20.  
  21.  
  22. int main( )
  23. {
  24. float mean = 0;
  25. float median = 0;
  26. float mode= 0;
  27. int numbers[ MAX_SIZE ], size;
  28.  
  29.  
  30. getNumbers( numbers, size,MAX_SIZE);
  31.  
  32. createFrequency(size,frequency[],NUM_RANGE);
  33. createHistogram(frequency, NUM_RANGE);
  34.  
  35. mean=calcMean(ofstream& inFile, numbers, size );
  36.  
  37. sort (int list[], int last);
  38. bubbleUp(int list[], int current, int last);
  39. median = calcMedian(ofstream&, int list[], int);
  40. mode = calcMode(ofstream& report, int freq[], int size);
  41. printResults(mean, median, mode );
  42.  
  43. return 0;
  44. }
  45. //===============getNumbers========================
  46. void getNumbers( int numbers[ ], int& size,int MAX_SIZE )
  47. {
  48. ofstream inFile;
  49. size = 0;
  50. inFile.open( "scores.txt" );
  51. if ( !inFile )
  52. {
  53. cerr << "scores.txt cannot be opened" << endl;
  54. exit( 100 );
  55. }
  56. while ( ( size < MAX_SIZE ) && ( inFile >> numbers[ size ] ) )
  57. {
  58. ++size;
  59. }
  60. inFile.close( );
  61. return;
  62. }//end of getData
  63.  
  64. //===================Mean=======================
  65. // finds the average of the numbers
  66. float calcMean(ofstream & inFile, int numbers[ ], int size)
  67. {
  68. float total=0;
  69. float mean;
  70.  
  71. for (int i=0;i<size;i++)
  72. total= total+ numbers[i ];
  73.  
  74. mean =total/size;
  75.  
  76.  
  77.  
  78. return mean;
  79. }//End of mean
  80. //===============createFrequency========================
  81. //Creates the frequency of scores
  82. void createFrequency(int scores [] , int size, int frequency[],
  83. int range)
  84. {
  85. for(int i = 0;i < size;i++)
  86. frequency [scores[i]]++;
  87. return;
  88. }//end createFrequency
  89. //===================createHistogram=======================
  90. //Creates Histogram of scores
  91. void createHistogram(int frequency[], int range)
  92. {
  93. for(int i = 0;i<=range;i++)
  94. {
  95. cout<<setw(3)<<i<<setw(3)<<frequency[i];
  96. for(int j = 1;j<=frequency[i];j++)
  97. {
  98. cout<<"*";
  99. cout<<endl;
  100. }
  101. }
  102. return;
  103. }//end createHistogram
  104.  
  105. //===================Median=======================
  106. //find the median of scores
  107. float calcMedian(ofstream& file, int list[], int size)
  108. {
  109. double median;
  110.  
  111.  
  112. sort(list, size); // this will sort the list
  113. if (size%2 ==0)
  114. {
  115. median = ((list[(size/2)+1]) + (list[size/2]))/2;
  116. median = floor(median);
  117. }
  118. else
  119. {
  120. median = (float) (list[size/2]);
  121. median = ceil(median);
  122. }
  123.  
  124.  
  125. return median;
  126. }//end Median
  127.  
  128. //===================sort=======================
  129. void sort(int list[], int last)
  130. {
  131. for(int current=0; current< last; current++)
  132. bubbleUp(list, current, last);
  133. return;
  134. }//sort
  135. //===============bubbleUp===============================
  136. void bubbleUp(int list[], int current, int last)
  137. {
  138. for( int walker = last; walker > current; walker--)
  139. if(list[walker] < list[walker-1])
  140. {
  141. int temp = list[walker];
  142. list[walker] = list[walker-1];
  143. list[walker-1] = temp;
  144. }
  145. return;
  146. }//bubble sort
  147. //=================mode=====================//
  148. float mode (ofstream& file, int frequency[], int size)
  149. {
  150.  
  151. int largest=0, mode;
  152.  
  153. for(int i = 0; i < size; i++)
  154. {
  155. if(frequency[i] > largest)
  156. {
  157. largest = frequency[i];
  158. }
  159. }
  160.  
  161. for(int i = 0; i < size; i++)
  162. {
  163. if(frequency[i] == largest)
  164. mode = i;
  165. }
  166. return mode ;
  167. }
  168.  
  169. //==============printResults==============
  170. //prints out results
  171. void printResults( float mean, float median, float mode)
  172. {
  173. cout<<"The mean is"<<setw(4)<<mean<<endl;
  174. cout<<"The median is"<<setw(4)<<median<<endl;
  175. cout<<"The mode is"<<setw(4)<<mode<<endl;
  176.  
  177. return;
  178. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,619
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 468
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: help please

 
0
  #2
Dec 6th, 2006
Post your errors and highlight the lines on which they occur...that way it would be easier for us to spot them...
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: help please

 
0
  #3
Dec 6th, 2006
A number of errors in the above code:
  1. float mean = 0;
  2. float median = 0;
  3. float mode= 0;
  4. int numbers[ MAX_SIZE ], size;
  5.  
  6.  
  7. getNumbers( numbers, size,MAX_SIZE);
  8.  
  9. createFrequency(size,frequency[],NUM_RANGE); // you never declared "frequency", so why are you trying to use it?
  10. createHistogram(frequency, NUM_RANGE);
  11.  
  12. mean=calcMean(ofstream& inFile, numbers, size ); // you can't just declare a variable right when you're calling the function... declare it beforehand
  13.  
  14. sort (int list[], int last); // read previous comment on this...
  15. bubbleUp(int list[], int current, int last); // ^_^
  16. median = calcMedian(ofstream&, int list[], int); // crazy, too
  17. 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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 35
Reputation: JS1988 is an unknown quantity at this point 
Solved Threads: 0
JS1988 JS1988 is offline Offline
Light Poster

Re: help please

 
0
  #4
Dec 6th, 2006
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
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: help please

 
0
  #5
Dec 7th, 2006
Originally Posted by JS1988 View Post
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:
  1. 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:
  1. void createFrequency(int scores [] , int size, int *frequency,
  2. 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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 35
Reputation: JS1988 is an unknown quantity at this point 
Solved Threads: 0
JS1988 JS1988 is offline Offline
Light Poster

Re: help please

 
0
  #6
Dec 7th, 2006
Ok i fixed all that now i think, but i still got that problem with my ofstream&. Oh and im not using pointers
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: help please

 
0
  #7
Dec 7th, 2006
Originally Posted by JS1988 View Post
Ok i fixed all that now i think, but i still got that problem with my ofstream&.
You mean this line, correct?
  1. 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:
  1. ofstream inFile;
  2. 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.
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. void change_x(int x);
  5.  
  6. int main() {
  7.  
  8. int x=0;
  9. cout << "Inital value of x is " << x << ".\n";
  10. change_x(x);
  11. cout << "Value of x after change_x() is " << x << ".\n";
  12. }
  13.  
  14. void change_x(int x) {
  15.  
  16. x = 500;
  17. }
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:
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. void change_x(int *x);
  5.  
  6. int main() {
  7.  
  8. int x=0;
  9. cout << "Inital value of x is " << x << ".\n";
  10. change_x(&x);
  11. cout << "Value of x after change_x() is " << x << ".\n";
  12. }
  13.  
  14. void change_x(int *x) { // note that we could also use "int &x"
  15.  
  16. *x = 500;
  17. }
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
"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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 35
Reputation: JS1988 is an unknown quantity at this point 
Solved Threads: 0
JS1988 JS1988 is offline Offline
Light Poster

Re: help please

 
0
  #8
Dec 7th, 2006
I was declaring it the wrong way. Thanks
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 35
Reputation: JS1988 is an unknown quantity at this point 
Solved Threads: 0
JS1988 JS1988 is offline Offline
Light Poster

Re: help please

 
0
  #9
Dec 7th, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 35
Reputation: JS1988 is an unknown quantity at this point 
Solved Threads: 0
JS1988 JS1988 is offline Offline
Light Poster

Re: help please

 
0
  #10
Dec 7th, 2006
Sorry i Forgot code tags
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC