Logic Problems? Need help.

Greetings programmers. I hope to someday be able to include myself among the ranks of those of you who are going to probably look at this code and see the issue in 10 seconds. Although please note that i have stared at it for hours, can't find a similar problem and it also works the other way around. Keep in mind also that this is an example exercise from a book and the solution was supposed to be quite simple. I'm pretty far past this part of the book now, and have written numerous small programs since, but I just cannot let this go.

Basically, all this program does is create an array (myarray) and give each instance of the array a number 0-9 (arbitrarily) purposely making them not in any type of order. It then calls a function to sort the array from highest to lowest.

The code in the book sorted the array from lowest to highest, I copied this code and it works fine. Theoretically it should be cake to reverse it. Here is the code from the book that is working.

please read on as i have listed my revised code and the odd issues i'm having. Sorry this is so long, I have solved so many logic issues but I literally have no more ideas, I made an account just to post this and will now hopefully be an active member!!


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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
 "book version - sorts array lowest to highest."
#include <iostream>

using namespace std;

void swapper(int *x, int *y);
void sorter(int n);
int myarray[10] = {7, 5, 3, 8, 4, 6, 9, 0, 2, 1};

int main()
{


   //print all current array values
   for(int c = 0; c < 10; c++)
   {
       cout << "myarray [" << c << "] = " << myarray[c] << endl;
   }


   cout << endl << endl;

    sorter(10);

   //reprint after sorter function, hopefully in order.
   for(int c = 0; c < 10; c++)
   {
       cout << "myarray [" << c << "] = " << myarray[c] << endl;
   }

    return 0;
}

//used to swap two values in the array via pointers
void swapper(int *x, int *y)
{
    int temp;

    temp = *x;
    *x = *y;
    *y =temp;

}


//used to sort the array via nested loops. The outermost loop counts from the 0 //spot to the 9th and runs the next loop for each one
//the inner loop compares the array slot with the "current highest" setting a //new high if necessary and then switching their places
//void sorter(int n)
{
    int c, curr, low;

    for(c = 0; c < n - 1; c++)
    {
        low = c;

        for(curr = c; curr < n; curr++)
        {
            if(myarray[curr] < myarray[low])
            {
                low = curr;
            }
            if(c != low)
            {
                swapper(&myarray[c], &myarray[low]);
            }
        }
    }

}

 


I had thought (as hinted by the book that you only need to change a few signs around to get this to work backwards but I was very wrong. I have even manually followed the variables through the loops with a pen and paper and it works differently than the result I'm seeing. In my version of the excercise I have numerous sections of code to print all the variables so that I can look through and see where it goes wrong, i've found it but I don't know why its happening.

here is a picture of the console window with the part in question in the red box. followed by my code, with comments, hopefully they help, as I said there is a bunch of extra code to print all the values so I could try to track down the issue.

http://i.imgur.com/tenDlJG.jpg

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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
 "my version - with debug code"

#include <iostream>

using namespace std;

void swapper(int *x, int *y);
void sorter(int n);
int myarray[10] = {7, 5, 3, 8, 4, 6, 9, 1, 2, 1};

int main()
{


   //print all current array values
   for(int c = 0; c < 10; c++)
   {
       cout << "b4: myarray [" << c << "] = " << myarray[c] << endl;
   }


   cout << endl << endl;

    sorter(10);

   //reprint after sorter function, hopefully in order.
   for(int c = 0; c < 10; c++)
   {
       cout << "af: myarray [" << c << "] = " << myarray[c] << endl;
   }

    return 0;
}


void swapper(int *x, int *y)
{
    int temp;

    temp = *x;
    *x = *y;
    *y = temp;


}

void sorter(int n)
{
    int c, curr, high;

    for(c = 0; c < n - 1; c++)
    {
        high = c;

        for(int c2 = 0; c2 < 10; c2++)
        {
            cout << "in: myarray [" << c2 << "] = " << myarray[c2] << endl ;
        }

        cout << endl;


        for(curr = c; curr < n; curr++)
        {

//this next line just prints the values before the comparative if statement in an effort to track down the problem.
            cout << "BEFORE:: c = " << c << " curr = " << myarray[curr] << " high = " << myarray[high] << endl;

            if(myarray[curr] > myarray[high])
            {
                high = curr;
            }

//this next line just prints the values after the comparative if statement in an effort to track down the problem.
            cout << "AFTER:: c = " << c << " curr = " << curr << " high = " << high << endl;

            if(c != high)
            {
                swapper(&myarray[c], &myarray[high]);

//this line prints the exact values swapped which helps to show when this function is actually called and what values it swaps. 
                cout << "Swapped myarray[c]: " << myarray[c] << "(" << c << ")" << " and myarray[high]: " << myarray[high] << "(" << high << ")" << endl;

            }
        }
    }

}
Do the swap in the outer loop.
I need to change my name to Abysmal Reg.

Thanks for the help!!

How come it works the other way though? the reason i missed this is because it works fine in the first program!! I'm glad it works and clearly see why but is it just a coincidence that the other one works?
Nevermind, I already figured it out. I'm more in awe now of how you figured it out so fast with my post being so long.

Also it turns out it was just a coincidence that the other one worked right, you can input a sequence of numbers that 'breaks' it. thanks again for the help.
Topic archived. No new replies allowed.