The program calculates the standard deviation by having the user input the list of numbers into an array.
So i get these errors on the program
error C2057: expected constant expression
error C2466: cannot allocate an array of constant size 0
error C2133: 'Nums' : unknown size

here is the code

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

double std_Dev (double Nums[], int inSize, double &avg, double &sum, double &sum2);

int main ( ){
	int inSize;
	double avg = 0, sDev, sum = 0, sum2 = 0;
	cout << "How many Numbers would you like to find the standard deviation for (max 100):";
	cin >> inSize;

	double Nums[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, avg, sum, sum2);

	cout << sum<<" "<<inSize<<" "<<avg<<" "<<sDev<<endl;

	return 0;
}

double std_Dev (double Nums[], int inSize, double &avg, double &sum, double &sum2){
	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 );
	}

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

	return sDev;
}

works in gcc but for some reason doesnt in VS 2005 which is what I need dumb class guidelines.

error C2057: expected constant expression
error C2466: cannot allocate an array of constant size 0

cin >> inSize;
double Nums[inSize];

You can't declare an array with a variable size. Use a const int instead, or use new and delete .
This should have never compiled in gcc.. Are you sure about that?

Niek

> This should have never compiled in gcc ...
true, it should never compile without errors in any c++ compiler.
but g++ does accept it without specific switches that turn on C++ compliance.
eg. > g++ --std=c++98 --pedantic -c whatever.cc
or > g++ --ansi --pedantic -c whatever.cc
would generate the error.

I just changed it to this seems to work fine, i just initialized the array with a size of 100

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

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 find the standard deviation for (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 );

	cout << "The Standard deviation is "<< sDev<<endl;

	return 0;
}

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

True, this will work, but make sure that the user didn't input anything greater than 100 or else you will get horrible crashes.

if (inSize > 100) 
{
    cout << "try again (<100)" << endl;
    cin >> inSize;
}
else etc

Niek

This article has been dead for over six months. Start a new discussion instead.