Array

This is what I am trying to do.

Assume an array has values {19, 13, 7, 12, 16}. Show the order of elements in the array after each pass of the selection-sort algorithm.

every time I go to compile it I get this message.
8 I:\CS352\week2\discussions\Untitled4.cpp variable-sized object `subValues' may not be initialized

not sure what it means but I do enitialize it where

 
int subValues[values] = {19, 13, 7, 12, 16};



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// This program stores numbers 19, 13, 7, 12, 16 in an int array
#include <iostream>
using namespace std;

int main()
{ 
    int 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 << "The sorted algorithm is:" <<endl;
    //cout << one <<" , " << two << " , " << three <<" , " << four <<" , " << five << endl;
    
    system("PAUSE");
    return EXIT_SUCCESS;
    
}
Last edited on
Change int values = 5; on line 7 to const int values = 5.

You can't dynamically allocate an array like that, if you didn't want values to be constant, you would need to do it like:
1
2
int values = 5;
int *subValues = new int[values];

And then set each value separately.
Last edited on
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
 
int subValues[] = {19, 13, 7, 12, 16};


EDIT: Darn it Intrexa posted that before me :-(
Last edited on
edit: false information!
Last edited on
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.
Thanks for the quick response:
Intrexa I am having a hard time understanding why it is that you would use a cont int value 5 ?
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:

int subValues[5] = {19, 13, 7, 12, 16};
Intrexa I got code to work now wondering how to show output I entered this
1
2
 cout << "The sorted algorithm is:" <<endl;
    cout << subVaues[count] << endl;
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.)
Misspelled subValues

><
LOL oopps sometimes the obvious is not so obious
still no go
1
2
 cout << "The sorted algorithm is:" <<endl;
    cout << subValues[count] << endl;



36 I:\CS352\week2\discussions\Untitled4.cpp name lookup of `count' changed for new ISO `for' scoping
Please post your entire program, you might have done something weird with the count variable.
closed account (D80DSL3A)
Two things:
1) On line 22 you need an opening brace {, not a closing brace }

2) You want to see the element values after each swap during the sort?
Insert this between lines 31 and 32:
1
2
3
4
cout << "After pass # " << count+1 <<  " the order is: ";
for( int i=0; i<values; ++i)
    cout << subValues[i] << " ";
cout << endl;
I added the text you said to add fun2code in and it said non-value in increment

here is what I have thus far
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// This program stores numbers 19, 13, 7, 12, 16 in an int array
#include <iostream>
using namespace std;

int main()
{ 
    const int 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; ++1)
           cout << subValues[i] << " ";
       cout << endl;
    }
       
    
    cout << "The sorted algorithm is:" <<endl;
    cout << subValues[count] << endl;
    
    system("PAUSE");
    return EXIT_SUCCESS;
    
}
Last edited on
closed account (D80DSL3A)
An error was made when you copied that code. On line 33 that should be ++i (the letter i) not ++1.
Man I can't believe I missed that..........ouch.

here is what I got and what the output was after I ran it
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// This program stores numbers 19, 13, 7, 12, 16 in an int array
#include <iostream>
using namespace std;

int main()
{ 
    const int 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 . . .

not sure where the 212168 comes from....

and why pass does it every other number
Last edited on
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.
Last edited on
That worked!!
Thanks for all the help...... wish I was as good at this as everyone here still trying to get use to it.
Topic archived. No new replies allowed.