# Simple Logical Problem.. HELP!

This is a discussion on Simple Logical Problem.. HELP! within the C Programming forums, part of the General Programming Boards category; Code: case 1: ///////////////EASY MATH printf("EASY\n"); if (x1==0){ printf("1 + 6\n");scanf("%d",&a1[0]);} if (x1==1){ printf("2 + 3\n");scanf("%d",&a1[1]);} if (x1==2){ printf("2 + ...

1. ## Simple Logical Problem.. HELP!

Code:
``` case 1: ///////////////EASY MATH
printf("EASY\n");
if (x1==0){
printf("1 + 6\n");scanf("%d",&a1[0]);}
if (x1==1){
printf("2 + 3\n");scanf("%d",&a1[1]);}
if (x1==2){
printf("2 + 7\n");scanf("%d",&a1[2]);}
if (x1==3){
printf("7 + 4\n");scanf("%d",&a1[3]);}
if (x1==4){
printf("6 + 9\n");scanf("%d",&a1[4]);}

if (x2==0){
printf("1 + 2\n");scanf("%d",&a2[0]);}
if (x2==1){
printf("1 + 3\n");scanf("%d",&a2[1]);}
if (x2==2){
printf("1 + 4\n");scanf("%d",&a2[2]);}
if (x2==3){
printf("9 + 4\n");scanf("%d",&a2[3]);}
if (x2==4){
printf("3 + 7\n");scanf("%d",&a2[4]);}

if (x3==0){
printf("7 - 5\n");scanf("%d",&a3[0]);}
if (x3==1){
printf("9 - 6\n");scanf("%d",&a3[1]);}
if (x3==2){
printf("3 - 1\n");scanf("%d",&a3[2]);}
if (x3==3){
printf("2 - 2\n");scanf("%d",&a3[3]);}
if (x3==4){
printf("9 - 2\n");scanf("%d",&a3[4]);}

if (x4==0){
printf("6 * 6\n");scanf("%d",&a4[0]);}
if (x4==1){
printf("9 * 2\n");scanf("%d",&a4[1]);}
if (x4==2){
printf("2 * 6 \n");scanf("%d",&a4[2]);}
if (x4==3){
printf("4 * 8\n");scanf("%d",&a4[3]);}
if (x4==4){
printf("7 * 6\n");scanf("%d",&a4[4]);}

if (x5==0){
printf("6 / 6\n");scanf("%d",&a5[0]);}
if (x5==1){
printf("9 / 3\n");scanf("%d",&a5[1]);}
if (x5==2){
printf("30 / 6 \n");scanf("%d",&a5[2]);}
if (x5==3){
printf("100 / 10\n");scanf("%d",&a5[3]);}
if (x5==4){
printf("54/ 9\n");scanf("%d",&a5[4]);}

//ans EASY MATH
/////1
if (a1[0]==7){
score++;}
if (a1[1]==5){
score++;}
if (a1[2]==9){
score++;}
if (a1[3]==11){
score++;}
if (a1[4]==15){
score++;}
////2
if (a2[0]==3){
score++;}
if (a2[1]==4){
score++;}
if (a2[2]==5){
score++;}
if (a2[3]==13){
score++;}
if (a2[4]==10){
score++;}
////3
if (a3[0]==2){
score++;}
if (a3[1]==3){
score++;}
if (a3[2]==2){
score++;}
if (a3[3]==0){
score++;}
if (a3[4]==7){
score++;}
//////////////4
if (a4[0]==36){
score++;}
if (a4[1]==18){
score++;}
if (a4[2]==12){
score++;}
if (a4[3]==32){
score++;}
if (a4[4]==42){
score++;}
///////5
if (a5[0]==1){
score++;}
if (a5[1]==3){
score++;}
if (a5[2]==5){
score++;}
if (a5[3]==10){
score++;}
if (a5[4]==6){
score++;}

printf("you got %d out of 5\n",score);

break;//EASY MATH```
just a piece of the code

This is a 5 item quiz program...

the problem is that when you got a perfect score.. it will display you got 6 out 5...which is I cant tell why... please help.. thanks

note: already set the score to 0.

2. To help you debug this, start by formatting your code properly. This way, you will be better able to trace the flow of control and hence figure out why score ends up as 6 instead of 5.

3. Originally Posted by laserlight
To help you debug this, start by formatting your code properly. This way, you will be better able to trace the flow of control and hence figure out why score ends up as 6 instead of 5.
How do you format it properly?

4. Something like this:
Code:
```case 1: ///////////////EASY MATH
printf("EASY\n");
if (x1 == 0) {
printf("1 + 6\n");
scanf("%d", &a1[0]);
}
if (x1 == 1) {
printf("2 + 3\n");
scanf("%d", &a1[1]);
}
if (x1 == 2) {
printf("2 + 7\n");
scanf("%d", &a1[2]);
}
if (x1 == 3) {
printf("7 + 4\n");
scanf("%d", &a1[3]);
}
if (x1 == 4) {
printf("6 + 9\n");
scanf("%d", &a1[4]);
}```
Also, you have too much duplication of code. It isn't outright duplication, but there are repeated structures that could be simplified. For example, a very minimal simplication of the above code might be:
Code:
```case 1: ///////////////EASY MATH
printf("EASY\n");
if (x1 == 0) {
printf("1 + 6\n");
} else if (x1 == 1) {
printf("2 + 3\n");
} else if (x1 == 2) {
printf("2 + 7\n");
} else if (x1 == 3) {
printf("7 + 4\n");
} else if (x1 == 4) {
printf("6 + 9\n");
}
if (0 <= x1 && x1 < 5) {
scanf("%d", &a1[x1]);
}```
If you placed those strings used in the printf calls in an array, you might even just write:
Code:
```case 1: ///////////////EASY MATH
printf("EASY\n");
if (0 <= x1 && x1 < 5) {
printf("%s\n", al_texts[x1]);
scanf("%d", &a1[x1]);
}```
Then if instead of variables x1, x2, etc, you used an array x:
Code:
```case 1: ///////////////EASY MATH
printf("EASY\n");
for (i = 0; i < 5; ++i) {
if (0 <= x[i] && x[i] < 5) {
printf("%s\n", texts[i][x[i]]);
scanf("%d", &a[i][x[i]]);
}
}```

5. There's no need to have five different arrays with (at least) five different elements just to store one value in each array. And, checking these arrays can be problematic:

Code:
```/* let's say that x1 is 0 */

if (x1==0) {

printf("1 + 6\n");
scanf("%d",&a1[0]);
}

/* now a1[0] is (hopefully) 7 */
/* and a1[1], a1[2], ...a1[4] aren't initialized to anything */

if (a1[0]==7){
score++;}
/* score should be 1 */

if (a1[1]==5){
score++;}
/* because the array isn't initialized, if a1[1] happens to have a 5, the score will wrongly increase */

if (a3[3]==0){
score++;}
/* BUT, even if it is initialized, it won't help when the program checks for zero here */```
If x3 isn't 3, this value will have never been changed, and there's a good chance it's already zero. Hence the comparison evaluates to true, and the score increases without a correct answer.

Also, I see
Code:
`score++;`
way too many times. You should figure out a way to check answers in a loop similar to what Laserlight posted.