Objective: I have two arrays. 1st Array is: evenList and the 2nd is: oddList. I want to print the even and odd numbers between the given ranges in this format.
This is my code,
cout << "\nEven numbers between " << lowerLimit << " to " << upperLimit << ": ";
for(int i; i < evenList.size(); i++){
cout << evenList[i] << " ";
}
cout << "\n\nOdd numbers between " << lowerLimit << " to " << upperLimit << ": ";
for(int j; j < oddList.size(); j++){
cout << oddList[j] << " ";
}
the first for loop prints the desired output but the second loop don't show the odd numbers. Here is the output:
I've read some of contents about for loops already but I just can't get the answer. If someone facing the same question or problem and got the answer, please share. I will really appreciate it.
You're not initializing your iterators. int i;
should be int i = 0;
, and likewise for int j
. As it is now, this is undefined behavior. It's just chance that it worked the first time and not the second time, it might as well work both times, not work at all, crash right off the bat or do something entirely different.
Does your compiler emit a warning for this code? Ideally, it should say something like "uninitialized local variable 'i' used". Always listen to compiler warnings, they can help point out some common mistakes. If your compiler issues no warning here, try to see if you can configure it to be more strict with warnings.