如何创建二维数组 [英] How to create a 2D array

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

问题描述

我仍然是C编程的初学者,我需要一些帮助来为我的C编程类编写代码.提示是:该程序的输入是位于名为textfile94的文件中的浮点数据的二维数组.输入数组将包含3行数据,每行包含5列数据的行.

Im stil a beginner in C programming and I need a little help writing a code for my C programming class . The prompt is: Input for this program is a two-dimensional array of floating point data located in a file named textfile94. The input array will contain 3 rows of data with each row containing 5 columns of data.

  • 我希望您使用动态内存分配的两个下标方法.
  • 使用malloc创建一个保存指针的数组.
  • 该数组的每个元素都指向另一个数组,即行数据.
  • 在循环中使用malloc创建行.
  • 然后,您可以使用两个下标运算符[r] [c]来获取数据进行程序所需的求和和平均.

此程序需要预先编码的2D数组的高度和宽度的硬编码(实际上是3x5).我希望您不要创建代码中的文字数字,而是要创建一个全局常量变量来保存这些维度,并在代码中使用它们.

This program calls for hard-coded height and width of the 2D array, known ahead of time (3x5, actually). Instead of writing in the literal numbers in your code, I want you to create a global constant variable to hold those dimensions, and use those in your code.

这是我到目前为止所拥有的(我不确定它是否正确):

Here is what I have so far (I'm not sure if its correct):

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

#define int rows = 3;
#define int columns = 5;

float array[rows][columns];

int main(int argc, char* argv[]){
FILE* fin;
float x;
int i,j;
int* array;

fin = fopen("textfile94", "r");
fscanf("%f", &x);
array = (int*) malloc(rows*sizeof(int*));

for(i=0;i<rows;i++){
   for(j=0;j<columns;j++)
   array[i]=(int*) malloc(columns* sizeof(int));
}
  }
printf("The Average values for the three rows are:%f",array[rows]);
printf("The Average values for the five columns are:%f", array[columns]);

return 0;
}

推荐答案

由于您已经知道数组(3x5)的尺寸,因此不需要使用 malloc()动态分配它.br>您所拥有的是正确的(但应将其设置为本地而不是全局):

Since you already know the dimensions of the array(3x5) you do not need to allocate it dynamically using malloc().
What you have is correct(but you should make it local instead of global):

float array[3][5];

这已经分配了足够的内存来容纳所有数组元素.

This already allocates enough memory to hold all the array elements.

您的要求也注明:

我希望您创建一个全局常量变量来保存这些尺寸,并在代码中使用它们.

I want you to create a global constant variable to hold those dimensions, and use those in your code.

这意味着您需要声明常量以表示数组元素,例如:

Which means you need to declare constants to indicate array elements, something like:

const int rows = 3;
const int columns = 5;

float array[rows][columns];

从您的评论中,我相信您使用的是c89(或更早版本).在c99可变长度数组(如果您不了解此,请在Google上查找 VLA ),这意味着上面的代码可以编译.但是在c98中不允许使用VLA.C89和更早的版本要求使用数组维度的编译时常量表达式,因此您将需要使用编译时常量表达式(const限定变量不在C中).因此,您将需要使用:

From your comments, I believe you are using c89(or earlier version).In c99 variable length Arrays(look up VLA on google if you are not aware of this) are allowed,Which means above would compile.But in c98 VLA are not allowed. C89 and earlier versions require using compile-time constant expressions for the array dimension.So you will need to use compile-time constant expressions (which const-qualified variables are not in C). So you will need to use:

#define ROWS 3
#define COLUMNS 5

float array[ROWS][COLUMNS];

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

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