Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,570 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,661 people online right now. Registration is fast and FREE... Join Now!




Array problems in program trying to calculate standard deviation

 
Reply to this topicStart new topic

Array problems in program trying to calculate standard deviation

inf4my
25 Nov, 2007 - 09:21 PM
Post #1

New D.I.C Head
*

Joined: 19 Nov, 2007
Posts: 23


My Contributions
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
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.
User is offlineProfile CardPM
+Quote Post

jjhaag
RE: Array Problems In Program Trying To Calculate Standard Deviation
25 Nov, 2007 - 10:04 PM
Post #2

me editor am smartastic
Group Icon

Joined: 18 Sep, 2007
Posts: 1,789



Thanked: 2 times
Dream Kudos: 775
Expert In: C,C++

My Contributions
Different standards have different things to say about using varaible length arrays. I'm not sure what standard VS 2005 is built to meet, but C99 (C only) allows them. I believe that they are not allowed by the most recent C++ standard. I'm sure that someone more familiar with the standard will be able to answer this, but I'll do some more digging in a bit.

Until then, have no fear, you can still dynamically allocate your array. You will need to use the new operator, and allocate space pointed to by a double*:
CODE
double* Nums=new double[inSize];

This replaces the line where you declare your array of doubles after the user inputs the number of elements that they are going to enter. You will still access the array in the usual way, so the rest of your program can remain unchanged (assuming it's all correct in the first place).

Hope that helps,

-jjh
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/7/09 10:18PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month