Thread: Help with function

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    29

    Arrow 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);
    
    }

  2. #2
    Registered User
    Join Date
    Feb 2003
    Posts
    29

    Lightbulb 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!

  3. #3
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361

    Re: Help with function

    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.

  4. #4
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361

    Re: one more question

    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).
    Last edited by Eibro; 03-12-2003 at 01:34 AM.

  5. #5
    Registered User nag's Avatar
    Join Date
    May 2002
    Posts
    22

    Re: Re: Help with function

    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++)
    Two men looked out from Prison Bars,One saw the mud,the other saw stars.

  6. #6
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    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!!!

  7. #7
    Registered User nag's Avatar
    Join Date
    May 2002
    Posts
    22
    yes but most newbies dont see faq nor anybody tells them about this!
    Two men looked out from Prison Bars,One saw the mud,the other saw stars.

  8. #8
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    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????

  9. #9
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    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.

  10. #10
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    > 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.

  11. #11
    Registered User
    Join Date
    Feb 2003
    Posts
    29

    Arrow 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?

  12. #12
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    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.

  13. #13
    Registered User
    Join Date
    Feb 2003
    Posts
    29

    Arrow 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);
      
      }

  14. #14
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Remove the return statement before the else statement.

    Code:
    	...
    	median = median/2;
    }
    //return(median);
    else if ((xnum%2)!=0) 
    {
    	median=0;
    	...
    Last edited by XSquared; 03-12-2003 at 08:33 PM.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  15. #15
    Registered User
    Join Date
    Feb 2003
    Posts
    29

    Arrow 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?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM