Help with function

This is a discussion on Help with function within the C++ Programming forums, part of the General Programming Boards category; here is the whole program... Code: //Name: Joseph Valenzuela //Date: March 11, 2003 //Purpose: To design and implement my own ...

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

    Lightbulb My entire program

    here is the whole program...
    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
    
    void compute(double& sum, double& sum_of_squares);
    //Precondition:None
    //Postcondition:To compute the sum and sum of squares of the numbers in the input file
    double mean(int xnum, double sum);
    //Precondition:Requires the amount of numbers and sum of those numbers
    //Postcondition:Calculates the mean of the numbers in the input file and returns the value
    double variance(double sum_of_squares, int xnum, double avg);
    //Precondition:Requires sum of the squares, the amount of numbers and the mean of the numbers
    //Postcondition:Calculates the variance of the numbers in the input file and returns the value
    double deviation(double var); 
    //Precondition:Requires the variance
    //Postcondition:Calculates the standard deviation of the numbers in the input file and returns the value
    double median (int xnum);
    //Precondition:Requires amount of numbers in input file
    //Postcondition:Calculates the median of the numbers in the input file, and returns the value
    
    void main()
    {
      
      double sum,sum_of_squares,avg,var,dev,med; //Declares variables
      int xnum;
      
      
      cout <<"\nWELCOME TO THE BASIC STATISTICS PACKAGE!\n";
      
      cout << "Enter amount of numbers to be evaluated: ";
      cin >> xnum;
     
      cout.setf(ios::fixed);
      cout.setf(ios::showpoint);
      cout.precision(2);
      
      compute(sum,sum_of_squares);
      avg = mean(xnum, sum);
      cout << "The Mean price of the numbers is $" << avg << endl;
      
      med = median(xnum);
      cout <<"The median price of the numbers is $" << med << endl;
    
      var = variance(sum_of_squares,xnum,avg);
      cout <<"The Variance price of the numbers is $" << var << endl;
      
      dev = deviation(var);
      cout <<"The Standard Deviation price of the numbers is $" << dev << endl;
      
      
      
      
      
    }
    
    //Precondition:None
    //Postcondition:To compute the sum and sum of squares of the numbers in the input file
    void compute(double& sum, double& sum_of_squares)
    {
      
      sum = sum_of_squares = 0;
      double in_num=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())
        {
          sum += in_num;
          sum_of_squares += in_num*in_num;
          input_data >> in_num;
        };
      
      input_data.close();
      
    }
    
    //Precondition:Requires the amount of numbers and sum of those numbers
    //Postcondition:Calculates the mean of the numbers in the input file
    double mean(int xnum, double sum)
    {
      double avg=0;
      
      avg = sum/xnum;
      return(avg);
      
    }
    
    //Precondition:Requires sum of the squares, the amount of numbers and the mean of the numbers
    //Postcondition:Calculates the variance of the numbers in the input file
    double variance(double sum_of_squares, int xnum, double avg)
    {
      
      double var=0;
      
      var = (sum_of_squares/xnum) - (avg*avg);
      return(var);
      
    }
    
    //Precondition:Requires the variance
    //Postcondition:Calculates the standard deviation of the numbers in the input file
    double deviation(double var)
    {
      
      double dev;
      
      dev =  sqrt(var);
      return(dev);
      
    }
    
    //Precondition:Requires amount of numbers in input file
    //Postcondition:Calculates the median of the numbers in the input file, and returns the value
    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;
         
          }
       
        
        
        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);
      
      }

  2. #17
    Registered User
    Join Date
    Feb 2003
    Posts
    29
    Hey I got it guys dont worry about it. Thanks

Page 2 of 2 FirstFirst 12
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, 02: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21