English 中文(简体)
如何用用户投入价值填补C的2D阵列?
原标题:How to fill a 2D array in C with user input values?
  • 时间:2011-11-21 11:31:18
  •  标签:
  • c
  • arrays

注:这是一个家庭工作问题。

Use FOR construction to fill 2D board with values that were given by user. The program asks for board size n, m and then it asks for each board value.

My try

#include <stdio.h>
int main(){
    printf("Enter the number of columns");
    int i = scanf("%d",&i);
    printf("Enter the number of rows");
    int y = scanf("%d",&y);

    int r[i][y];
    int a;
    int b;
    for (a=0; a<i; a++){
        for(b=0; b<y; b++){
            int r[a][b] = scanf("%d",&a,&b); //bug
        }
    }
}

<代码>Bug: c:13个变型物体不得入选

http://www.un.org。

#include <stdio.h>

int main(){

    printf("Enter the number of columns");
    int i; 
    scanf("%d", &i);
    printf("Enter the number of rows");
    int y; 
    scanf("%d", &y);

    int r[i][y];
    int a;
    int b;

        for (a=0; a<i; a++){
            for (b=0; b<y; b++){
    scanf("%d",&r[a][b]);
        }
    }
}
问题回答

>>scanf 处理正在读到的变量,将项目编号改为。 该数值不折成美元。

替换

int i = scanf("%d",&i);
int y = scanf("%d",&y);

页: 1

scanf("%d",&i);
scanf("%d",&y);

以及

int r[a][b] = scanf("%d",&a,&b);

页: 1

scanf("%d",&r[a][b]);

http://www.un.org。

You are using variable length array (VLA) in your program:

int r[i][y];

as i 以及 y are not constants 以及 are variables. VLA are a C99 st以及ard feature.

你们必须积极分配2D阵列,否则就不知道它的规模。

替换

int r[i][y];

iii

int *r = malloc(i*y*sizeof(int));

增加:

free(r);

* 以及SCANF错误,人们已经在这里回答。

首先,<代码>scanf的回报值 页: 1

第二,C无法通过使用变量创建阵列。 你们首先必须积极分配一个阵列。 在第一阵列的每一条目中,你必须另设一个阵列。

Don!

The use of scanf(%d, &var) is incorrect.
scanf reads from console an integer (this type is specified by its first paramenter %d) and stores it in the second parameter.
This second parameter must be a pointer, so an & is needed when your variable is not a pointer yet.

因此,你应该以这种方式纠正你的法典:

int i;    
scanf("%d",&i);

int y;
scanf("%d", &y);

for 住宿

scanf("%d", &r[a][b]);

由于直线缓冲,没有打印电文。

If you add to your strings (to start a new line), it might do what you expect:

printf("Enter the number of columns
");

或者,如果你真的希望用户在同一线路上进行分类,那么你需要人工冲动缓冲:

printf("Enter the number of columns");
fflush (stdout);




相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签