-4

I'm having some trouble with this line of code: (sorry it is in danish)

 case 1 :
     system("cls");
       printf("K maa kun have en veardi mellem 1 og 20\nIndtast k:");
        scanf("%d", &k1);
  while(!(k1>=1 && k1 <=20))//Starten på fejlsikret tilstand
    {
     printf("K er for hoej eller har en forkert veardi.\nProev igen:  ");
     fflush(stdin);
     scanf("%d", &k1);
    }//slutning på fejlsikret tilstand

 system("cls");
 printf("X maa kun have en veardi mellem 1 og 20\nIndtast din modstanders tal: ");
 scanf("%d", &x1);
while(!(x1<21))//Starten på fejlsikret tilstand
 {
 printf("X er for høj.");
 scanf("%d", &x1);
 }//slutning på fejlsikret tilstand

            newmove=(21-x1)%(k1+1)+x1;
            printf("Du skal sige = %d", newmove);
            printf("\n\n\nTryk 1 og afslut med Enter for at komme tilbage til start");
            scanf("%d", &m);
            continue;

But it seems that the modulus part is calculating wrong. eks: If I put in the numbers: x1 = 12 and k1 = 12, it gives me 20. But it should be 17. Can anyone help?

Lasse
  • 597
  • 2
  • 10
  • 33

3 Answers3

1

the newmove should be 21

newmove=(21-x1) % (k1 + 1) + x1;
        (21-12) % (12 + 1) + 12
        (  9    %    13  ) + 12
                9          + 12
         21
MOHAMED
  • 41,599
  • 58
  • 163
  • 268
0

newmove=(21-x1)%(k1+1)+x1;

So if x1=12 and k1=12

newmove=(21-12)%(12+1)+12;
newmove=9%13+12;
newmove=9+12
newmove=21

Why should it be 17 ?

Joze
  • 1,285
  • 4
  • 19
  • 33
0

I would expect your result to be 21. Substituting x1=12 and k1=12 into this expression:

(21-x1)%(k1+1)+x1

gives:

(21-12)%(12+1)+12 = (9)%(13)+12 = 9 + 12 = 21
Marc Cohen
  • 3,742
  • 2
  • 19
  • 19