1

I have an array [8][8]

char d[ROWS][COLS] = {
        {0, 2, 0, 2, 0, 2, 0, 2},
        {2, 0, 2, 0, 2, 0, 2, 0},
        {0, 2, 0, 2, 0, 2, 0, 2},
        {1, 0, 1, 0, 1, 0, 1, 0},
        {0, 1, 0, 1, 0, 1, 0, 1},
        {3, 0, 3, 0, 3, 0, 3, 0},
        {0, 3, 0, 3, 0, 3, 0, 3},
        {3, 0, 3, 0, 3, 0, 3, 0}};


int countOccurrences(char d[ROWS][COLS], int i, int j, int res)
{
    res = 0;
    for (i=0; i<=ROWS; i++){
        for(j=0; j<=COLS; j++){
            if ('2' == d[ROWS][COLS])
            res++;
            
        }
    }      
    return res;
    printf("score%d", res);

} 

my code doesn't work and I need to find and count the occurrences in 2D array and print it out

Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
  • Arrays start at index 0 and end at index "size-1". Therefore `i=0; i<=ROWS;` doesn't make any sense, you access the array out of bounds. – Lundin Oct 25 '21 at 06:35
  • https://shrib.com/#Mara4rMnyRK here 's my code, i made changes you suggested me but somehow it return crazy numbers, i would appreciate your help) anyways its not for school or anything just trying to learn, could you please check my code briefly and tell me why it doesnt work as it should? – Hazel Hochmann Oct 25 '21 at 10:16
  • If you still have problems after implementing the suggestions in the posted answers, then you could ask a new question and post the code there. – Lundin Oct 25 '21 at 10:24

2 Answers2

2

I made a comment, but I will also go ahead and respond with the fixed function. There were three main issues in the code that I'll point out:

  1. In your code, you have numbers within the array, but then you try comparing a char to a number ('2' == 2 is false), so that means your result will be 0 over all
  2. The print statement was after the return, which means it won't actually run.
  3. the if statement if (2 == d[ROWS][COLS]) is always going to check the number at position ROWS and COLS. The first issue there is that ROWS and COLS aren't changing positions, that's a constant; you want to check at the position i and j. Second, since array's in C are zero-based, accessing d at position [ROWS][COLS] will actually point to some unknown memory space since that's beyond the scope of the array, but that's just an interesting fact.

The following code should fix the problems I pointed out above:

char d[ROWS][COLS] = {
    {0, 2, 0, 2, 0, 2, 0, 2},
    {2, 0, 2, 0, 2, 0, 2, 0},
    {0, 2, 0, 2, 0, 2, 0, 2},
    {1, 0, 1, 0, 1, 0, 1, 0},
    {0, 1, 0, 1, 0, 1, 0, 1},
    {3, 0, 3, 0, 3, 0, 3, 0},
    {0, 3, 0, 3, 0, 3, 0, 3},
    {3, 0, 3, 0, 3, 0, 3, 0}};

int countOccurrences(char d[ROWS][COLS], int i, int j, int res) {
    res = 0;

    // NOTICE that it is now i < ROWS and j < COLS instead of
    // i <= ROWS and j <= COLS to deal with what I mentioned
    // before about the zero-based indexing in C, which means
    // we need to stop BEFORE ROWS and COLS
    for (i = 0; i < ROWS; i++) {
        for (j = 0; j < COLS; j++) {
            if (2 == d[i][j])
                res++;
        }
    }      

    printf("score%d", res);
    return res;
}
charlie-map
  • 556
  • 5
  • 17
  • There's still an issue with `i<=ROWS` and `j<=COLS` (from the original code) - this is going to cause `d[i][j]` to exceed the array bounds. – sj95126 Oct 25 '21 at 01:32
  • correct @sj95126 -- I fixed the code to correctly correspond with that. – charlie-map Oct 25 '21 at 01:58
0

There are some problems with your code:

  • You have a char '2', which will transform to whatever character encoding that your system use, in my case, ASCII, which will be saved by the value 50, and you are comparing it with the value 2, which will return false.
  • You're comparing d[ROWS][COLS] every time, that's probably not what you want.
  • In your loop, there will be times that it will try to access d[ROW][...], d[...][COL], which will create an out of bounds error.
  • You make too many unnecessary arguments for your function.
  • You return before printf(), so printf() never get executed.

Suggested code:

char d[ROWS][COLS] = {
        {'0', '2', '0', '2', '0', '2', '0', '2'},
        {'2', '0', '2', '0', '2', '0', '2', '0'},
        {'0', '2', '0', '2', '0', '2', '0', '2'},
        {'1', '0', '1', '0', '1', '0', '1', '0'},
        {'0', '1', '0', '1', '0', '1', '0', '1'},
        {'3', '0', '3', '0', '3', '0', '3', '0'},
        {'0', '3', '0', '3', '0', '3', '0', '3'},
        {'3', '0', '3', '0', '3', '0', '3', '0'}};


int countOccurrences(char anotherName[], int ROWS, int COLS)
{
    int res = 0;
    for (int i=0; i<ROWS; i++){
        for(int j=0; j<COLS; j++){
            if ('2' == anotherName[i][j])
            ++res;
            
        }
    }      
    printf("score%d", res);
    return res;
} 
// You should call it like this: int something = countOccurrences(d, ROWS, COLS);