hello. here i go again...

were learning Arrays this week, and after a lot of trial/error, ive finally created a program that actually runs and compiles.

my issue of the matter now is being able to put the user's 4 numerical inputs in order from low to high.

if anyone can take a look at my program below and kindly suggest what i could possibly be missing. thank you. =)

``````#include <fstream>
#include <iostream>
using namespace std;

int main()
{
// allows user inputs
const int SIZE = 4;
int a[SIZE];
int i;
for (i = 0; i < SIZE; i++)
{
cout << "Enter score a[" << i << "]: ";
cin >> a[i];
cin.ignore(1000, 10);
}

int size;

for (i = 0; i < SIZE; i++)
cout << a[i] << ' ';
cout << endl;

cin >> size;
cin.ignore(1000, 10);
int* score = new int[size];

delete [] score;

cin.ignore();
cin.get();
return 0;
}``````
7
Contributors
10
Replies
11
Views
8 Years
Discussion Span
Last Post by xiikryssiix

There are a multitude of sorting algorithms!
You aren't dealing with unsorted data, so you can do an insertion sort so as you enter a new number, you find its position in the list, stretch the list and insert it.

If dealing with an array of data, the slowest but simplest solution is a bubble sort. This is essentially a double loop. Outer loop sets a nonsort flag, and the inner loop compares adjacent array cells. If it finds a sort problem, it sets the nonsort flag to sort, and swaps the two, then continues down the array in the inner loop. it exits, finds the sort flag set then repeats the entire operation again. If the flag stays cleared because nothing was swapped then you are done.

Another method would be to set the base index to zero. Scan for the lowest item, and swap it with the item at index 0. Then advance the index to slot 1. Repeat, until you reach the last slot which will be in its correct position.

There are many many other sorts. You'll find them on-line and when you get a chance by the book set from Knuth, which is a very good resource on this topic.

lol thank you for the help WildGoose, but i am in super beginners class of C++; i have no idea what bubble loops are!

do you have a sample code of a simple algorithmn? we just learned Arrays this week and its already killing me!

Sorry can't do that.
Do this in two steps. Between your data entry loop and your data print loop.

for i = 0 to N - 1;
if ary > ary[i+1] then swap those two!
loop until i is N - 2 so that i + 1 won't equal N or you'll be writing past the end of the array.

ary[10] uses 0...9 to access it!
------
Once you do that then put an outer loop.

Outer loop <do>
flag = false;

Inner Loop if a swap then flag = true
end do-while( flag is true )

Bubble sort is the simplest sort algorithm out there. Google "bubble sort", "bubble sort c++", "bubble sort c++ sample code", stuff like that, and you'll get plenty of sample code, all based on arrays.

Here's a bunch of applets I made myself explaining the different sorts. I really need to put a splash page in there, but be patient and they'll load.

No code in these, but they give a decent visual representation of what's going on.

you can you selection sort as well. Since the purpose is array;selection sort is just as good.
You can get all sorting methods from the web.just google them up!

I know that you're just starting out and doing these things as exercises, but if you want a simple and practical way to do this, then I'd use an STL vector instead of a C-style array. You need to have `#include <vector>` and `#include <algorithm>` at the top of your code. Then you can simply do something of the form

``````vector my_vec;
my_vec.resize(SIZE);
for(int i = 0; i < SIZE; i++)
my_vec.at(i) = SIZE - i;
sort(my_vec.begin(), my_vec.end());``````

This should result in a sorted vector. If you need to sort arrays specifically, I tend to use the Gnu Scientific Library (GSL), which has functions of this type (among many others!)

Edit: STL vectors also have a number of additional benefits, such as flexibility and exception handling. Unless you really need an array for some reason, I'd be inclined to use them (as well as the other STL containers, such as map and list).

I think you should have used `vector<TYPE> my_vec` .

I think you should have used `vector<TYPE> my_vec` .

In the instance under discussion here, `vector <int> my_vec;` would be the ticket.