Thread: Statistic problem

  1. #1
    Registered User
    Join Date
    Mar 2008
    Location
    Slovenia
    Posts
    36

    Statistic problem

    Hello. My problem is like this. I have to make a program where i have 6 random number from 1 to 6. I can change any of the numbers 2 times. So far this is working for me. The next step i dont know how to begin. I have to remember what king of a resault my numbers have been. Like in a poker game. The 5 numbers represet cards. So now if i have two similar numbers, its one pair. Something like

    25264 -> this is like one pair, i have to look for this and than print out like how many pairs, two pairs, tree of a kind and so on i had in one session of playing the game

    I dont know how to do this. So far this is my code:

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    
    using namespace std;
    
    int MetKocke();
    
    int main(){
    
    	int Kocka[6];
    	int seme,ponovniMet,steviloKock,zamenjavaKock;
    	char izhod='d';
    
    	seme=time(NULL);
    	srand(seme);
    
    	do{
    		for(int i=1;i<6;i++){
    			Kocka[i]=MetKocke();
    			cout<< "Kocka"<< i <<" = " << Kocka[i] << endl;
    		}
    
    	cout<<"Ponovni meti 0-krat, 1-krat, 2-krat? "; cin>>ponovniMet;
    	
    	for(int j=0;j<ponovniMet;j++){
    	
    		cout<<"Koliko kock zelite ponovno metati od 1 do 5. "; cin>>steviloKock;
    		
    		for(int k=1;k<=steviloKock;k++){
    			cout<<"Katere kocke zelis zamenjat od 1 do 5 "; cin>>zamenjavaKock;
    			Kocka[zamenjavaKock]=MetKocke();
    		
    			for(int l=1;l<6;l++)
    				cout<< "Kocka"<< l <<" = " << Kocka[l] << endl;
    		}
    	}
    
    	cout<<"Go on with the game y ali n"<<" "; cin>>izhod;
    
    	}while(izhod=='y');
    
    	return 0;
    }
    
    int MetKocke(){
    
    	return(rand()%6)+1; //random numbers from 1 to 6
    }
    If you could just point me in the right direction.

    Thank you in advance.
    bnk

  2. #2
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    Since there are only 6 possible values, you can create another 6-element array:
    Code:
    int frequencies[] = {0,0,0,0,0,0};
    Then look at each element of your random array Kocka[i]. If the number in Kocka[i] is n, add 1 to frequencies[n-1].

    When finished, frequencies[j] will tell you how many of (j+1) are in Kocka.

  3. #3
    Registered User
    Join Date
    Mar 2008
    Location
    Slovenia
    Posts
    36
    Thank you for your help... I implemented what you said.

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    
    using namespace std;
    
    int throwCube();
    
    int main(){
    
    	int bnkslo[6];
    	int number[7]={0,0,0,0,0,0,0};
    	int seme,throwAgain,numberCubes,changeCubes;
    	char izhod='d';
    
    	seme=time(NULL);
    	srand(seme);
    
    	do{
    		for(int i=1;i<6;i++){
    		bnkslo[i]=throwCube();
    		cout<< "Cube"<< i <<" = " << bnkslo[i] << endl;
    		}
    
    	cout<<"Throw again 0-time, 1-time, 2-time "; cin>>throwAgain;
    	
    	for(int j=0;j<throwAgain;j++){ //how many times do you want to throw again
    	
    		cout<<"How many Cubes do you want to throw again 1 to 5. "; cin>>numberCubes;
    		
    		for(int k=1;k<=numberCubes;k++){ //how many cubes do you want to change
    			
    			cout<<"Whitch Cubes do you want to change 1 to 5. "; cin>>changeCubes;
    			bnkslo[changeCubes]=throwCube();
    		
    			for(int l=1;l<6;l++) //changing the selected cube
    				cout<< "Cube"<< l <<" = " << bnkslo[l] << endl;
    		}
    	}
    
    	for(int n=1;n<6;n++){ //looking for numbers
    		switch(bnkslo[n]){
    			case 1:
    				number[1]++;
    				break;
    			case 2:
    				number[2]++;
    				break;
    			case 3:
    				number[3]++;
    				break;
    			case 4:
    				number[4]++;
    				break;
    			case 5:
    				number[5]++;
    				break;
    			case 6:
    				number[6]++;
    				break;
    	}
    }
    	cout<<"Do you want to countinue? y or n"<<" "; cin>>end;
    
    	}while(end=='y');
    
    	return 0;
    }
    
    int throwCube(){
    
    	return(rand()%6)+1; //generating random numbers from 1 to 6
    }
    Now the problem is kind of still there. I have the numbers that are there counted but i do not know how to use that to rank them...

    I think somethink like that:

    If my hand would be: 23252

    That means i have :
    123456
    031010

    now i could again whit a for loop go true the array adn use another switch to look at the array

    switch(number[a])
    case 1:
    if(number[2]&&number[3]...)

    am i on the right track or is there something else that i could do?

    Thank you in advance.

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    Quote Originally Posted by bnkslo
    ... print out like how many pairs, two pairs, tree of a kind and so on ...
    Is this all that you want to do, or are there more requirements?

    By the way, your "throwing" loop is only iterating 5 times. Since you want to ignore the "0" element of the array, you can write
    Code:
    int bnkslo[7];
    //...
    for(int i=1;i<7;i++){
        bnkslo[i]=throwCube();
        cout<< "Cube"<< i <<" = " << bnkslo[i] << endl;
    }
    You have a similar problem with the "looking for numbers" loop.

    Also, a switch statement is a bit "clunky" for this job. You could replace the entire switch block with this:
    Code:
    for(int n=1;n<7;n++){
      number[bnkslo[n]]++;
    }
    but you really should get used to using the array[0] element, and counting from 0 in your loops. After a while it will feel more natural.

  5. #5
    Registered User
    Join Date
    Mar 2008
    Location
    Slovenia
    Posts
    36
    Quote Originally Posted by R.Stiltskin View Post
    Originally Posted by bnkslo
    ... print out like how many pairs, two pairs, tree of a kind and so on ...

    Is this all that you want to do, or are there more requirements?
    Yes this is all i want to do. At the end i have to keep track of the hands that where dealt.

    Like:

    1 x pair
    4 x 2 pair
    1 x tree of a kind
    ...
    ..
    .
    and so on

    I see what you mean about the "0".

    And the switch statement is really "clunky" but i would never thought of number[bnkslo[n]]++; this opened my eyes a bit more...

    Thank you.

  6. #6
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    So you need some variables to accumulate the statistics over all the games, one for each possible outcome. Then write another loop that looks at each element of the numbers[] array and records the statistic for the game. The most obvious way I guess is to have a series of nested if-else statements to classify the results in that loop. Four-of-a-kind and five-of-a-kind are easy since either of those precludes any other possibility (and you can immediately break out of the loop). Pairs and triples are a bit trickier since you can have 2 pairs or a pair and a triple in a game. You can use two boolean flag variables, initially false to record the first observation of pair or triple, and set the appropriate game counter when you find either a pair or a triple AND one of the flags is true (and then reset the flags to false), and then examine the flag values after the loop is finished to see if one of the flags is true (meaning that only 1 pair or 1 triple was seen).

    You might want to count "straights" also. Since there are 5 "cards" and only 6 possible values, that will be very easy to figure out.

  7. #7
    Registered User
    Join Date
    Mar 2008
    Location
    Slovenia
    Posts
    36
    Thank you for your help. I finished it. And i have to say it works.

    Now its time to do the second assigment --> calculator. I already know i will need some help...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. beginner problem
    By The_Nymph in forum C Programming
    Replies: 4
    Last Post: 03-05-2002, 05:46 PM