<mosaic.cnfolio.com>
PROGRAM
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int outofrange(int x); //funtion to check if the input of card values are out of range
  5.  
  6. //main function starts
  7.  
  8. void main(){
  9.  
  10. printf("Card Game\n");
  11. int p1;
  12. int player1 [10];
  13. int p2;
  14. int player2 [10];
  15.  
  16.  
  17. //PLAYER 1
  18.  
  19. //input number of cards for player 1
  20.  
  21. scanf("%d", &p1);
  22.  
  23.    if(p1<2 || p1>10){
  24.       printf ("player1: you should have number of cards between 2 and 10\n");
  25.       goto endofproc;
  26.       
  27.    }
  28.    
  29.    
  30.  
  31. //scanning player 1 card values
  32.  
  33. for (int i=0;i<p1;i++){
  34.    scanf("%d", &player1[i]);
  35.    int flag=outofrange(player1[i]);
  36.    if(flag==1){
  37.    goto endofproc;
  38.    }
  39.    }
  40.    
  41.    
  42.    //sorting the arrays in decreasing order
  43.    
  44.    int c, d, swap;
  45.    
  46.    for (c = 0 ; c < ( p1 - 1 ); c++)
  47.   {
  48.     for (d = 0 ; d < p1 - c - 1; d++)
  49.     {
  50.       if (player1[d] < player1[d+1])
  51.       {
  52.         swap       = player1[d];
  53.         player1[d]   = player1[d+1];
  54.         player1[d+1] = swap;
  55.       }
  56.     }
  57.   }
  58.  
  59.  //player 1 array sorted in decreasing order
  60.    
  61.  
  62. //PLAYER 2
  63.  
  64. //input number of cards for player 2
  65.  
  66. scanf("%d", &p2);
  67.  
  68.    if(p2<2 || p2>10){
  69.       printf ("player2: you should have number of cards between 2 and 10\n");
  70.        goto endofproc;
  71.    }
  72.    
  73. //scanning player 2 card values
  74.  
  75. for (int i=0;i<p2;i++){
  76.    scanf("%d", &player2[i]);
  77.    int flag= outofrange(player2[i]);
  78.    if(flag==1){
  79.    goto endofproc;
  80.    }
  81.    }
  82.    
  83.    
  84.    //sorting the array in decreasing order
  85.    
  86.    int c1, d1, swap1;
  87.    
  88.    for (c1 = 0 ; c1 < ( p2 - 1 ); c1++)
  89.   {
  90.     for (d1 = 0 ; d1 < p2 - c1 - 1; d1++)
  91.     {
  92.       if (player2[d1] < player2[d1+1])
  93.       {
  94.         swap1       = player2[d1];
  95.         player2[d1]   = player2[d1+1];
  96.         player2[d1+1] = swap1;
  97.       }
  98.     }
  99.   }
  100.  
  101.  //player 2 array sorted in decreasing order
  102.  
  103.  
  104. //MAIN ALGORITHM
  105.  
  106. //Setting index value
  107.    int index = 0;
  108.  
  109.    if (p1<=p2){
  110.  
  111.    index = p1-1;
  112.    }
  113.  
  114.    else {
  115.       index=p2-1;
  116.    }
  117.  
  118.  
  119. //matching values
  120.  
  121.    for (int i=0; i<index;i++){
  122.    if(player1[i]>player2[i])
  123.    {
  124.       printf("player 1 wins\n");
  125.       goto endofproc;
  126.       
  127.    } 
  128.    else if(player1[i]<player2[i]){
  129.    printf("player 2 wins\n");
  130.       goto endofproc;
  131.    }
  132.    else {
  133.    
  134.    }
  135.    }
  136.  
  137. if(p1<p2){
  138.       printf("player 2 wins\n");
  139.    }
  140.    else if (p1>p2){
  141.       printf("player 1 wins\n");
  142.    }
  143.    else {
  144.    printf("its a Draw\n");
  145.    }
  146.  
  147.  
  148. //MAIN ALGORITHM END
  149.  
  150.  
  151.    
  152. endofproc:
  153. printf("\nPrograming exiting");
  154.  
  155.  
  156. }
  157.  
  158. //funtion to check that the person add values of cards that are in the range
  159.  
  160. int outofrange(int x){
  161. int flag=0;
  162. if (x<1 || x>10){
  163.    printf("\n%d is out of range.you should have number of cards between 1 and 10 \n", x);
  164.    flag=1;
  165.    
  166.    }
  167.  
  168. return(flag);
  169.  
  170. }