Write a program that generates 10 (TEN) random integer numbers that range from 1 to 50,
and store them in an array variable. Display the original order of random integer numbers.
Then create a function that sorts the number elements from the array in ascending order and
display the result.

The sample output screen is as the following:
Random numbers generated: 8 46 17 3 23 8 13 33 20 42
After sorting: 3 8 8 13 17 20 23 33 42 46

(I stop in half way with no direction how to continue on it :( )

## All 2 Replies

``````#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
void sorting[10]
int main()
{
intrandnum[10];
srand(time(NULL)); //to generate different sets of random numbers
for(int i=0; i<10; i++)
{
randnum[i]=1+rand()%50;
cout<<randnum[i]<<" ";
}
cout<<endl;
return 0;
}``````

Here is a good place to start looking at sorting algorithms.
Being new, you'll want to study the Bubble Sort and Insertion Sort algorithms as they are the simplest to learn.

Not sure if it will help or not, but here is an example Bubble Sort (in FreeBASIC):

``````#define FALSE 0
#define TRUE (Not FALSE)

dim passCount as integer = 0
dim swaps as byte = TRUE

'display the original array
print "Using the modified bubble sort algorithm"
print "Original array:"
for index as integer = LBound(myArray) to UBound(myArray) step 1
print using "myArray(##) "; index;
print using "= ####"; myArray(index)
next index
sleep 1000

'execute the bubble sort
while (swaps)
swaps = FALSE       'set the swaps flag
passCount += 1      'add 1 to pass count for each while iteration
for index as integer = LBound(myArray) to UBound(myArray)-passCount step 1          'execute a sorting pass
if (myArray(index) < myArray(index+1)) then 'REVERSE TO SORT ASCENDING
swap myArray(index), myArray(index+1)   'swap values if necessary
swaps = TRUE
end if
next index
wend    'end while loop

'display the sorted array
cls
print "After modified bubble sort:"
print "Sorted array:"
for index as integer = LBound(myArray) to UBound(myArray) step 1
print using "myArray(##) "; index;
print using "= ####"; myArray(index)
next index

sleep
end``````

Here is an insertion sort (again, in FreeBASIC):

``````'execute the insertion sort
for startIndex as integer = LBound(myArray)+1 to Ubound(myArray)  'establish starting index of each pass
for index as integer = startIndex to LBound(myArray)+1 step -1
if (myArray(index) > myArray(index-1)) then 'REVERSE TO SORT ASCENDING
swap myArray(index), myArray(index-1)
else
exit for
end if
next index
next startIndex``````
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.