arranging randomly generated numbers in ascending order

This is a discussion on arranging randomly generated numbers in ascending order within the C++ Programming forums, part of the General Programming Boards category; i am attempting to write a program that will generate 6 random lotto numbers. i have been successful at generating ...

  1. #1
    Unregistered
    Guest

    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. #2
    junior member mix0matt's Avatar
    Join Date
    Aug 2001
    Posts
    144
    there's hundred of sorting algorithms on the internet. you can find one on google that will suit you...
    THIS IS NOT JUST A CHRONICLING OF THINGS WE HAVE DONE IN THE PAST BUT OUR RISE TO POWER.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting arrays (ascending order)
    By utoots in forum C++ Programming
    Replies: 3
    Last Post: 04-08-2003, 08:57 AM
  2. Help with listing numbers in order
    By boontune in forum C++ Programming
    Replies: 2
    Last Post: 03-23-2003, 06:56 PM
  3. Arranging numbers in ascending order
    By Aero in forum C++ Programming
    Replies: 7
    Last Post: 12-22-2001, 06:52 AM
  4. unique randomly generated numbers
    By grobe59 in forum C++ Programming
    Replies: 1
    Last Post: 11-07-2001, 07:26 PM
  5. display numbers in order
    By mike in forum C++ Programming
    Replies: 2
    Last Post: 09-24-2001, 09:16 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21