Using the example of 10, all the numbers from 1 to 10 are initially in the game. Suppose the user chooses 9; 9 is added to his score. The Tman gets 1 and 3 since these are exact divisors of 9; the Tman’s score is now 1 + 3 = 4. The numbers 1, 3 and 9 are removed from the game so the numbers remaining are 2, 4, 5, 6, 7, 8 and 10. Suppose the user chooses 6; his score is now 9 + 6 = 15. The Tman gets 2 and his score is now 4 + 2 = 6. The numbers 2 and 6 are removed from the game, leaving 4, 5, 7, 8 and 10. Note that now the user cannot choose 4, 5 or 7 since none of these has a divisor remaining in the game. Suppose he chooses 10; the Tman gets 5. The user’s score becomes 15 + 10 = 25 and the Tman’s score becomes 6 + 5 = 11. The numbers remaining are 4, 7 and 8. The user can choose only 8, making his score 33. The Tman gets 4, making his score 15. Only 7 is left in the game. Since there is nothing the user can choose, the Tman gets the remaining number(s) (7, in this example, making his score 22) and the game ends with the user being the winner.

Write a program to play the game of Tman. The first item of data requested is the upper limit (10 in the example) of the numbers in the game. Assume that the limit is less than or equal to 100. The program then repeatedly asks for the user’s choice until the game is over. For each choice entered, the program must print what the Tman gets as well as the current scores. If the user enters an invalid choice (for example, a number not in the game or a number with no divisors remaining in the game), print a message and ask for another number. At the end, print the winner.

# include <stdio.h>
# include <stdlib.h>
# include <string.h>
# include <conio.h>

typedef struct{
        int a[11];
        }number;

const int max = 11;  


int main  () {   
    number array;
    int j;
    int count = 0;
    int y,x,z;    
    int sumuser;
    int sumtman;
    
    for ( j= 1; j <max; j++ ){
        count = count +1 ;
        array.a[j] = count;
        
       }
       
    
          do{
                    printf( "Enter number you want");
                    scanf ("%d",z);
                    for ( j= 1; j <max; j++ ){
                        if ( z == array.a[j]){
                           sumuser = sumuser + array.a[j];
                           array.a[j] = 0;
                           for(x = 1; x != z ; x++){
                                 y = z%x;
                                 if(y == 0 ){
                                 sumtman = sumtman + x;
                                 array.a[x] =0;
                                 
                                 }}}}                   
          }while(y !=0);
    
          
        printf("%d\n",sumtman);
        printf("%d",sumuser);
       
       
        getch ();
        
        }

i've reached this far and completely lost as to what to do again...

Recommended Answers

All 2 Replies

Good job. nicely formatted code, but you used conio.h . What grade did you get?

I am assuming that the input is always going to be a whole number and not an integer.

I am not providing you with the entire logic. Though I modified your code. You still need to work on it. I am providing comments with the code that might help.

I hope it helps...

do{
            printf( "Enter number you want");
            scanf ("%d",z);
            for ( j= 1; j <max; j++ ){
                   if ( z == array.a[j]  ){   
                       if (array.a[j] != -1 ){  //check whether the number is previously used or not.
                          
                                 // Before adding it to the score, you will have to check whether the number has any multiples left. If there are no multiples left then you will have to provide a proper error message to the user. 
	                           sumuser = sumuser + array.a[j];
	                           array.a[j] = -1;
	                           for(x = 1; x <= z/2 ; x++){  // a number cannot have a multiple greater than half of the number. Number '100' will have a multiple till '50'. After 50, there is no multiple of 100 except 100. This condition reduces the number of loops.
        	                            y = z%x;
                	                    if(y == 0 ){
                        	                 if (array.a[x] != -1 ) {  
                                	            sumtman = sumtman + x;
                                        	    array.a[x] =-1;
                                                  }//if 	
                                            }//if
                                   }//for                          
                          }//if
                          else{  // If the number is used then give a proper error message to the user.

				printf("Wrong selection");
				break;
                          }//else
                   }//if
           }//for 
      }while(y !=0);//This Y condition is wrong. You are reusing the 'y' variable. This is not going to work. The looping should occur until all the numbers are used from the array. You need to keep a count of the number used and compare it to the total count of the numbers in the array.
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.