
January 30th, 2013, 05:25 AM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 1
Time spent in forums: 10 m 45 sec
Reputation Power: 0
|
|
|
Selection Sorter
Hey there,
I'm trying to write a program that takes ten values from the user and sorts them in ascending order.
I can't get the loop that actually sorts them into order to work.
It's been driving me crazy for 3 days.
Here is my source code(Hopefully you can understand what I'm trying to do):
#include <iostream>
using namespace std;
int main()
{
//Main declarations
int list[10];
int sort[10];
int inc = 0;
int min = 0;
//Function to receive values from user
cout << "Enter ten random integers to sort " << endl;
for(int cnt = 0; cnt < 10; ++cnt)
{
cout << "[" << cnt+1 << "]: ";
cin >> list[cnt];
}
cout << endl;
//Displaying unsorted list
cout << "Unsorted list = " << endl;
for(int cnt = 0; cnt < 10; ++cnt)
{
cout << list[cnt];
if(!(cnt == 9))
{
cout << ", ";
}
}
cout << endl;
//Finding minimum value
cout << "Sorting..." << endl;
for(int cnt = 0; cnt < 10; cnt++)
{
if(list[min] >= list[cnt])
{
min = cnt;
}
}
sort[0] = list[min];
inc++;
min = 0;
//Sorting remaining values in ascending order, using minimum as margin (here is the problem)
for(;inc < 10; inc++)
{
for(int cnt = 0; cnt < 10; cnt++)
{
if(sort[inc] <= list[cnt] && list[min] >= list[cnt])
{
min = cnt;
}
if(cnt == 10)
{
sort[inc] = list[min];
min = 0;
}
}
}
//Display sorted list (unfinished coding)
cout << "inc = " << inc << endl;
for(int cnt = 0; cnt < 10; cnt++)
{
cout << sort[cnt] << endl;
}
system("pause");
}
|