2D array search in C

12,012

Solution 1

Change this:

y[j] = j;
x[c] = c;

for this:

y[count] = j;
x[count] = c;

The arrays x and y are your results and they have to be indexed according to the number of results.

Solution 2

Change you code like this it will work fine :

Try this :

int flag = 0; 

printf("What letter are you lookiong for? ");

scanf("%c", &search);


for (j = 0; j < 5; j++)
{
    for (c = 0; c < 5; c++)
    {
        if (array[j][c] == search)
        {
           printf("%c is present at (%d , %d) times in array.\n", search, j, c);
           flag = 1;
        }
    }
}

if (flag == 0)
    printf("%c is not present in array.\n", search);

Solution 3

This statement of your code,

y[j] = j;
x[c] = c;

changes the elements at j position in y[] and c position in x[] to j and c.

B is present at 0,1 and also at 1,1 and so on.

So, for first round of loop for 'B', when j is 0 and c becomes 1 the value at y[0] will be initialized to 0 and x[1] to 1. (as search will be TRUE at 0,1) Again when the loop continues and j becomes 1 and c becomes 1, y[1] becomes 1, and x[1] again is 1 (overwritten, as in the previous loop, we did the same).

Because for all j, 0 to 4, the value of c will be 1 for 'B'.

So, for every value of y[0to4] which are 0,1,2,3,4, we are connected to the same c[1] which is 1.

But when you are printing the results, you are printing the wrong pair of results, you are printing y[0] with x[0] (tough you never saved any value in x[0], all values were saved in x[1] for 'B' ), y[1] with x[1] etc.

Only one result will be right this way, that will be y[1] with x[1] i.e. (1,1). For all other value, y[0,2,3,4], will be 0,2,3,4 but x[0,2,3,4] will be 0,0,0,0.

So, you logic is wrong, it should be like,

y[count]=j;
x[count]=c;

Now, for 'B', we are saving the value j i.e. 0 at y[0] with c i.e. 1 at x[0]. So when we print result, we get the correct pairs of positions and for every value of y[n] =0,1,2,3,4 we have a value saved in x[n]=1,1,1,1,1.

Share:
12,012
T.Malo
Author by

T.Malo

Nothing to see here..

Updated on June 04, 2022

Comments

  • T.Malo
    T.Malo almost 2 years

    I am still learning C and could use some help. I am trying write a program that will to do a search of a 2D array of char's. And have it tell you what points the searched for char is in relation to the 2D array in a (x y) coordinates. My problem is that the program is not outputting the right (x y) coordinates. enter image description here Now I thought the program should output (1,0),(1,1), (1,2), (1,3), (1,4) for B. I also plan on adjusting the coordinates so that it would count at 1 instead of 0 i.e for B output should be (2,1),(2,2), (2,3), (2,4), (2,5). So far the only coordinate that prints out right is (1,1) and I am not sure why my code does not work. What can I do to fix this?

    FULL CODE:

    #define _CRT_SECURE_NO_WARNINGS
    #define SIZE 5
    #include <stdio.h>
    
    
    int main()
    {
        int c, count = 0;
        int x[SIZE] = { 0 };
        int y[SIZE] = { 0 };
    
        int j, i;
        char array[SIZE][SIZE] = { { 0 }, { 0 } };
        char array2[SIZE] = { 'A', 'B', 'C', 'D', 'S' };
        char search;
    
        for (i = 0; i < 5; i++)
        {
            for (j = 0; j < 5; j++)
            {
    
                array[i][j] = array2[j];
            }
        }
    
        for (i = 0; i < 5; i++)
        {
            for (j = 0; j < 5; j++)
            {
                printf("%c ", array[i][j]);
            }
            printf(" \n");
        }
    
        printf("What letter are you lookiong for? ");
        scanf("%c", &search);
    
    
        for (j = 0; j < 5; j++)
        {
            for (c = 0; c < 5; c++)
            {
                if (array[j][c] == search)
                {
                    y[j] = j;
                    x[c] = c;
                    count++;
                }
            }
        }
    
        if (count == 0)
            printf("%c is not present in array.\n", search);
        else
        {
            for (i = 0; i < count; i++)
            {
    
                printf("%c is present at (%d , %d) times in array.\n", search, x[i], y[i]);
            }
    
        }
    
    
    
        return 0;
    }