Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Reading a matrix in C

I’m trying to make a program in C that reads my matrix and calculates the sum of elements,
but it gives me an error at scanf("%d",&a[i]). This is the error :

      warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘int 
    (*)[10]’ [-Wformat=] 

and this is my code:

#include <stdio.h>

int main() { 
    int m,n,a[10][10],s=0;
    scanf("%d",&n);
    scanf("%d",&m);

    for(int i=1;i<=n;i++) 
        for(int j=1;j<=m;j++)
            scanf("%d",&a[i]);
 
 
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
            s=s+a[i][j];
    
    printf("%d",s);
    
    return 0;
}

Anyone know why?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

change scanf("%d",&a[i]);
to scanf("%d",&a[i][j]);
as you need to give him the address to write on but you give the address of array of ten int

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading