954,504 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Please Help!!

Hi I'm a new user of c++ and our professor gave us a homework problem where we have to find the standrad deviation, the min, the max and the avg of an array of numbers, here is what I have so far but now I'm really stuck trying to make the algorithm work for the min and max and avg.

#include <iostream>
#include <cmath>
using namespace std;

double mma (double Nums[], int inSize, double max[], double min[]);

double std_Dev ( double Nums[], int inSize );

double avg = 0, sDev, sum = 0, sum2 = 0;

int main ( ){
	int inSize;
    double Nums[100];

	cout << "How many Numbers would you like to work with (max 100):";
	cin >> inSize;

	cout << "\nInput the numbers hit enter after each entry:";
	for ( int i = 0; i < inSize; i++ ){
		cin >> Nums[i];
	}

	sDev = std_Dev ( Nums, inSize );
	mma ( Nums, inSize );

	cout <<"The max, the min and the average is"<<
    cout << "The Standard deviation is "<< sDev<<endl;
	

	return 0;
}

double mma (double Nums[], int inSize, double max[], double min[]){
    for ( int i = 0; i < inSize; i++ ){
        if (inSize>max[i]){max[i]=inSize;}
        if (inSize<min[i]){min[i]=inSize;}
    	sum += Nums[i];
	    }
    avg = sum / inSize;
    
}
    
double std_Dev ( double Nums[], int inSize ){
	for ( int i = 0; i < inSize; i++ ){
		sum += Nums[i];
	}
    avg = sum / inSize;

	for ( int i = 0; i < inSize; i++ ){
		sum2 += pow( Nums[i] - avg, 2 );
	}

	sDev = sqrt( sum2 / ( inSize - 1 ) );

	return sDev;
}

Thank you for all your help!

sonicstage
Newbie Poster
7 posts since Jan 2009
Reputation Points: 10
Solved Threads: 0
 

Code tags:


[code]
// paste code here
[/code]

#include <iostream>
#include <cmath>
using namespace std;

double mma (double Nums[], int inSize, double max[], double min[]);

double std_Dev ( double Nums[], int inSize );

double avg = 0, sDev, sum = 0, sum2 = 0;

int main ( ){
int inSize;
double Nums[100];

cout << "How many Numbers would you like to work with (max 100):";
cin >> inSize;

cout << "\nInput the numbers hit enter after each entry:";
for ( int i = 0; i < inSize; i++ ){
cin >> Nums[i];
}

sDev = std_Dev ( Nums, inSize );
mma ( Nums, inSize );

cout <<"The max, the min and the average is"<<
cout << "The Standard deviation is "<< sDev<<endl;


return 0;
}

double mma (double Nums[], int inSize, double max[], double min[]){
for ( int i = 0; i < inSize; i++ ){
if (inSize>max[i]){max[i]=inSize;}
if (inSize<min[i]){min[i]=inSize;}
sum += Nums[i];
}
avg = sum / inSize;

}

double std_Dev ( double Nums[], int inSize ){
for ( int i = 0; i < inSize; i++ ){
sum += Nums[i];
}
avg = sum / inSize;

for ( int i = 0; i < inSize; i++ ){
sum2 += pow( Nums[i] - avg, 2 );
}

sDev = sqrt( sum2 / ( inSize - 1 ) );

return sDev;
}


Here's your code in code tags, but you either didn't format it or the formatting got stripped. Here it is formatted. As you can see, it's much easier to read:

#include <iostream>
#include <cmath>
using namespace std;

double mma(double Nums[], int inSize, double max[], double min[]);

double std_Dev(double Nums[], int inSize);

double avg = 0, sDev, sum = 0, sum2 = 0;

int main()
{
  int inSize;
  double Nums[100];

  cout << "How many Numbers would you like to work with (max 100):";
  cin >> inSize;

  cout << "\nInput the numbers hit enter after each entry:";
  for (int i = 0; i < inSize; i++)
  {
    cin >> Nums[i];
  }

  sDev = std_Dev(Nums, inSize);
  mma(Nums, inSize);

  cout << "The max, the min and the average is" << cout << 
    "The Standard deviation is " << sDev << endl;


  return 0;
}

double mma(double Nums[], int inSize, double max[], double min[])
{
  for (int i = 0; i < inSize; i++)
  {
    if (inSize > max[i])
    {
      max[i] = inSize;
    }
    if (inSize < min[i])
    {
      min[i] = inSize;
    }
    sum += Nums[i];
  }
  avg = sum / inSize;

}

double std_Dev(double Nums[], int inSize)
{
  for (int i = 0; i < inSize; i++)
  {
    sum += Nums[i];
  }
  avg = sum / inSize;

  for (int i = 0; i < inSize; i++)
  {
    sum2 += pow(Nums[i] - avg, 2);
  }

  sDev = sqrt(sum2 / (inSize - 1));

  return sDev;
}


What exactly is the problem? We need more details. Does it compile? Does it run? Does it give the wrong answers? What is the input? What is the correct output for that input? What is the actual output?

VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
 

One thing that caught my eye is line 26: mma(Nums, inSize);
You're calling the function 'mma' with two arguments, but in your function definition you say: double mma(double Nums[], int inSize, double max[], double min[]); which are 4 arguments. Also you say that the function should return a 'double', but it never does. (perhaps you want a 'void' function?)

Nick Evan
Not a Llama
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 

Very sorry but I couldn't get the source of the problem. Did you compile it? did you run it? whare does the problem lie?

idb_study
Newbie Poster
7 posts since Dec 2008
Reputation Points: 10
Solved Threads: 0
 
Very sorry but I couldn't get the source of the problem. Did you compile it? did you run it? whare does the problem lie?

The post above your post, specifiesexactly where the problem lies

verruckt24
Posting Shark
952 posts since Nov 2008
Reputation Points: 485
Solved Threads: 89
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You