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
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;
}
#include <iostream>
usingnamespace 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