Sorting Array then place each step in 2D HELP PLS!

Ive been stuggling with this question for a week now, I'd really appreciate if someone helped me figure out the mistake in my code:( Thanks in advance!

Write a program that uses a two dimensional array to show the steps
of sorting a set of numbers. The user should enter the size of the set
and save them in the first row of the array (Step 0 represents the
original set). In each step, the program will find the smallest value in
the list and place it in the right order.


Sample output:


How many values do you have? 6
Please enter the values:
2 1 4 3 6 5
Step 0: 2 1 4 3 6 5
Step 1: 1 2 4 3 6 5
Step 2: 1 2 4 3 6 5
Step 3: 1 2 3 4 6 5
Step 4: 1 2 3 4 6 5
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

int main()
{
	int num,hold;
	int Array[100][100];
	
	
	cout<<"How many values do you have?";
	cin>>num;
	
	cout<<"Please enter the values: ";
	
	for (int i=0;i<num;i++)
	cin>>Array[0][i];
	
	for (int i=0;i<num;i++)
	Array[1][i]=Array[0][i];
	
	
	for (int pass=1;pass<num;pass++)
	{
		
	
		for (int swap=0;swap<num-1;swap++)
		{
			
			if (Array[pass][swap+1]<Array[pass][swap])
			{
				hold=Array[pass][swap];
				Array[pass][swap]=Array[pass][swap+1];
				Array[pass][swap+1]=hold;
				
				
			}	
		}
		
		
		for (int i=0;i<num;i++)
		Array[pass+1][i]=Array[pass][i];
	}
	
	
	for (int row=0;row<num;row++)
	{
		cout<<" Step "<<row<<" :";
		
		for (int column=0;column<num;column++)
		cout<<Array[row][column]<<" ";
		
		cout<<endl;
	}
	
	return 0;
}
Try this:

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

int main()
{   int num,hold;
	int Array[100][100];
	int swaps = 0;
	int pass = 0; 
		
	cout<<"How many values do you have?";
	cin>>num;
	cout<<"Please enter the values: ";
	for (int i=0;i<num;i++)
	    cin>>Array[0][i];	
		
	do 
	{   pass++;
	    swaps = 0;
	    for (int i=0;i<num;i++)
		    Array[pass][i] = Array[pass-1][i];
        for (int swap=0;swap<num-1;swap++)
		{   if (Array[pass][swap+1]<Array[pass][swap])
			{   hold=Array[pass][swap];
				Array[pass][swap]=Array[pass][swap+1];
				Array[pass][swap+1]=hold;						
		        pass++;
		        swaps++;
		        for (int i=0;i<num;i++)
		            Array[pass][i] = Array[pass-1][i];
			}	
		}				
	}
	while (swaps);
	
	for (int row=0;row<pass;row++)
	{   cout<<" Step "<<row<<" :";
		for (int column=0;column<num;column++)
		    cout<<Array[row][column]<<" ";
		cout<<endl;
	}
	system ("pause");
	return 0;
}
How many values do you have?6
Please enter the values: 2 1 4 3 6 5
 Step 0 :2 1 4 3 6 5
 Step 1 :1 2 4 3 6 5
 Step 2 :1 2 3 4 6 5
 Step 3 :1 2 3 4 5 6
 Step 4 :1 2 3 4 5 6

Thanks alot! It worked! Thanks again!
Topic archived. No new replies allowed.