Unless you're allocating memory for arrays dynamically, their size needs to be a constant. Use the const modifier for values and you should be good. If that doesn't fix it try to define subValues as
Ashishduh that won't fix it, you're still initalizing the array with a non-constant value. All you're doing is initializing the variables over 5 lines, it's the same thing.
As an aside, C99 allows variable sized array like this, and the gcc compiler(s) has an extension to allow them in C90 and C++. For the keen, you can use the -pedantic switch to ensure you are warned if you use a non-standard extension such as this in your code.
Because when you change 'value' to const, the array goes from being dynamically sized to being a constant size. Then you can initialize it like you did. In other words, the reason the compiler doesn't like the way you're doing it is because 'value' can change. This way it can't. It's like saying:
but got this: 36 I:\CS352\week2\discussions\Untitled4.cpp `subVaues' undeclared (first use this function)
(Each undeclared identifier is reported only once for each function it appears in.)
// This program stores numbers 19, 13, 7, 12, 16 in an int array
#include <iostream>
usingnamespace std;
int main()
{
constint values = 5;
int subValues[values] = {19, 13, 7, 12, 16};
// I could just as easily make the person input these values but for the sake of
// specificity I will enter them.
// Display the contents of the array
cout << "The numbers entered were:" << endl;
cout << " " << subValues[0] << endl;
cout << " " << subValues[1] << endl;
cout << " " << subValues[2] << endl;
cout << " " << subValues[3] << endl;
cout << " " << subValues[4] << endl;
for (int count = 0; count < values; count++)
{
int smallCount = count;
for (int currentCount = count ++ ; currentCount < values; currentCount ++)
{
if (subValues[currentCount] < subValues[smallCount])
smallCount = currentCount;
}
swap(subValues[count], subValues[smallCount]);
cout << "After pass #" << count + 1 << " the order is: ";
for ( int i=0; i < values; ++i)
cout << subValues[i] << " ";
cout << endl;
}
system("PAUSE");
return EXIT_SUCCESS;
}
here is the output I recieved look to me like the compiler is reading ASCII code for the number and not the actual int
The numbers entered were:
19
13
7
12
16
After pass #2 the order is: 19 7 13 12 16
After pass #4 the order is: 19 7 13 12 16
After pass #6 the order is: 19 7 13 12 212168
Press any key to continue . . .
It is reading subValues[6], which is undefined, which is giving you that garbage value of 212168. You are incrementing count on line 21, and again on line 25. That is why your passes are growing in increments of 2. Changing line 25 to for (int currentCount = count ; currentCount < values; currentCount ++) fixes the problem.