# Help with function

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 03-12-2003
3kgt
Help with function
In my first function "compute" if I want to return the values of "sum" and "sum_of_squares" in main what do I have to do?
Code:

```//Name: Joseph Valenzuela //Date: March 11, 2003 //Purpose: To design and implement my own program for project1. #include <iostream.h> // For output to the screen and input from user #include <fstream.h> // To allow input and output streams #include <stdlib.h> // Contains exit function #include <math.h> // Contains square root function double compute(); //Precondition: //Postcondition: double mean(double xnum, double sum); //Precondition: //Postcondition: double deviation(double var); //Precondition: //Postcondition: double variance(double sum_of_squares, double xnum, double avg); //Precondition: //Postcondition: void main() {   double xnum,sum,avg; ifstream input_data; cout <<"\nWELCOME TO THE BASIC STATISTICS PACKAGE!\n"; cout << "Enter amount of numbers to be evaluated: "; cin >> xnum;  sum = compute();  avg = mean(xnum, sum);  cout << sum << endl;  cout << avg << endl; } double compute() {   double sum=0, sum_of_squares=0, in_num=0, count=0;   ifstream input_data;  input_data.open("stats.dat");  if (input_data.fail())   {     cout <<"Input file could not be opened.\n";     exit(1);   }   input_data >> in_num;   while (!input_data.eof())     {       count++;     sum += in_num;     sum_of_squares += in_num*in_num;     input_data >> in_num;     }     return(sum);   return (sum_of_squares);   } double mean(double xnum, double sum) {   double avg=0;   avg = sum/xnum;   return(avg); } double variance(double sum_of_squares, double xnum, double avg) {     double var=0;     var = (sum_of_squares/xnum) - (avg*avg);   return(var);   } double deviation(double var) {   double dev;     dev =  sqrt(var);   return(dev); }```
• 03-12-2003
3kgt
one more question
If I wanted to compute the median of all the numbers in my input file, in this program what would be the best way to go about this? Thanks!
• 03-12-2003
Eibro
Re: Help with function
Quote:

Originally posted by 3kgt
In my first function "compute" if I want to return the values of "sum" and "sum_of_squares" in main what do I have to do?
Code:

```//Name: Joseph Valenzuela //Date: March 11, 2003 //Purpose: To design and implement my own program for project1. #include <iostream.h> // For output to the screen and input from user #include <fstream.h> // To allow input and output streams #include <stdlib.h> // Contains exit function #include <math.h> // Contains square root function double compute(); //Precondition: //Postcondition: double mean(double xnum, double sum); //Precondition: //Postcondition: double deviation(double var); //Precondition: //Postcondition: double variance(double sum_of_squares, double xnum, double avg); //Precondition: //Postcondition: void main() {   double xnum,sum,avg; ifstream input_data; cout <<"\nWELCOME TO THE BASIC STATISTICS PACKAGE!\n"; cout << "Enter amount of numbers to be evaluated: "; cin >> xnum;  sum = compute();  avg = mean(xnum, sum);  cout << sum << endl;  cout << avg << endl; } double compute() {   double sum=0, sum_of_squares=0, in_num=0, count=0;   ifstream input_data;  input_data.open("stats.dat");  if (input_data.fail())   {     cout <<"Input file could not be opened.\n";     exit(1);   }   input_data >> in_num;   while (!input_data.eof())     {       count++;     sum += in_num;     sum_of_squares += in_num*in_num;     input_data >> in_num;     }     return(sum);   return (sum_of_squares);   } double mean(double xnum, double sum) {   double avg=0;   avg = sum/xnum;   return(avg); } double variance(double sum_of_squares, double xnum, double avg) {     double var=0;     var = (sum_of_squares/xnum) - (avg*avg);   return(var);   } double deviation(double var) {   double dev;     dev =  sqrt(var);   return(dev); }```

void main(), what the hell? main() returns an int.
You can only return one value from a function. However, you could pass two parameters to your compute function (one for sum and one for sum of squares) and use them as return values. For example:
Code:

```void compute(double& sum, double& sum_of_squares) {   sum = sum_of_squares = 0;   double in_num=0, count=0;   ifstream input_data;  input_data.open("stats.dat");  if (input_data.fail())   {     cout <<"Input file could not be opened.\n";     exit(1);   }   input_data >> in_num;   while (!input_data.eof())     {       count++;     sum += in_num;     sum_of_squares += in_num*in_num;     input_data >> in_num;     } }```
Then call it like this:
Code:

```double sum, sum_of_squares; compute(sum, sum_of_squares);```
After that function call, sum and sum_of_squares will hold the values given to them in the function.
• 03-12-2003
Eibro
Re: one more question
Quote:

Originally posted by 3kgt
If I wanted to compute the median of all the numbers in my input file, in this program what would be the best way to go about this? Thanks!
This is how *I* would do it:
Code:

```#include <vector> #include <algorithm> // ... double median() {   std::ifstream fin("log.txt");   std::vector<int> medianvec;   double val;   while (log >> val)       medianvec.push_back(val);   std::sort(medianvec.begin(), medianvec.end());   int size = medianvec.size();   if (size & 1)       return medianvec[size/2];   else  // What are you supposed to do with two medians? Get the average?       return (medianvec[size/2] + medianvec[size/2-1]) / 2; }```
Basically, read all integers into an array, sort the array and return the element at array size / 2 (middle of the array).
• 03-12-2003
nag
Re: Re: Help with function
Quote:

Originally posted by Eibro
void main(), what the hell? main() returns an int.

Why are you are worrying about it so much,
I have a book
Microsoft's Guide To C++ Programming.
In this book the writer throughout used void main() in all code examples :):):):):) (yes the creators of VC++)
• 03-12-2003
ripper079
Quote:

Why are you are worrying about it so much
....
...
...
...
Spare us the time and make a search on this board on the topic. It has been covered gizillions time!!!
• 03-12-2003
nag
yes but most newbies dont see faq nor anybody tells them about this!
• 03-12-2003
ripper079
Quote:

yes but most newbies dont see faq nor anybody tells them about this!
Hmmm I find it very hard to miss them. Well, there is a FAQ board and a faq button(upper right). Maybe we should ask the mod the make then larger (no sarcasm)... well I donīt now. How(where) would you like them to be????
• 03-12-2003
alpha
or you could download a more standard compliant compiler, newer compilers; and try to compile void main. you will get errors. it is not standard. if you want to know what, go to Bjarne Stroustrup's site; check the FAQ, as well.

Just click here. It is a link to the faq.
• 03-12-2003
Govtcheez
> faq button(upper right).

That's the FAQ for using vBulletin. The rules and stuff are in the Programming FAQ link at the bottom of the page.

> yes the creators of VC++

So they wrote a compiler. Who cares? They didn't invent the language.
• 03-12-2003
3kgt
Thanks eibro
Hey thanks for trying to help me out with that, I am trying the call by refrence right now, I dont think I can compute the median that way because we haveent learned the algorithm or vector libraries. I think for the median I have to close my input file then reopen it to catch the middle numbers and if it is even I have to average the two middle numbers and if it is odd I can catch the middle number and return that value. What do you think?
• 03-12-2003
sounds like a good learning exercise to me. If you store your input in an array and keep track of the indexes used you can use the index to help you determine the median. Say the last index used was 2. That means there were 3 items read in and the median is at index 1. If the last index was 99 then there were 100 items read in and the median is the average between the value at index 49 and index 50. Since you already know what xnum is going to be, user has to input the value!, then the last index is xnum - 1 and you can go from there.
• 03-12-2003
3kgt
My median function
Ok guys here is what I came up with for my median function, but I am still getting a compiler error saying that it expects a statement before the else? How should I fix that?

Code:

```double median (int xnum) {   double median,med_pos,count=0, temp;   ifstream input_data;         input_data.open("stats.dat");     if (input_data.fail())       {         cout <<"Input file could not be opened.\n";         exit(1);                 med_pos = xnum/2;       }     if ((xnum%2)==0)       {         median=0;         while (input_data >> temp)           {                    count++;             if((count==med_pos)||(count==med_pos+1))                     median = median + temp;           }         median = median/2;           }     return(median);             else if ((xnum%2)!=0)       {         median=0;         while (input_data >> temp)           {                    count++;             if(count==med_pos+1)               median = median + temp;           }         median = median/2;       }     return(median);     }```
• 03-12-2003
XSquared
Remove the return statement before the else statement.

Code:

```        ...         median = median/2; } //return(median); else if ((xnum%2)!=0) {         median=0;         ...```
• 03-12-2003
3kgt
Thanks, but is there anything wrong with the code?
Hey thanks that fixed it and now it compiles correctly but how come it doesnt return anything, it returns 0 every time, is there something wrong with the code?
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last