Standard Deviation C++ Program

In summary, the conversation discusses a program that is supposed to find the standard deviation of a set but is encountering an issue with the for loop in the main function. It is mentioned that no matter the size given to the array, the integer 'i' in the for loop goes to 2 and increments to a strange number. The conversation also touches on the compiler used and the concept of static vs dynamic allocation. The solution proposed is to either create a pointer or move the data declaration below where the number of elements is read.
  • #1
Mindscrape
1,861
1
I wrote a program that is supposed to find the standard deviation of a set, but something really strange happens in a for loop that I have in my main function.

int main()
{
int i=1;
int NUMBER_OF_ELEMENTS;
double data[NUMBER_OF_ELEMENTS];

cout << "Please enter the number of elements followed by each individual data element:" << endl;
cin >> NUMBER_OF_ELEMENTS;

for(i=1;i<=NUMBER_OF_ELEMENTS;i++)
{
cout << "Data element " << i << ": ";
cin >> data[i-1];
cout << endl;
}


cout << "The standard deviation is: " << std_dev(data, NUMBER_OF_ELEMENTS);
return EXIT_SUCCESS;
}

No matter what size I give to the array (for the number_of_elements), the integer, 'i,' in the for loop will go to 2, and the next time through the loop 'i' increments to some wacky number (i.e. 1432104).

Can anyone pick out what would cause something strange like this?
 
Physics news on Phys.org
  • #2
what compiler did you use to? does it actually compile?
do you know anything about "static vs dynamic allocation"?
and what do you expect these two lines to do:

int NUMBER_OF_ELEMENTS;
double data[NUMBER_OF_ELEMENTS];
 
  • #3
I think I see what you are getting at. The data declaration should be below where the number of elements is read?
 
  • #4
Think about this: how would the compiler know how big to make the array?
 
  • #5
So I could either make a pointer or just move the declaration.

Awesome, thanks.
 

Related to Standard Deviation C++ Program

1. What is standard deviation in a C++ program?

Standard deviation in a C++ program is a statistical measure that describes how spread out a set of data is from its mean (average) value. It is commonly used to measure the variability or dispersion of a set of data.

2. How is standard deviation calculated in a C++ program?

The formula for calculating standard deviation in a C++ program is:
standard deviation = square root of (sum of (each data point - mean)^2 divided by total number of data points)

3. Why is standard deviation important in a C++ program?

Standard deviation is important in a C++ program because it allows us to understand the spread of data and the degree of variation from the mean. It also helps in identifying outliers and determining the reliability of the data.

4. Can you provide an example of a C++ program to calculate standard deviation?

Yes, here is a simple C++ program that calculates the standard deviation of a set of data:

```#include #include using namespace std; float calculateSD(float data[], int n) { float sum = 0.0, mean, SD = 0.0; for (int i = 0; i < n; i++) { sum += data[i]; } mean = sum / n; for (int i = 0; i < n; i++) { SD += pow(data[i] - mean, 2); } return sqrt(SD / n); } int main() { float data[] = { 2.5, 3.8, 4.2, 5.0, 6.3 }; int n = sizeof(data) / sizeof(data[0]); cout << "Standard Deviation = " << calculateSD(data, n); return 0; } ```

5. Are there any built-in functions in C++ to calculate standard deviation?

Yes, the cmath library in C++ provides a built-in function std::sqrt() that can be used to calculate the square root of a number, which is required in the formula for standard deviation. Additionally, there are other libraries such as statistics and boost that have built-in functions for calculating standard deviation in C++.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
24
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
616
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Programming and Computer Science
Replies
12
Views
1K
Back
Top