Sorting variables

This is a discussion on Sorting variables within the C++ Programming forums, part of the General Programming Boards category; Greetings, folks. Firstly, great tutorials. I have been trying to make a break into C++ programming for a while, but ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    3

    Question Sorting variables

    Greetings, folks.

    Firstly, great tutorials. I have been trying to make a break into C++ programming for a while, but couldn't make the intellectual leap from my old (very old) Sinclair Spectrum Basic programming to C++. Very impressed.

    Ok, what I am trying to map out is how I would resolve the following quandry. Currently, I have no code written, and am trying to understand the process of what needs to happen before I get in there with my coding chainsaw.

    What I need to do is make a dice rolling program with a difference. Let's call the two sides A and B. The Dice themselves can be 6, 8, 10 or 12 sided, and there can be between 3 and 5 dice rolled, depending on defined starting criteria (I have that part worked out, I think). The issue I am having is how to have the code then sort the results, highest A roll vs highest B roll. 2nd highest A roll vs 2nd highest B roll, etc.

    If, as an example, A rolls 3 10 7 6 and B rolls 8 1 6 8 4, I would want the code to compare A's 10 with B's 8, then A's 7 with B's 8, then A's 6 with B's 6....etc.

    This is for a board game that I play, and writing a battle resolution program seems like a good idea

    I'm not asking for the code, as there is an intellectual challenge in writing it, but I really need help in formulating how I would attack getting the code to sort the rolls into highest-to-lowest so that I can compare them, presumably by assigning the results as variables that I can directly compare with (if).

    Thanks in advance, folks.

    Andrew Campbell.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    The C++ STL (Standard Template Library) container classes and functions are your friend. You can use a container class to store various types of objects (plain-old-data types like ints, floats, etc. or even user defined objects like classes or structs) and then call functions that will sort them automatically for you.

    You could for example, store the dice rolls from the two "sides" into two vector<int> containers which behaves like arrays that grow in size automatically to accommodate as many values as you wish to store into them. Then you could call the STL sort function on both of these vector containers. From there it is just a matter of comparing the elements in both vectors one by one.

    Learning the STL group of container classes and functions can be somewhat intimidating for those who've never experimented with them before. Once you do learn, you'll be glad you did because they make many traditional programming chores almost trivial.
    Last edited by hk_mp5kpdw; 04-10-2005 at 09:21 AM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    3
    Ok, here is where I am up to. The code will ask for the numbers of attacking and defending units, and will request the Tech Level of the attackers and defenders. It then will work out what die type to assign to the attackers and defenders, and assign how many of that die type they get.

    You may think it's all crap, but I'm pretty damn chuffed with it. My first programming in 20 years

    My problem now is that I don't know how to sort the arrays ATTACKERARRAY and DEFENDERARRAY so I can compare the rolls. I appreciate the response I got above, but don't actually understand what it said (Still a total newb)

    I'm using the DEV-C++ compiler 4.9.9.2.

    Cheers,

    Andrew Campbell.

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <time.h>
    
    using namespace std;
    
    int ATMAX;
    int ATTL;
    int DEMAX;
    int DETL;
    int ATINITNUM;
    int DEINITNUM;
    int ROUNDNUM;
    float COMPARISONAT; //the comparison of force sizes.  This is a float, as it is derived by division.
    float COMPARISONDE; //the comparison of force sizes.  This is a float, as it is derived by division.
    int DEADDDICE;  //the additional dice that the defenders get due to force size
    int ATADDDICE;  //the add dice that the attackers get due to force size
    int DEDICE; // this is the number of defending dice (normally 3), but modified by DEADDDICE
    int ATDICE; // this is the number of attacking dice (normally 3), but modified by ATADDDICE
    
    int STARTUP()
    {
        
    
        cout<<"Enter the Tech Level of the attacking force. \n";
        cin>> ATTL;
        switch ( ATTL ){
               case 1:
                    cout<<"You chose TL1.  Attacker will be rolling D6. \n";
                    ATMAX = 6;
                    cin.get ();
                    break;
               case 2:
                    cout<<"You chose TL2.  Attacker will be rolling D8. \n";
                    ATMAX = 8;
                    cin.get ();
                    break;
               case 3:
                    cout<<"You chose TL3.  Attacker will be rolling D10. \n";
                    ATMAX = 10;
                    cin.get ();
                    break;
               case 4:
                    cout<<"You chose TL4.  Attacker will be rolling D12. \n";
                    ATMAX = 12;
                    cin.get ();
                    break;
               default:
                       cout<<"Non valid TL number entered, smartass.  Exiting. \n";
                       cin.get ();
                       break;
                       }
        cout<<"Enter the Tech Level of the defending force. \n";
        cin>> DETL;
        switch ( DETL ){
               case 1:
                    cout<<"You chose TL1.  Defender will be rolling D6. \n";
                    DEMAX = 6;
                    cin.get ();
                    break;
               case 2:
                    cout<<"You chose TL2.  Defender will be rolling D8. \n";
                    DEMAX = 8;
                    cin.get ();
                    break;
               case 3:
                    cout<<"You chose TL3.  Defender will be rolling D10. \n";
                    DEMAX = 10;
                    cin.get ();
                    break;
               case 4:
                    cout<<"You chose TL4.  Defender will be rolling D12. \n";
                    DEMAX = 12;
                    cin.get ();
                    break;
               default:
                       cout<<"Non valid TL number entered, smartass.  Exiting. \n";
                       cin.get ();
                       break;
                       }
        cout<<"\n The ATMAX variable, the max die roll, is set at ("<<ATMAX <<")\n";
        cout<<"\n The DEMAX variable, the max die roll, is set at ("<<DEMAX <<")\n";
        cout<<"\n Enter the number of attacking units. \n";
        cin>> ATINITNUM;
        cout<<"\n Enter the number of defending units. \n";
        cin>> DEINITNUM;
        cin.get ();
        cout<<"\n You have entered (" <<ATINITNUM <<") Attacking Units, and (" << DEINITNUM <<") Defending Units.\n";
        cin.get ();
    }
    
    int DICENUMBERS()
    {
        COMPARISONAT = ATINITNUM/DEINITNUM;
        COMPARISONDE = DEINITNUM/ATINITNUM;
        cout<<"\n The Attacking Force Divided by the Defending Force returns("<<COMPARISONAT<<")\n";
        cout<<"\n The Defending Force Divided by the Attacking Force returns("<<COMPARISONDE<<")\n";
        if (COMPARISONAT>COMPARISONDE)
        {                     
        if (COMPARISONAT >=2)
        {
                        if (COMPARISONAT <3)
                        {
                                         ATADDDICE = 1;
                                         }
                        else
                        {
                            ATADDDICE = 2;
                        }
                        }
        else
        {
                        ATADDDICE = 0;
                        }
                        }
        else
        {if (COMPARISONDE >=2)
        {
                        if (COMPARISONDE <3)
                        {
                                         DEADDDICE = 1;
                                         }
                        else
                        {
                            DEADDDICE = 2;
                        }
                        }
        else
        {
                        DEADDDICE = 0;
                        }
                        }
         cout<<"The Attacking force gets ("<<ATADDDICE<<") Extra Dice for this round!\n";
         cout<<"The Defending force gets ("<<DEADDDICE<<") Extra Dice for this round!\n";
    }
    
    int ATTACKERROLLS() // this is the program that rolls the correct no of dice and pops them in an array.
    {
        ATDICE = 3+ATADDDICE;
        cout<<"The Attacker will get a total of ("<<ATDICE<<")x("<<ATMAX<<")Sided Dice for this round.\n";
        int ATTACKERARRAY[ATDICE]; // This sets the size of the Attacker array equal to the Attacker dice.
        int p;
        int q;
        const int LOW=1;
        const int ATHIGH = ATMAX;
        for ( p=0; p<ATDICE; p++ )
        {
            cin.get ();
            time_t seconds;
            time(&seconds);
            srand((unsigned int) seconds);
            ATTACKERARRAY[p] = rand() % (ATHIGH-LOW+1) +LOW;
            q=(p+1);
            cout<<"Roll number "<<q<<" is "<<ATTACKERARRAY[p]<<" ! \n";
      }
    }
           
    int DEFENDERROLLS() // this is the program that rolls the correct no of dice and pops them in an array.
    {
        DEDICE = 3+DEADDDICE;
        cout<<"The Defender will get a total of ("<<DEDICE<<")x("<<DEMAX<<") Sided Dice for this round.\n";
        int DEFENDERARRAY[DEDICE]; // This sets the size of the defender array equal to the defender dice.
        int x;
        int y;
        const int LOW=1;
        const int DEHIGH = DEMAX;
        for ( x=0; x<DEDICE; x++ )
        {
            cin.get ();
            time_t seconds;
            time(&seconds);
            srand((unsigned int) seconds);
            DEFENDERARRAY[x] = rand() % (DEHIGH-LOW+1) +LOW;
            y=(x+1);
            cout<<"Roll number "<<y<<" is "<<DEFENDERARRAY[x]<<" ! \n";
      }
    }
            
       
            
            
    int main ()
    {
        cout<<"Colony Dice rolling program, version Beta 0.3\n\n";
        ROUNDNUM = 1;
        STARTUP();
        DICENUMBERS();
        cout<<"\n Battle Commences!!, Round ("<<ROUNDNUM<<")\n";
        ATTACKERROLLS();
        cin.get ();
        DEFENDERROLLS();
        cin.get ();
    }

  4. #4
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    Use sort from stl or create a sort algo yourself

    array= 6,7,2,1,6,4,2


    Code:
    for (a=0; a<7; a++)
    {
        for (b=0; b<7; b++)
        {
          is array[a]>array[b]?
          Yes=>swap array[a] with array[b];
         }
    }
    
    print array[a]  from zero to seven.

  5. #5
    Registered User
    Join Date
    Apr 2005
    Posts
    3
    I'll give it a bash now. Cheers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 02:14 AM
  2. basic question about global variables
    By radeberger in forum C++ Programming
    Replies: 0
    Last Post: 04-06-2009, 12:54 AM
  3. Replies: 15
    Last Post: 09-30-2008, 02:12 AM
  4. esbo's data sharing example
    By esbo in forum C Programming
    Replies: 49
    Last Post: 01-08-2008, 10:07 PM
  5. Replies: 6
    Last Post: 01-02-2004, 12:01 PM

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