简体   繁体   中英

Scanf matrix is not working in c?

here is the code, its not working; the problem is you need to read a number, that will be the column that will be sum if c='S' or Calculate the average of an array[12][12], and print at the end

i'm using while because a thought was the for statement that wasn't working but it is something else

#include<stdio.h>
#include<stdlib.h>
int main(){
    float v[12][12];
    int col,i=0,j=0;
    char c;
    float soma=0;

    scanf("%d",&col);
    scanf("%c",&c);


    while(i<12){
        while(j<12){
            scanf("%f",&(v[i][j]));
            j++;
        }
            i++;

        }

    for(i=0;i<12;i++)
    {
        soma+=(v[i][col]);
    }
    if(c=='S') printf("%.1f\n",soma);
    else printf("%.1f\n",soma/12);
    system("pause");
    return 0;
}
    scanf("%d",&col);
    scanf(" %c",&c); <----- space before %


    while(i<2)
    {
        j=0;   <----- j reset to zero
        while(j<2){
            scanf("%f",&v[i][j]);
            j++;
        }
            i++;
    }

space before %c because it reads the enter after you input col , read this for further information: problem in scanning character in C

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM