【问题标题】:2 Dimensional array and Print out average of the Columns and Rows2 维数组并打印出列和行的平均值
【发布时间】:2011-12-23 22:34:44
【问题描述】:

我仍然是 C 编程的初学者,我需要一些帮助来为我的 C 编程课程编写代码。

提示是:该程序的输入是一个二维浮点数据数组,位于名为 textfile94 的文件中。输入数组将包含 3 行数据,每行包含 5 列数据。

  • 我要你用动态内存的二下标法 分配。
  • 使用 malloc 创建一个包含指针的数组。
  • 该数组的每个元素都指向另一个数组,即数据行。
  • 在循环中使用 malloc 来创建行。
  • 然后您可以使用两个下标运算符 [r][c] 来获取您的数据 进行程序要求的求和和平均。
  • 此程序要求对二维数组的高度和宽度进行硬编码, 提前知道(实际上是 3x5)。
  • 我希望您不要在代码中写入文字数字,而是 创建一个全局常量变量来保存这些维度,并使用 那些在你的代码中。

这是我的代码:

#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(fin,"%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[i]);
    printf("The Average values for the five columns are:[%f]", array[j]);
    return 0;
}

在文本文件中:4.33 5.33 1.11 99.00 100.00 1.0 33.3 12.5 1.1 -1000.00 22.1 11.9 2.4 8.3 8.9

程序应该输出: 三行的平均值为:41.95 -190.42 10.32 五列的平均值为:9.14 16.84 5.33 36.13 -297.7

无法正确执行此操作,我们将不胜感激。我不想要我想从中学习的答案,但只需要一些提示。谢谢。

更新代码:

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

#define ROWS 3
#define COLUMNS 5

float array[ROWS][COLUMNS];

int main(int argc, char* argv[]){

FILE* fin;
int i;
float x;
float** array;

fin = fopen("textfile94", "r");
array=(float**) malloc(ROWS*sizeof(float*));
    for(i=0;i<ROWS;i++)
      array[ROWS]=(float*)malloc(COLUMNS*sizeof(float));
    for(j=0;j<COLUMNS;j++){
      fscanf(fin,"%f",&x);
      x = array[ROWS][COLUMNS];
}
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;
}

【问题讨论】:

    标签: c multidimensional-array


    【解决方案1】:

    好的,我看看我能补充什么。

    定义不是这样写的,按照惯例应该全部大写

    #define ROWS 3
    #define COLUMNS 5
    

    他希望您通过 malloc 动态分配数组,您现在静态分配一个二维浮点数组,然后您尝试强制将整数数组放入其中。你应该看看如何用 malloc 做多维数组。

    基本上你想要的是

    float **array;
    

    现在数组是一个指向浮点数的指针,然后将数组行数分配给浮点数。

    ROWS * sizeof(float*)
    

    之后,您可以为每一行分配 array[row]

    COLUMNS * sizeof(float)
    

    现在您有了数组[ROWS][COLUMNS] 结构

    以pythonesque伪代码读取数据的一种方法是

    for(row 1..3)
        array[row] = malloc(...)
        for(col 1..5)
            fscanf(value)
            array[row][col] = value
    

    如果我太含糊了,请告诉我,试图给出提示而不给出代码。

    【讨论】:

    • 好吧,我想我明白了一点,我要试试。我觉得我正在用这段代码兜圈子,但从长远来看,不想要答案会对我有所帮助。
    • 我对最后一部分感到困惑:然后 malloc 一个行大小的 float* 块将其分配给数组,然后 malloc cols 的 float 分配给它。
    • 是的,我看看我能不能把它清理掉。但从好的方面来说,我刚刚对其进行了测试,一旦我们弄清楚指针,你就离我们不远了。
    • 我添加了 malloc 的大小声明,因为它们可能很棘手,而且我认为它们除了具有在书籍/互联网中查找它们的技能之外没有增加太多
    • 好的,到目前为止我已经添加了所有内容,我是否需要像我的代码中那样为列部分使用 for 循环?它还表示来自此“array[ROWS]=(float**)malloc(COLUMNS*sizeof(float));”的“不兼容指针类型的赋值”我不知道那是什么意思:(
    【解决方案2】:

    这应该让您开始了解如何分配数组、分配和访问值,然后释放内存。为清楚起见,省略了错误检查。您很可能需要验证 calloc 是否确实返回了一个有效指针。

    要完成程序,您必须将值读入数组,然后计算平均值。

    #include <stdlib.h>
    #include <stdio.h>
    
    const size_t rows = 3;
    const size_t columns = 5;
    
    int main(void)
    {
        size_t i, j;
        /* allocate a two-dimensional array of zeroes */
        double **array = calloc(1, rows * sizeof(double *));
        for (i = 0; i < rows; ++i) {
            array[i] = calloc(1, columns * sizeof(double));
        }
        /* print it out - replace this by reading in values */
        for (i = 0; i < rows; ++i) {
            for (j = 0; j < columns; ++j) {
                fprintf(stdout, "%.2f", array[i][j]);
                fputc(' ', stdout);
            }
            fprintf(stdout, "\n");
        }
        /* TODO loop through the array again and average the data */
        /* free memory */
        for (i = 0; i < rows; ++i) {
            free(array[i]);
        }
        free(array);
        return EXIT_SUCCESS;
    }
    

    【讨论】:

    • 它不使用 calloc 我假设使用 malloc 并且它是浮点数而不是双精度数
    • callocmalloc 在功能上是等效的,calloc 只是为您清零分配的内存,这通常是一种很好的编程习惯。将double 更改为float 非常简单。
    • 好吧,我只是有点困惑,我还是完成了,代码会更新新的。
    猜你喜欢
    • 1970-01-01
    • 2021-06-20
    • 2020-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多