Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

bravens52

macrumors regular
Original poster
Jul 16, 2009
110
0
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;
		}
	}
 
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;
    }
}
 
the order you did was not how i learned it and i tried to compile your code and got 8 errors
 
okay i will switch up the code to look like mines and seee what happens.

thanks for the help as usual lloyd
 
the program is building but now its not displaying the output numbers..its just displaying results using preferences
 
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);
 
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
 
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
 
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

did u update the code?? it says it hasnt been updated this 9.36
 
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?
 
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?

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?
 
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.
 
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
 
am i on the right track?

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.
 
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.

yup thanks..its just so easy to give up on these programs because they can take forever and a day sometimes.
 
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

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

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

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


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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.