C中的多维数组问题 [英] Multidimensional Array Problem in C.

查看:88
本文介绍了C中的多维数组问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从一个文件中读取如下:

I wanna read from one file which will be like this:

10
1 7
9 7
1 1
9 1
2 4
8 4
6 5
6 3
4 3
4 5





数字。第一个是N并且告诉我们应该读多少行。

在我阅读之后我必须将它们打印回给用户,这就是问题所在。这是我的代码:



the numbers. The first is the N and tell us how much lines should we read.
After i read them i have to print them back to the user and thats the problem. Here is my code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    int N, simeio[N][N], i, x ,y;
    FILE *fp;
    
    fp = fopen("pulsars.in","r");
    fscanf(fp,"%d", &N);
    
    for(i=0; i<N; ++i)
    {
             fscanf(fp,"%d %d", &x, &y);
             simeio[x][y] = i;
    }
    
    fclose(fp);
    
    for(i=0; i<N; ++i) printf("%d %d", simeio[i][i]);
    
    getchar();
}





问题出在哪里?请尽快回答我。

感谢您的时间。



Where is the problem? Please answer me ASAP.
Thanks for your time.

推荐答案

这是您的家庭作业。所以没有人会在这里为你做这件事。



但是,我有一个提示给你:



编译你的代码。编译器错误消息将帮助您找到大部分问题。
This is your home work. So nobody will do it for you here.

However, I have one tip for you:

Compile your code. The compiler error messages will help you to locate most of your problems.


许多错误!



首先,你有在为数据赋值之前,使用变量N初始化数组!尝试使用
Many mistakes!

First of all, you have initialized the array using the variable N, before assigning a value to it! Try using
int * simeio;

(指向int的指针,稍后将充当数组)和在从文件中读取N的值后,使用malloc()分配内存。请查看 www.cplusplus.com/malloc [ ^ ]有关malloc()的信息。



其次,根据您的文件pulsars .in,它看起来不像NxN阵列。它看起来像我作为Nx2阵列。因此,您可以将以下代码

(a pointer to int, which will act as an array later) and use malloc() to allocate memory after you have read the value for N from the file. Take a look at www.cplusplus.com/malloc[^] for information on malloc().

Second, according to your file "pulsars.in", it doesn''t look like an NxN array. It looks like to me as an Nx2 array. So you can change the following code

int simeio[N][N]

更改为

int simeio[N][2]





第三,您需要更正代码

.

Third, you need to correct the code

simeio[x][y] = i;

如下:

as following:

simeio[i][0] = x;
simeio[i][1] = y;



另外,插入一些printf语句到检查是否正确读取了文件中N,x和y的值。然后你应该没问题。



祝你好运:)


Additionally, insert some printf statements to check whether you read the values for N, x, and y from the file correctly. Then you should be OK.

Good luck :)


不要让我们尽快回答。我们这样做,根据定义,当可能意味着,当我们倾向于给学习者免费帮助时。



您是否有权使用调试器?为什么要发布你的代码并说问题出在哪里?但不告诉我们问题是什么?



您的代码在哪里读取第一行以获取N的值?我看到不能编译的行,你还需要在N有值时定义你的数组,并使用指针。你应该考虑阅读你的错误信息,谷歌搜索和阅读你的教科书,而不是要求我们做你所有的工作。
Don''t ask us to answer ASAP. We do that, by definition, when ''possible'' means, when we are inclined to give learners free help.

Do you not have access to a debugger ? Why do you post your code and say ''where is the problem ?'' but not tell us WHAT the problem is ?

Where does your code read the first line to get a value for N ? I see lines that won''t compile, you also need to define your array when N has a value, and using pointers. You should consider reading your error messages, googling them, and reading your textbooks instead of asking us to do all your work.


这篇关于C中的多维数组问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆