Thread: Numerical order with ifs question?

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    25

    Numerical order with ifs question?

    In the variables ac3, bc3, cc3, dc3, ec3, and fc3, I have stored the number of moves it took for something to happen. Now, I need to get the lowest, 2nd lowest and third lowest from that group.
    Code:
    if(ac3<=bc3 && ac3<=cc3 && ac3<=dc3 && ac3<=ec3 && ac3<=fc3)
        {
            first=1;
            ac3=99;
        }
        if(bc3<=ac3 && bc3<=cc3 && bc3<=dc3 && bc3<=ec3 && bc3<=fc3)
        {
            first=2;
            bc3=99;
        }
        if(cc3<=ac3 && cc3<=bc3 && cc3<=dc3 && cc3<=ec3 && cc3<=fc3)
        {
            first=3;
            cc3=99;
        }
        if(dc3<=ac3 && dc3<=bc3 && dc3<=cc3 && dc3<=ec3 && dc3<=fc3)
        {
            first=4;
            dc3=99;
        }
        if(ec3<=ac3 && ec3<=bc3 && ec3<=cc3 && ec3<=dc3 && ec3<=fc3)
        {
            first=5;
            ec3=99;
        }
        if(fc3<=ac3 && fc3<=bc3 && fc3<=cc3 && fc3<=dc3 && fc3<=ec3)
        {
            first=6;
            fc3=99;
        }
    That code works and will pick the lowest from the group.
    // I made it do like "#c3=99;" so that for picking the 2nd lowest, it wouldn't count the first.

    Now, when I try to pretty much duplicate that code for 2nd place, it messes everything up. Any idea why?
    I set three void functions
    (void checkfirst
    void checksecond
    void checkthird)
    so that they do not interfere with each other, but it seems when I put in the 2nd or 3rd, it messes with all of them and usually has a lot of 6s (the highest?)

    Here is the entire code if you were wondering:
    Code:
    #include<iostream.h>
    #include<time.h>
    #include<stdlib.h>
    void wait(double seconds)
    {
        clock_t endwait;
        endwait=clock()+seconds*CLK_TCK;
        while(clock()<endwait){}
    }
    
    int ac,bc,cc,dc,ec,fc;
    int ac2,bc2,cc2,dc2,ec2,fc2;
    int ac3,bc3,cc3,dc3,ec3,fc3;
    int first, second, third;
    
    char track[6][12]=
    {
        '1','.','.','.','.','.','.','.','.','.','.','.',
        '2','.','.','.','.','.','.','.','.','.','.','.',
        '3','.','.','.','.','.','.','.','.','.','.','.',
        '4','.','.','.','.','.','.','.','.','.','.','.',
        '5','.','.','.','.','.','.','.','.','.','.','.',
        '6','.','.','.','.','.','.','.','.','.','.','.'
    };
    int x=0;
    void showrace();
    void movedahorse();
    void checkfirst();
    void checksecond();
    void checkthird();
    
    int main()
    {
        srand(time(0));
        showrace();
        do
        {
            movedahorse();
            wait(0.3);
            system("cls");
            showrace();
        }while(track[0][11]!='1' || track[1][11]!='2' || track[2][11]!='3' || track[3][11]!='4' || track[4][11]!='5' || track[5][11]!='6');
        
        checkfirst();
    	checksecond();
    	checkthird();
    
    //cout<<endl<<ac2+ac<<endl<<bc2+bc<<endl<<cc2+cc<<endl<<dc2+dc<<endl<<ec2+ec<<endl<<fc2+fc<<endl;
        return 0;
    }
    
    
    void showrace()
    {
        for (int x=0;x<6;x++)
        {
            for(int y=0;y<12;y++)
            {
                cout<<track[x][y]<<"   ";
            }
            cout<<endl;
            cout<<endl;
        }
    }
    
    void movedahorse()
    {
        int a,b,c,d,e,f;
        srand(time(0));
        a=rand()%2+1;
        b=rand()%2+1;
        c=rand()%2+1;
        d=rand()%2+1;
        e=rand()%2+1;
        f=rand()%2+1;
        
        
        if(a==1&&ac<11)
        {
            ac=ac+1;
            track[0][ac]='1';
            track[0][ac-1]='.';
            srand(time(0));
        }else if(a==2&&ac<11)
        {
        ac2=ac2+1;
        }
        
        if(b==1&&bc<11)
        {
            bc=bc+1;
            track[1][bc]='2';
            track[1][bc-1]='.';
            srand(time(0));
        }else if(b==2&&bc<11)
        {
        bc2=bc2+1;
        }
    
        if(c==1&&cc<11)
        {
            cc=cc+1;
            track[2][cc]='3';
            track[2][cc-1]='.';
            srand(time(0));
        }else if(c==2&&cc<11)
        {
        cc2=cc2+1;
        }
    
        if(d==1&&dc<11)
        {
            dc=dc+1;
            track[3][dc]='4';
            track[3][dc-1]='.';
            srand(time(0));
        }else if(d==2&&dc<11)
        {
        dc2=dc2+1;
        }
    
        if(e==1&&ec<11)
        {
            ec=ec+1;
            track[4][ec]='5';
            track[4][ec-1]='.';
            srand(time(0));
        }else if(e==2&&ec<11)
        {
        ec2=ec2+1;
        }
    
        if(f==1&&fc<11)
        {
            fc=fc+1;
            track[5][fc]='6';
            track[5][fc-1]='.';
            srand(time(0));
        }else if(f==2&&fc<11)
        {
        fc2=fc2+1;
        }
        
    }
        
    void checkfirst()
    {
        
        ac3=ac2+ac;
        bc3=bc2+bc;
        cc3=cc2+cc;
        dc3=dc2+dc;
        ec3=ec2+ec;
        fc3=fc2+fc;
    	
        if(ac3<=bc3 && ac3<=cc3 && ac3<=dc3 && ac3<=ec3 && ac3<=fc3)
        {
            first=1;
            ac3=99;
        }
        if(bc3<=ac3 && bc3<=cc3 && bc3<=dc3 && bc3<=ec3 && bc3<=fc3)
        {
            first=2;
            bc3=99;
        }
        if(cc3<=ac3 && cc3<=bc3 && cc3<=dc3 && cc3<=ec3 && cc3<=fc3)
        {
            first=3;
            cc3=99;
        }
        if(dc3<=ac3 && dc3<=bc3 && dc3<=cc3 && dc3<=ec3 && dc3<=fc3)
        {
            first=4;
            dc3=99;
        }
        if(ec3<=ac3 && ec3<=bc3 && ec3<=cc3 && ec3<=dc3 && ec3<=fc3)
        {
            first=5;
            ec3=99;
        }
        if(fc3<=ac3 && fc3<=bc3 && fc3<=cc3 && fc3<=dc3 && fc3<=ec3)
        {
            first=6;
            fc3=99;
        }
        cout<<"First place goes to Horse #"<<first<<endl;
    }
    
    void checksecond()
    {
    if(ac3<=bc3 && ac3<=cc3 && ac3<=dc3 && ac3<=ec3 && ac3<=fc3)
        {
            second=1;
            ac3=99;
        }
        if(bc3<=ac3 && bc3<=cc3 && bc3<=dc3 && bc3<=ec3 && bc3<=fc3)
        {
            second=2;
            bc3=99;
        }
        if(cc3<=ac3 && cc3<=bc3 && cc3<=dc3 && cc3<=ec3 && cc3<=fc3)
        {
            second=3;
            cc3=99;
        }
        if(dc3<=ac3 && dc3<=bc3 && dc3<=cc3 && dc3<=ec3 && dc3<=fc3)
        {
            second=4;
            dc3=99;
        }
        if(ec3<=ac3 && ec3<=bc3 && ec3<=cc3 && ec3<=dc3 && ec3<=fc3)
        {
            second=5;
            ec3=99;
        }
        if(fc3<=ac3 && fc3<=bc3 && fc3<=cc3 && fc3<=dc3 && fc3<=ec3)
        {
            second=6;
            fc3=99;
        }
        cout<<"Second place goes to Horse #"<<second<<endl;
    }
    
    void checkthird()
    {
    }

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You should be using else ifs so that only one section will be run. Otherwise, setting a variable to 99 might make another if below be entered as well.

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    25
    Hmmm very good point, didnt think of that, can you show me basically how itd be set up. I get what you're saying but how would you make it stop checking once the IF is completed?

    (PS: don't worry about Ties)

    edit:
    nevermind! THANKS I think I got it

    k, it works now!
    I just added the "else" after the } of each if command (except the last) and now it works perfectly

    betting function here i come :P
    Last edited by JDrake; 11-04-2006 at 07:28 AM.

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    25
    How do you have it so that
    if(money<0)
    END PROGRAM?

    (simple way?)

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    If you are in the main function, just return. If that is an error, then you can return 1 or some other non-zero number to signal an error, or you can just return 0 like normal if your users don't want to check the program return value.

    If you are not in the main function, it is a little more complicated. You could use exit(), but that is poor design. What you should do is have that function return the information that the money is negative to main so that main can do what it wants, which in this case is to return and end the program. If your function already returns a value, you might even have to rethink some things in your overall design, perhaps by separating larger functions into smaller ones.

  6. #6
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Here's a far easier way to do things, using std::sort to sort a vector in order. std::pair lets you group two values together (the values to sort, and the 1,2,3,4,5,6 that tells you which variable it was from). std::sort will sort the first value in ascending order. v[x].second returns the second value from each pair.

    Code:
    #include <vector>
    #include <algorithm>
    #include <iostream>
    
    
    int main(int, char**){
    
    	int ac3 = 6, bc3 = 4, cc3 = 12, dc3 = 1, ec3 = 11, fc3 = 99;
    
    	std::vector<std::pair<int, int> > v;
    	v.push_back(std::make_pair<>(ac3,1));
    	v.push_back(std::make_pair<>(bc3,2));
    	v.push_back(std::make_pair<>(cc3,3));
    	v.push_back(std::make_pair<>(dc3,4));
    	v.push_back(std::make_pair<>(ec3,5));
    	v.push_back(std::make_pair<>(fc3,6));
    
    	std::sort(v.begin(), v.end());
    
    	std::cout << "First: " << v[0].second << " second: " << v[1].second << " third: " << v[2].second <<std::endl;
    
    }

    Though, in reality, you really should improve your variable names. Firstly, a variable name should describe what the variable is. Secondly, if you need a lot of very similar variables, use arrays or (even better) vectors.

    Example:

    Code:
    #include <vector>
    #include <algorithm>
    #include <iostream>
    
    int main(int, char**){
    
    	std::vector<int> elapsedTimes(6);
    	elapsedTimes[0] = 6;
    	elapsedTimes[1] = 4;
    	elapsedTimes[2] = 12;
    	elapsedTimes[3] = 1;
    	elapsedTimes[4] = 11;
    	elapsedTimes[5] = 99;
    
    	std::vector<std::pair<int, int> > v;
    	for (int i = 0; i < (int) elapsedTimes.size(); ++i)
    		v.push_back(std::make_pair<>(elapsedTimes[i],i));
    
    	std::sort(v.begin(), v.end());
    
    	std::cout << "First: " << v[0].second << " second: " << v[1].second << " third: " << v[2].second << std::endl;
    
    }
    Last edited by Cat; 11-04-2006 at 01:44 PM.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Curiosity question about classes...
    By Raigne in forum C++ Programming
    Replies: 5
    Last Post: 03-10-2008, 12:34 PM
  2. FFT returning values in wrong order
    By DavidP in forum C# Programming
    Replies: 3
    Last Post: 10-25-2007, 01:15 PM
  3. Design layer question
    By mdoland in forum C# Programming
    Replies: 0
    Last Post: 10-19-2007, 04:22 AM
  4. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM
  5. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 03:57 PM