-2

I got an exercise to do I need to find in matrix what line do i have even number All numbers in a row should be double

for exemple: in matrix matrix[R][C]={{8,1,2},{3,7,5},{6,2,14},{13,8,15},{8,0,2,},{4,50,26},{2,84,11},{12,36,9}};
Example output: 2,4,5

That's what I did, I have to do it with Pointer, What am I doing wrong ? on the input i get: 0,1,2

#define R 8
#define C 3   
void *ckeckme(int **m)
{
    int **p,j,i;

    for(p=m;p-m<C;p++){
    if(isEven(&p)==1)
    printf("%d",p-m);
    }
}

int isEven(int *v)
{
    int *ptr,conut=0,cc=0;
    int i,j;                                        
    for(j=0;j<R;j++)
    for(i=0; i<C; i++){

        if(*(v + i+ j)%2==0)
                conut++;

    if(conut==3)
    {

    return 1;
    conut=0;
    }
}

}    
int main() {

int x,i,j,matrix[R][C]={    {8,1,2},
                    {3,7,5},
                    {6,2,14},
                    {13,8,15},
                    {8,0,2},
                    {4,50,26},
                    {2,84,11},
                    {12,36,9}};
    *ckeckme(matrix);

}    
B RAMI
  • 19
  • 1
  • 5

1 Answers1

0

First: I'd start from reading all the warnings, I guess there are lot of them, starting from checkme returning no value despite declared type (void*) — you probably don't get what asterisk means. In function declaration it means that it return void pointer (no specified data type) and in the call it would be derefference, but how compiler allows you do that, I don't know.

Second: next time format your code properly. It's no wonder why you cannot get it, it's a disaster. Especially that any reasonable editor can do it automatically.

Surely there is problem with checkme: for(p=m;p-m<C;p++){ — that looks ridiculus. C name is misleading because it's number of rows and not opposite. By the way, why you ommit last 3 columns?

if(isEven(&p)==1) — I don't know it doesn't crash. & returns pointer so you pass pointer to matrix when it was intended to pass one row.

An then in even you seems to check whole matrix again. better come back to stuff you understand anyhow. Oh, and the values were supposed to be doubles, noti ints.

gcc -Wall -pedantic -Werror yourcode.c #compile with strict rules
/tmp/x.c: In function ‘ckeckme’:
/tmp/x.c:8:8: error: implicit declaration of function ‘isEven’ [-Werror=implicit-function-declaration]
     if(isEven(&p)==1)
        ^~~~~~
/tmp/x.c:9:5: error: implicit declaration of function ‘printf’ [-Werror=implicit-function-declaration]
     printf("%d",p-m);
     ^~~~~~
/tmp/x.c:9:5: error: incompatible implicit declaration of built-in function ‘printf’ [-Werror]
/tmp/x.c:9:5: note: include ‘<stdio.h>’ or provide a declaration of ‘printf’
/tmp/x.c:9:14: error: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘long int’ [-Werror=format=]
     printf("%d",p-m);
             ~^  ~~~
             %ld
/tmp/x.c:5:15: error: unused variable ‘i’ [-Werror=unused-variable]
     int **p,j,i;
               ^
/tmp/x.c:5:13: error: unused variable ‘j’ [-Werror=unused-variable]
     int **p,j,i;
             ^
/tmp/x.c: In function ‘isEven’:
/tmp/x.c:15:22: error: unused variable ‘cc’ [-Werror=unused-variable]
     int *ptr,conut=0,cc=0;
                      ^~
/tmp/x.c:15:10: error: unused variable ‘ptr’ [-Werror=unused-variable]
     int *ptr,conut=0,cc=0;
          ^~~
/tmp/x.c: In function ‘main’:
/tmp/x.c:42:14: error: passing argument 1 of ‘ckeckme’ from incompatible pointer type [-Werror=incompatible-pointer-types]
     *ckeckme(matrix);
              ^~~~~~
/tmp/x.c:3:7: note: expected ‘int **’ but argument is of type ‘int (*)[3]’
 void *ckeckme(int **m)
       ^~~~~~~
/tmp/x.c:42:5: error: dereferencing ‘void *’ pointer [-Werror]
     *ckeckme(matrix);
     ^~~~~~~~~~~~~~~~
/tmp/x.c:34:9: error: unused variable ‘j’ [-Werror=unused-variable]
 int x,i,j,matrix[R][C]={    {8,1,2},
         ^
/tmp/x.c:34:7: error: unused variable ‘i’ [-Werror=unused-variable]
 int x,i,j,matrix[R][C]={    {8,1,2},
       ^
/tmp/x.c:34:5: error: unused variable ‘x’ [-Werror=unused-variable]
 int x,i,j,matrix[R][C]={    {8,1,2},
     ^
/tmp/x.c: In function ‘ckeckme’:
/tmp/x.c:11:1: error: control reaches end of non-void function [-Werror=return-type]
 }
 ^
/tmp/x.c: In function ‘isEven’:
/tmp/x.c:31:1: error: control reaches end of non-void function [-Werror=return-type]
 }
 ^

I assume you cannot think it yourself so consider this code:

#include <stdio.h>
#include <math.h>

int main(void) {
    double matrix[8][3]={ {8,1,2},
                    {3,7,5},
                    {6,2,14},
                    {13,8,15},
                    {8,0,2},
                    {4,50,26},
                    {2,84,11},
                    {12,36,9}};

    for( int i = 0; i < 8; i++ ) {
        int j;
        for( j = 0; j < 3; j++ ) {
            if( fmod( matrix[i][j], 2 ) != 0 ) // % operator is only for ints
                break;
        }

        if( j == 3 )
            printf( "row %d is all even\n", i+1 );
    }

    return 0;
}    

Only thing you have to do is to do it with pointers. ;)

Leo
  • 31
  • 1