Convert into C++

Help me convert this into c++

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
Length ← length (List); Max, Location, Value, Stack1, Stack2;
For i ← Length -1 to 1 do
  Max ← i;
  For j ← i - 1 to 0 do
    if( List[ j ] > List[ Max ] )
       Max ← j
       Push Max in Stack1
       Value ← List [Max]
       Push Value in Stack 2
    End if
  End for
  Swap (List[ i ], List[Max] );
  Pop the first element from both the stack //this element already has been  swapped

  While (Stacks are not empty && i > 0)
    i ← i - 1
    Location ← pop element from Stack1, Max ← Location
    Value ← pop element from Stack2.
    Swapped ← false
    For n ← Location - 1 to 0 && i - 1 do
      Swapped ← true;
      if ( List[ n ] > Value )
        Max ← n
        Push Max in Stack1
        Value ← List [Max]
        Push Value in Stack 2
      End if
    End for

    If (Swapped )
       Swap (List[ i ], List[Max] )
       Pop the first element from both the stack
    Else
       i ← i + 1
  End while // Stack count loop
End for // outer for loop 
Last edited on
We aren't a homework service. From the other thread, it appears that you don't know how to convert a single for loop into C++.

Here's a tutorial page on C++ from this site: http://www.cplusplus.com/doc/tutorial/

Try to convert it to C++ yourself, and then post what you have so far when you can't get further. That's when we can help you learn the most.

Also, please edit your post and future posts and add code formatting,
[code]
    {Paste Code Here}
[/code]
Last edited on
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
#include <stdio.h>
#include <stack>

using namespace std;

void swap(int *xp, int *yp)
{
    int temp = *xp;
    *xp = *yp;
    *yp = temp;
}

int arr[] ={20, 15, 8, 10,	5, 7, 6, 2,	9,	1}, Max, Location, Value;

void Selection(int arr[], int n)
{
    stack<int> Stack1, Stack2;
    int i, j;

    for (i = 0; i > n+1; i--)
    {
        Max = i;
        for(j = i-1; j > n;  j-- )
            if(arr[j] > arr[Max])
            Max = j;
            Stack1.push(Max);
            Value = arr[Max];
            Stack2.push(Value);

            swap(&arr[i], &arr[Max]);
    }
    Stack1.pop();
    Stack2.pop();

    while(Stack1.empty() && Stack2.empty() && i > 0)
        i = i - 1;
        Location = Stack1.top();
        Max = Location;
        Value = Stack2.top();

        for (n = Location; n > i+1; n-- && i-- )
        {
            if(arr[n] > Value)
                Max = n;
                Stack1.push(Max);
                Value = arr[Max];
                Stack2.push(Value);
        }



}

I stuck at swapped coding and with the loop coding
What do you mean by "swapped coding"? And which loop?

Notice that your for loop in line 23 only includes lines 24 and 25. If you don't use {}, the for loop will only contain a single statement. Same with an if expression. So, you if in line 24 only applies to line 25. And your for loop in line 23 only applies to the statement in lines 24 and 25. Similar in lines 35 and 43.

Your swap function in 6 looks fine. I would use references rather than pointers to simplify things a little bit. Also, the std::swap function in <utility> (or <algorithm if you you are using c++98) will do this for you.

Last edited on
Well pretty much every if / for / End if / End for in your pseudo code need corresponding { } brace pairs in your C++ code.

1
2
3
4
5
6
7
8
        for(j = i-1; j > n;  j-- )
            if(arr[j] > arr[Max])
            Max = j;
            Stack1.push(Max);
            Value = arr[Max];
            Stack2.push(Value);

            swap(&arr[i], &arr[Max]);


What you wrote is
1
2
3
4
5
6
7
8
9
        for(j = i-1; j > n;  j-- )
            if(arr[j] > arr[Max])
                Max = j;

        // These have nothing to do with your loop, or your conditional.
        Stack1.push(Max);
        Value = arr[Max];
        Stack2.push(Value);
        swap(&arr[i], &arr[Max]);


Topic archived. No new replies allowed.