sorting array!

Hello there,
I'd appreciate you helping me figure out the mistake in my HW.
The question says:

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
Step 5: 1 2 3 4 5 6





I know how to swap and so on, but the problem is assigning each new row to the MODIFIED previous row!


Here's my code:

#include <iostream>
using namespace std;

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
 int main()
{
	int numberofvalues,hold,hold2;
	
	cout<<"How many values do you have?";
	cin>>numberofvalues;
	
     int Array[numberofvalues][numberofvalues];
	
	cout<<"Enter the values:"<<endl;
	
	for (int i=0;i<numberofvalues;i++)                      // filling the 1st row only.
     	cin>>Array[0][i];
	
	cout<<endl;
	
	
	for (int pass=0;pass<numberofvalues-1;pass++)                // in each pass we wanna fill the sorted array
	{
		
		for (int index=0;index<numberofvalues-1;index++)
		{
			
			if (Array[pass][index+1]<Array[pass][index])                    
			{                                                               
				hold= Array[pass][index+1];
				Array[pass][index+1]=Array[pass][index];
				Array[pass][index]=hold;	
			}
			
		}
			for(int i=0;i<numberofvalues;i++)
			{
				Array[pass+1][i]=Array[pass][i];
			}
		
	}

	for (int row=0;row<numberofvalues;row++)                                //after the array has been completely filled-- Print
	{
		cout<<"step "<<row<<" :";
		
		for (int column=0;column<numberofvalues;column++)
		cout<<Array[row][column]<<" ";
		
		cout<<endl;
	}
	

Last edited on
closed account (48T7M4Gy)
Could you help us to help you by tidying up your code by getting rid of all the blank lines, properly indenting the code and using code tags - <> in the toolbox on the right hand side, please . You can do this by editing you current post, you don't have to create a new post. Thanks :)
done!
Ps. Thank you for your help.
closed account (48T7M4Gy)
Well done, makes it easier. We'll have a look. :)
Last edited on
Line 8 is not valid C++. Array sizes should be compile-time constants.

Step 0 should be the original array unmodified. So line 8 18, should probably begin with pass equal to 1 and go 'til pass is equal to numberofvalues. It should begin with copying the previous row to the current row. Then do the sort pass through the current row.
Last edited on
closed account (48T7M4Gy)
What exactly is the plan that's driving your program? Did you prepare a pseudocode algorithm as a plan?

One way to do the sort is:
1. SETUP 2D ARRAY - made up of a row of input number and a second row the same length.
2. DISPLAY UNSORTED ARRAY
3. FOR EACH COLUMN
loop:
{
4. SEED SMALLEST WITH LARGE NUMBER (alternatively find largest and use that)
5. FIND SMALLEST IN FIRST ROW
6. TRANSFER SMALLEST TO NEXT POSITION IN SECOND ROW
}
7. DISPLAY SORTED ARRAY
Topic archived. No new replies allowed.