Swapping Elements/values in Arrays

First, I was given a pseudocode to translate it into c++ language

Pseudocode

sortArray()
x = 0
didSwap = "YES"
while didSwap = "YES"
x = 0
didSwap = "NO"
if score[x] > score[x-1]
swap()
didSwap = "YES"
endif
x = x + 1
endwhile
return

swap()
temp = score[x-1]
score[x+1] = score[x]
score[x] = temp
return

Initial number I have is: 5,3,1,2,4

The output for this program should be: 1,2,3,4,5

Right now, my program seems to have two errors..I'm not exactly sure what's the problem since I have complete curly brackets???

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
  #include <iostream>
using namespace std;

void sortArray(int didSwap, int x);
void swap(int temp, int score[]);

int main()
{
	int score[] = {5, 3, 1, 2, 4};
	int temp;
	int didSwap;
	int x = 0;
	
	sortArray(didSwap, x)
	{ /// ====>> Error: expected ';' before '{' token.
		while(didSwap = "YES")
		{
			x = 0;
			didSwap = "NO";
			
			if(score[x] > score[x - 1])
			{
				swap()
				{
					temp = score[x - 1];
					score[x + 1] = score[x];
					score[x] = temp;		
				}
				didSwap = "YES";
			}
			else
		        {
			  x = x + 1;
			}
		}
	}

return 0;	
	
} // error expected '}' at end of input
Last edited on
http://www.cplusplus.com/doc/tutorial/functions/ (compare the example in Declaring functions)
1. You cannot define a function inside an another function. (There are exceptions, but you can ignore them for now.)

2. You need to read more about functions, their parameters, and about calling functions.
See http://www.cplusplus.com/doc/tutorial/functions/


3. A question about logic:
On line 21 you do compare two elements of an array. Indices: x and x-1
On the "swap" you look at three elements. Indices: x, x-1 and x+1

I can assure you that there is a logical error. Since it is already in the pseudocode, I must ask:
Is that a typo by the teacher, or by you?
I honestly just copied what my teacher gave to me and that was the pseudocode.

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

#include <iostream>
using namespace std;

void sortArray(int didSwap, int x)
void swap(int temp, int score[]) // ===>> now..I'm getting expected error initializer before 'void'

int main()
{
	int score[] = {5, 3, 1, 2, 4};
	int temp;
	int didSwap;
	int x = 0;
	sortArray(); // Added this part
	swap(); // Added this part
	
	sortArray(didSwap, x)
	{
		while(didSwap = "YES")
		{
			x = 0;
			didSwap = "NO";
			
			if(score[x] > score[x - 1])
			{
				swap()
				{
					temp = score[x - 1];
					score[x + 1] = score[x];
					score[x] = temp;		
				}
				didSwap = "YES";
			}
			else
		    {
			  x = x + 1;
			}
		}
	}

return 0;	
	
}
I think you should read the tutorial more carefully. Whole page.

Furthermore, the only change that I see is from
1
2
void sortArray(int didSwap, int x);
void swap(int temp, int score[]);

into
1
2
void sortArray(int didSwap, int x)
void swap(int temp, int score[])

Can you explain what you did there, and why?


If you are 100% certain that you have a verbatim copy of the pseudo code, then discuss its correctness with the teacher.
Topic archived. No new replies allowed.