how do i fix these errors?

Discussion in 'Mac Programming' started by bravens52, Apr 2, 2010.

  1. bravens52 macrumors regular

    Joined:
    Jul 16, 2009
    #1
    Code:
    #include <iostream>
    #include <iomanip>
    #include <math.h>
    
    using namespace std;
    
    void calcByRef(double ar[], int size, double &min, double &max, double &mean, double &median, double &stDev);
    void sort (double ar[], int size);
    
    
    int main ()
    
    {
    	double ar[15] = {4.2, 5.6, 2.1, 7.0, 51.3, 9.3, 81.7, 20.1, 4.5, 11.1, 40.2, 8.7, 86.4, 3.1, 1.0};
    	sort (ar,15);
    	
    	double min;
    	double max;
    	double median;
    	double stDev;
    	double mean;
    	
    	
    	cout << "Results using references";
    	[COLOR="Black"]
    	calcByRef(ar[], 15, &min, &max, &mean, &median, &stDev); //ERROR= expected primary expression before "]")
    	[/COLOR]
    	return 0;
    }
    
    [COLOR="black"]void calcByRef(ar[], 15, &min, &max, &mean,  &median, &stDev);[/COLOR] //ERROR= variable or field 'calcbyref' declared void
    
    [COLOR="black"]{[/COLOR] ERROR=expected unqualified id before '{' token
    	double sum=0;
    	int i;
    	min = ar[0]
    	max = ar[14]
    	
    	for (i=0; i < 15; i++) 
    	{
    		
    		sum += ar[i];
    	
    	}
    	
    			
    	
    			
    	mean = sum/size;
    
    			
    	if (size % 2==1) //odd
    median = ar[size/2];
    	else // even
    median = (ar[size/2] + ar[(size/2)-1]/2; 
    	
    		
    	stDev=sqrt((sumSq-(sqSum/size))/(size-1));	
    	
    	double sumSq=0, sqSum=0;
    	for(int i=0; i < size;, i++)
    	{
    		sumSq += ar[i] * ar[i];
    		sqSum += ar[i];
    	}
    	
    	
    		  }
    	
    void sort( float ar[15], int size)
    	
    	{
    		int first; double temp;
    		
    		for (int i=0; i < size - 1; i++) 
    		{
    			first=i;
    			
    			for (int j=i + 1; j < size; j++)
    			{
    				if(ar[j] < ar[first])
    					first=j;
    				
    			}
    			temp = ar[i];
    			ar[i]=ar[first];
    			ar[first]=temp;
    		}
    	}
     
  2. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #2
    Let get back to this in a few minutes!

    Where to begin? I'm to lazy tonight to explain so here is what I think you meant!

    Code:
    #include <iostream>
    #include <iomanip>
    #include <math.h>
    
    using namespace std;
    
    void calcByRef(double ar[], int size, double &min, double &max, double &mean, double &median, double &stDev);
    void sort(double ar[], int size);
    
    int main()
    {
        double ar[] = {
               4.2,  5.6,  2.1,  7.0, 51.3
            ,  9.3, 81.7, 20.1,  4.5, 11.1
            , 40.2,  8.7, 86.4,  3.1,  1.0
        };
        const size_t    ar_entries  = (sizeof(ar)/sizeof(ar[0]));
        
        sort(ar, ar_entries);
        
        double min;
        double max;
        double median;
        double stDev;
        double mean;
        
        
        cout << "Results using references";
        
        calcByRef(ar, ar_entries, min, max, mean, median, stDev);
        
        return 0;
    }
    
    void calcByRef(double ar[], int size, double &min, double &max, double &mean, double &median, double &stDev)
    {
        double sum = 0;
        int i;
        min = ar[0];
        max = ar[size-1];
        
        for ( i = 0; i < size; i++ )
        {
            sum += ar[i];
        }
        
        mean = sum / size;
        
        if (size % 2 == 1)
            // odd
            median = ar[size/2];
        else
            //  even
            median = ar[size/2] + ar[(size/2)-1] / 2;
        
        
        double sumSq = 0, sqSum = 0;
        stDev = sqrt((sumSq - (sqSum / size)) / (size - 1));
        
        for ( int i = 0; i < size; i++ )
        {
            sumSq += ar[i] * ar[i];
            sqSum += ar[i];
        }
    }
    
    void sort(double ar[], int size)
    {
        int first;
        double temp;
        
        for ( int i = 0; i < size - 1; i++ )
        {
            first = i;
            
            for ( int j = i + 1; j < size; j++ )
            {
                if ( ar[j] < ar[first] )
                    first = j;
            }
            
            temp = ar[i];
            ar[i] = ar[first];
            ar[first] = temp;
        }
    }
    
     
  3. bravens52 thread starter macrumors regular

    Joined:
    Jul 16, 2009
    #3
    the order you did was not how i learned it and i tried to compile your code and got 8 errors
     
  4. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #4
    Try it again something seems to be screwing up when I "paste" code.
     
  5. bravens52 thread starter macrumors regular

    Joined:
    Jul 16, 2009
    #5
    okay i will switch up the code to look like mines and seee what happens.

    thanks for the help as usual lloyd
     
  6. bravens52 thread starter macrumors regular

    Joined:
    Jul 16, 2009
    #6
    yeah im still getting 8 errors
     
  7. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #7
    I compiles here. So what are these errors?

    Changed it one last time for you!
     
  8. bravens52 thread starter macrumors regular

    Joined:
    Jul 16, 2009
    #8
    the program is building but now its not displaying the output numbers..its just displaying results using preferences
     
  9. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #9
    You seem to be in panic mode. Calm down and think!

    And where, amongst your code, do you "think" it should be outputting anything?

    Hint: Cou;d it be you meant to "cout" soemthing following:
    Code:
        cout << "Results using references";
    	
        calcByRef(ar, ar_entries, min, max, mean, median, stDev);
    
    
     
  10. bravens52 thread starter macrumors regular

    Joined:
    Jul 16, 2009
    #10
    Results using references
    Minimum: 1.0
    Maximum: 86.4
    Mean: 22.4
    Median: 8.7
    Standard Deviation: 28.9


    that is what my program should be displaying
     
  11. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #11
    But where is your code that outputs those results?

    I added it on this end where it outputs:

    Results using references
    Min: 1
    Max: 86.4
    Mean: 22.42
    Median: 8.7
    Standard Deviation: 0
     
  12. bravens52 thread starter macrumors regular

    Joined:
    Jul 16, 2009
    #12
    did u update the code?? it says it hasnt been updated this 9.36
     
  13. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #13
    Jhavel, I'm not doing your homework for you I am simply trying to lead you in the right direction.

    What I posted above was simply a minor correction to your code allowing it to compile. You haven't put anything in that ACTUALLY OUTPUTS your expected results.

    This question to you: where do you need to place code to output the result you've indicated you expect?
     
  14. bravens52 thread starter macrumors regular

    Joined:
    Jul 16, 2009
    #14
    im guessing i have to cout all of the variables such as mean median std dev etc in main() and then i can use set precision and set w to get the alignment correct?

    does that sound right?
     
  15. notjustjay macrumors 603

    notjustjay

    Joined:
    Sep 19, 2003
    Location:
    Canada, eh?
    #15
    Here's a hint: the program will only do exactly as it is told, no more, and no less.

    For example, if you want to see output on the screen that says:

    Hello, World!

    Then you had better have some code somewhere that prints the letters H, e, l, l, o, a comma, a space, etc., etc., etc.

    In other words, somewhere in your code there must be a string of text that says "Hello, World", and somewhere in your code there must be a command to print it out.

    Edit: OP posted the reply above while I was still typing this. Yes, you're on the right track now.
     
  16. bravens52 thread starter macrumors regular

    Joined:
    Jul 16, 2009
    #16
    Code:
    cout << "Results using references" << endl;
    	cout << "Minimum:" << min << endl;
    	cout << "Maximum:" << max << endl;
    	cout << "Mean:" << mean << endl;
    	cout << "Median:" << median << endl;
    	cout << "Standard Deviation:" << stDev << endl;
    	
        
    am i on the right track?

    thanks so much lloyd..that worked..i just need to get standard deviation to 28.9 (im guessing i messed up the calculations).

    thanks for not giving me the code..this is my first ever programming class so its still alittle to to me and overwhelming
     
  17. notjustjay macrumors 603

    notjustjay

    Joined:
    Sep 19, 2003
    Location:
    Canada, eh?
    #17
    Yup!

    Congratulations, and keep it up. Don't be afraid to try things out if you're not sure exactly how they work. Also, Google is great because you can find sample programs that do similar things, which can sometimes give you hints on how to do your particular application.
     
  18. bravens52 thread starter macrumors regular

    Joined:
    Jul 16, 2009
    #18
    yup thanks..its just so easy to give up on these programs because they can take forever and a day sometimes.
     
  19. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #19
    Stepped away for a few moments and you figured it out, congratulations!

    I'm still curious why you always wait till 3 hours before your homework is due to do it?
     
  20. bravens52 thread starter macrumors regular

    Joined:
    Jul 16, 2009
    #20
    this isnt due until sunday night. It's easter weekend and i wanted to get it finished so I didnt have to worry about it because of family functions.

    Im still not able to get my standard deviation calculating..could u perhaps help me lloyd?

    Code:
    stDev = sqrt((sumSq - (sqSum / size)) / (size - 1));
    	
    	for ( int i = 0; i < size; i++ )
        {
            sumSq += ar[i] * ar[i];
            sqSum += ar[i];
    		
    		sumSq *= sqSum;		
        }
    
    for some reason its saying zero and i dont know why..i even tried changing size to 15 which is the array element and i still am getting zero
     
  21. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #21
    Something is in the wrong order.

    Step through all the calculations that involve stDev. If stDev isn't involved, then what would its value be?
     
  22. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #22

    That's odd, here is says: "Assignment 8 Friday, April 2 in the Blackboard Assignment Dropbox by 11:59 PM"

    Anyway, as chown33 says, the answer is obvious if you step thru and check the order of calculations. Think calculation dependencies.
     
  23. notjustjay macrumors 603

    notjustjay

    Joined:
    Sep 19, 2003
    Location:
    Canada, eh?
    #23
    Remember, again, the computer is dumb, and does whatever you tell it, exactly when you tell it to.
     

Share This Page