Thread: C++ programming help

  1. #1
    Registered User shyrenee89's Avatar
    Join Date
    Feb 2010
    Location
    NIU
    Posts
    3

    C++ programming help

    Hi i have to do an assignment for class that want me to call by reference... i wrote one that builds and compose but when i run it it crashes.. so can someone please help me here?

    Code:
    #include <iostream>
    #include <iomanip>
    #include <fstream>
    #include <math.h>
    
     
    using namespace std;
     
    void calcByRef(double [], int , double& , double& , double& , double& , double& );
    
     
    int main ()
    {
     
            double ar [15] = {2.4,  6.5,  1.2,  0.7,  15.3,  3.9,  78.1,  12.0,  5.4,  10.1,  24.0,
                                               7.8,  46.8,  1.3,  0.1};
           
            double high, low, avg, median, stdDev;
            int num;
            
           
            cout << "Results using references";
    		
    		 calcByRef(ar, num, low, high, avg, median, stdDev);
           
            cout << "Minimum: \t\t\t" << low << endl;
            cout << "Maximum: \t\t\t" << high << endl;
            cout << "Mean: \t\t\t" << avg << endl;
            cout << "Median: \t\t\t" << median << endl;
            cout << "Standard Deviation: \t\t\t" << stdDev << endl;
           
          
           
            return 0;
    }
     
    /**********************************************************************************
    Function: calcByRef
     
    Use: Uses the passed in array to calculate and pass back.
     
    Arguments: 1. ar2[]: the array that contains the second set of numbers.
                       2. num: Number of elements in the array.
                       3. low: The smallest number in the array.
                       4. high: The largest number in the array.
                       5. avg: The mean of all the elements.
                       6. median: The median number of the array.
                       7. stdDev: The calcualated standard deviation of the elements.
                      
    Returns: nothing
    **********************************************************************************/
    void calcByRef(double ar[], int num, double& low, double& high, double& avg, double& median, double& stdDev)
                                                                                                  
           {
           
            int i, a, b;
           
            double sum;
           
            low = ar[0];
           
            high = ar[14];
           
            for(i=0; i<15; i++)
                    {
                    sum += ar[i];
                    }
            avg = (double)sum / num;
           
            median = ar[num/2];
           
            for(i=0; i<15; i++)
                    {
                    sum += i*i;
                    a = sum;
                    }
                   
            for(i=0; i<15; i++)
                    {
                    sum += i;
                    b = sum*sum;
                    }

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    You aren't initializing num to anything. So it is filled with a garbage value and with you use it in an array index it is not happy.
    Woop?

  3. #3
    Registered User shyrenee89's Avatar
    Join Date
    Feb 2010
    Location
    NIU
    Posts
    3
    Thanks, I just initialized num by 15 but it still says program has stopped working when i try ad run it...

    the extra bit is the sort i forgot to include

    Code:
    #include <iostream>
    #include <iomanip>
    #include <fstream>
    #include <math.h>
    
     
    using namespace std;
     
    void calcByRef(double [], int , double& , double& , double& , double& , double& );
    void sortAr (double []);
     
    int main ()
    {
     
            double ar [15] = {2.4,  6.5,  1.2,  0.7,  15.3,  3.9,  78.1,  12.0,  5.4,  10.1,  24.0,
                                               7.8,  46.8,  1.3,  0.1};
           
            double high, low, avg, median, stdDev;
            int num;
    		sortAr (ar);
           
            cout << "Results using references";
    		
        	 calcByRef(ar, num, low, high, avg, median, stdDev);
           
            cout << "Minimum: \t\t\t" << low << endl;
            cout << "Maximum: \t\t\t" << high << endl;
            cout << "Mean: \t\t\t" << avg << endl;
            cout << "Median: \t\t\t" << median << endl;
            cout << "Standard Deviation: \t\t\t" << stdDev << endl;
           
          
           
            return 0;
    }
     
    /**********************************************************************************
    Function: calcByRef
     
    Use: Uses the passed in array to calculate and pass back.
     
    Arguments: 1. ar2[]: the array that contains the second set of numbers.
                       2. num: Number of elements in the array.
                       3. low: The smallest number in the array.
                       4. high: The largest number in the array.
                       5. avg: The mean of all the elements.
                       6. median: The median number of the array.
                       7. stdDev: The calcualated standard deviation of the elements.
                      
    Returns: nothing
    **********************************************************************************/
    void calcByRef(double ar[], int num, double& low, double& high, double& avg, double& median, double& stdDev)
                                                                                                  
           {
           
            int i, a, b;
           
            double sum;
           
    	    num = 15;
    		
            low = ar[0];
           
            high = ar[14];
           
            for(i=0; i<15; i++)
                    {
                    sum += ar[i];
                    }
            avg = (double)sum / num;
           
            median = ar[num/2];
           
            for(i=0; i<15; i++)
                    {
                    sum += i*i;
                    a = sum;
                    }
                   
            for(i=0; i<15; i++)
                    {
                    sum += i;
                    b = sum*sum;
                    }
           
           
            stdDev = sqrt ((a - (double)(b/num))/(num -1));
           
            }
       
       
    /**********************************************************************************
    Function: sortAr
     
    Use: Sorts out the elements in the arrays and puts them in order.
     
    Arguments: 1. array: The inputted array to be sorted.
     
    Returns: nothing
    **********************************************************************************/
    void sortAr( double ar[])
    {
       int top = 0, last = 14, ptr, ssf;
       double temp, array[14];
       while ( top < last)
            {
                    ptr = top;
                    ssf = top;
                   
                    while ( ptr <= last)
                            {
                            if ( array[ptr] < array[ssf])
                                    {
                                     ssf = ptr;
                                    }
                            else
                                    {
                                   
                                    }
                            ptr++;
                           
                            }
                   
                           
                    temp = array[top];
                    array[top] = array[ssf];
                    array[ssf] = temp;
                   
                    top++;    
                           
    }        }

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    sum += ar[i];
    your sum is not initialized - you are adding to the garbage...

    your sort and calculate function should not depend on the size of the array.
    In your case - if you change the array size you will need to update the code of both functions - it is one of your design errors
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    242
    this code has a lot of problems.
    in addition to the initialization problems already mentioned, why are you even passing num into your function if the function is DEFINED so as to handle only arrays with 15 elements?

Popular pages Recent additions subscribe to a feed