-2

In line no 20,27,32,36 even though the value is true the count value is not increasing at the end it is showing 0 count value. i think i have made an mistake while comparing array. i dont know how to compare an char element 2d array the input is
1
xxx
ooo


the answer must be 3 the code is as below

#include <stdio.h>
    int main()
    {
        int n;
        scanf("%d", &n);
        for (int i = 0; i < n; n++)
        {
            int count = 0;
            char t[2][2];
            for (int i = 1; i <= 3; i++)
            {
                scanf("%c %c %c", &t[i][0], &t[i][1], &t[i][2]);
            }
            if (t[0][0] == '_' || t[0][1] == '_' || t[0][2] == '_' || t[1][0] == '_' || t[1][1] == '_' || t[1][2] == '_' || t[2][0] == '_' || t[2][1] == '_' || t[2][2] == '_')
            {
                for (int j = 0; j < 3; j++)
                {
                    if (t[j][0] == t[j][1] == t[j][2])
                    {
20)                        count++;
                    }
                }
                for (int q = 0; q < 3; q++)
                {
                    if (t[0][q] == t[1][q] == t[2][q])
                    {
27)                        count++;
                    }
                }
                if (t[0][0] == t[1][1] == t[2][2])
                {
32)                    count++;
                }
                else if (t[0][2] == t[1][1] == t[2][0])
                {
36)                    count++;
                }
                if (count == 0)
                {
                    printf("%d", 2);
                }
                else if (count == 1)
                {
                    printf("%d", 1);
                }
                else if (count > 1)
                {
                    printf("%d", 3);
                }
            }
            
        }
    }
Alan Birtles
  • 32,622
  • 4
  • 31
  • 60
  • 5
    `if (x == y == z)` should be `if (x == y && y == z)`. The former checks whether `x` is equal to `y`, and then compares `z` with either 0 or 1. The latter verifies that `x`, `y` and `z` all have the same value. – user3386109 May 17 '21 at 05:14
  • Can you simplify your code to eliminate distractions? For example, initialize `int n = 1;` instead of reading the value from the input. At which point, your outer `for` loop has only one iteration, so you could keep the loop body but drop the `for` line. Focus on the troublesome comparison. See [mre]. – JaMiT May 17 '21 at 05:15
  • `char t[2][2];` can accomodate 4 elements and this `for (int i = 1; i <= 3; i++)` loop will execute 3 times and `scanf("%c %c %c", &t[i][0], &t[i][1], &t[i][2]);` is scaning for 3 elements in each iteration. – thirdeye May 17 '21 at 05:21
  • Possible duplicate: https://stackoverflow.com/questions/58271178/why-comparing-three-variables-together-with-evaluates-to-false – Lukas-T May 17 '21 at 06:10

1 Answers1

-1

Code below is probably what you're looking for:

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

int main()
{
    int n, count=0; char s[20];
    printf("Enter total number of iterations (n): ");
    scanf("%s", s);    // Don't use %d. If you use %d and input a character, \
                          buffer issues arise.
    printf("\n");
    n = atoi(s);
    for (int i = 0; i < n; i++)
    {
        int count = 0;
        char t[3][3];
        for (int x = 0; x < 3; x++)
        {
            printf("Enter 3 chars with spaces between them: ");
            scanf(" %c %c %c", &t[x][0], &t[x][1], &t[x][2]);  // Space before\
                                                     the first %c makes scanf \
                                                     skip leading whitespace.
            printf("\n");
        }
    
        printf("n is %d\n\
t[0][0] = %c, t[0][1] = %c, t[0][2] = %c\n\
t[1][0] = %c, t[1][1] = %c, t[1][2] = %c\n\
t[2][0] = %c, t[2][1] = %c, t[2][2] = %c\n", n, \
\
t[0][0],      t[0][1],      t[0][2], \
t[1][0],      t[1][1],      t[1][2], \
t[2][0],      t[2][1],      t[2][2]);
        
        if (t[0][0] == '_' || t[0][1] == '_' || t[0][2] == '_' || \
            t[1][0] == '_' || t[1][1] == '_' || t[1][2] == '_' || \
            t[2][0] == '_' || t[2][1] == '_' || t[2][2] == '_')
        {
            for (int j = 0; j < 3; j++)
            {
                if ((t[j][0] == t[j][1]) && (t[j][1] == t[j][2]))
                {
                       count++;
                }
            }
            for (int q = 0; q < 3; q++)
            {
                if ((t[0][q] == t[1][q]) && (t[1][q] == t[2][q]))
                {
                       count++;
                }
            }
            if ((t[0][0] == t[1][1]) && (t[1][1] == t[2][2]))
            {
                   count++;
            }
            else if ((t[0][2] == t[1][1]) && (t[1][1] == t[2][0]))
            {
                   count++;
            }
            if (count == 0)
            {
                printf("%d", 2);
            }
            else if (count == 1)
            {
                printf("%d", 1);
            }
            else if (count > 1)
            {
                printf("%d", 3);
            }
        }
        
    }
    return(0);
}

Output is as follows (count seems to be > 1, so 3 is printed):

Enter total number of iterations (n): 1

Enter 3 chars with spaces between them: x x x

Enter 3 chars with spaces between them: o o o

Enter 3 chars with spaces between them: _ _ _

n is 1
t[0][0] = x, t[0][1] = x, t[0][2] = x
t[1][0] = o, t[1][1] = o, t[1][2] = o
t[2][0] = _, t[2][1] = _, t[2][2] = _
3
Dharman
  • 30,962
  • 25
  • 85
  • 135
  • 2
    Code-only answers are rarely useful to future visitors with the same question. Better would be to explain the changes in words, relegating the code to an illustration of the explanation. – JaMiT May 18 '21 at 00:55
  • @JaMiT. Variety of reasons why I chose to answer as I did: 1. I didn't want to make my answer too long. 2. I didn't want to harp on the mistakes of the person who posted the question. 3. By showing actual execution I felt that: a. I could connect better with the questions asked. b. events were made plain & more explanation was unnecessary. 4. I have other things to do & I cannot spend more time. Good luck ! – Siddhartha Shivshankar May 24 '21 at 06:40