Thread: arranging randomly generated numbers in ascending order

1. arranging randomly generated numbers in ascending order

i am attempting to write a program that will generate 6 random lotto numbers. i have been successful at generating 6 random numbers and also checking that each number is unique. however, i would like to arrange these generated numbers in ascending order. i'me stumped at this point. any help would be appreciated.

my problem lies in void function setInOrder. i can sort the first two numbers in ascending order successfully but after that ???
thanks in advance!

here is my code:

#include <iostream.h>
#include <stdlib.h>
#include <time.h>

struct lottoNum
{
short num1;
short num2;
short num3;
short num4;
short num5;
short num6;
};

void loadArray(lottoNum[]);
void setUnique(lottoNum[]);
void setInOrder(lottoNum[]);

void main()
{
lottoNum numbers[10]={0, 0, 0, 0, 0, 0};

srand(time(NULL) );

loadArray(numbers);

setUnique(numbers);

setInOrder(numbers);

for(short z = 0;z <= 9;z = z + 1)
{
cout<< numbers[z].num1 <<" "<< numbers[z].num2 <<" "<<
numbers[z].num3 <<" "<< numbers[z].num4 <<" "<<
numbers[z].num5 <<" "<< numbers[z].num6 << endl;
}//end for
cout<< endl;

}//end main

void loadArray(lottoNum numbers[])
{
for(short y = 0;y <= 9;y = y + 1)
{
numbers[y].num1 = 1 + rand() % (52 - 1 + 1);
numbers[y].num2 = 1 + rand() % (52 - 1 + 1);
numbers[y].num3 = 1 + rand() % (52 - 1 + 1);
numbers[y].num4 = 1 + rand() % (52 - 1 + 1);
numbers[y].num5 = 1 + rand() % (52 - 1 + 1);
numbers[y].num6 = 1 + rand() % (52 - 1 + 1);
}//end for
}//end loadArray

void setUnique(lottoNum numbers[])
{
for(short x = 0;x <= 9;x = x + 1)
{
if(numbers[x].num2 == numbers[x].num1 )
{
numbers[x].num2 = 1 + rand() % (52 - 1 + 1);
}
else
if(numbers[x].num3 == numbers[x].num2 ||
numbers[x].num3 == numbers[x].num1)
{
numbers[x].num3 = 1 + rand() % (52 - 1 + 1);
}
else
if(numbers[x].num4 == numbers[x].num3 ||
numbers[x].num4 == numbers[x].num2 ||
numbers[x].num4 == numbers[x].num1)
{
numbers[x].num4 = 1 + rand() % (52 - 1 + 1);
}
else
if(numbers[x].num5 == numbers[x].num4 ||
numbers[x].num5 == numbers[x].num3 ||
numbers[x].num5 == numbers[x].num2 ||
numbers[x].num5 == numbers[x].num1)
{
numbers[x].num5 = 1 + rand() % (52 - 1 + 1);
}
else
if(numbers[x].num6 == numbers[x].num5 ||
numbers[x].num6 == numbers[x].num4 ||
numbers[x].num6 == numbers[x].num3 ||
numbers[x].num6 == numbers[x].num2 ||
numbers[x].num6 == numbers[x].num1)
{
numbers[x].num6 = 1 + rand() % (52 - 1 + 1);
}
}//end for
}//end setUnique

void setInOrder(lottoNum numbers[])////////this is my problem!
{
short temp = 0;

for(short w = 0;w <= 9;w = w + 1)
{
if(numbers[w].num2 < numbers[w].num1 )
{
temp = numbers[w].num1;
numbers[w].num1 = numbers[w].num2;
numbers[w].num2 = temp;
temp = 0;
}
}//end for
}//end setInOrder

2. there's hundred of sorting algorithms on the internet. you can find one on google that will suit you...

Popular pages Recent additions