Why Does My C++ For Loop Increment Erratically?

Click For Summary

Discussion Overview

The discussion revolves around a C++ programming issue related to an erratic increment of a loop variable in a for loop, specifically in the context of calculating the standard deviation of a data set. The focus is on understanding the implications of variable declarations and memory allocation in C++.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant describes a problem with a for loop where the loop variable 'i' increments to unexpected values, suggesting a potential issue with how the array is declared.
  • Another participant questions the compiler used and whether the code compiles, hinting at the concepts of static versus dynamic allocation.
  • A participant proposes that the declaration of the data array should occur after reading the number of elements to avoid undefined behavior.
  • Another participant emphasizes the need for the compiler to know the size of the array at compile time, raising concerns about the original code structure.
  • One participant suggests alternatives, such as using a pointer or moving the declaration of the array, as potential solutions.

Areas of Agreement / Disagreement

Participants express a general consensus that the order of declarations is problematic, but there is no explicit agreement on the best solution or approach to resolve the issue.

Contextual Notes

The discussion highlights limitations related to variable scope, memory allocation, and the timing of array declarations, which are not fully resolved within the thread.

Who May Find This Useful

Readers interested in C++ programming, particularly those dealing with array declarations, memory management, and debugging loop behavior.

Mindscrape
Messages
1,854
Reaction score
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];

count << "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++)
{
count << "Data element " << i << ": ";
cin >> data[i-1];
count << endl;
}


count << "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
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];
 
I think I see what you are getting at. The data declaration should be below where the number of elements is read?
 
Think about this: how would the compiler know how big to make the array?
 
So I could either make a pointer or just move the declaration.

Awesome, thanks.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
3K
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 24 ·
Replies
24
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 23 ·
Replies
23
Views
3K