【问题标题】:Double precision numbers in a 2-d float二维浮点数中的双精度数
【发布时间】:2016-02-15 17:57:16
【问题描述】:

我有 2 个问题:首先......我在让它工作时遇到了问题。它要求输入到我的程序中的数字是双精度的。我的编译器告诉我它希望我的数组是一个 int。

我无法摆脱的两个错误在这些行中

scanf("%lf" ,&array_[rows][column]);

和:

sum += array_[rows][column]; //formula for calculating sum

我将所有变量都更改为整数,程序按预期运行。我只是不知道如何使这项工作成为精度双精度数。 提前致谢

这是我的完整代码和程序说明:

/*
 ============================================================================
 Name        : 4.c
 Author      : 
 Version     :
 Copyright   : Your copyright notice
 Description : This program creates a 5 row 5 column 2d array.
 The array contains double precision numbers that are then passed to a function
 that adds them, returns the value. It is then displayed to the user
 ============================================================================
 */
#include <stdio.h>
#include <stdlib.h>

int main()
{
    setvbuf(stdout,NULL,_IONBF,0);
    int array_[4][5];
    double rows;
    double column;
    double sum;
    sum=0;

    for(rows=0;rows<4;rows++) //Inputs users double precision numbers to 2d array
    {
        for(column=0;column<5;column++)
        {
            printf("\nEnter the values value for array position row %lf, column %lf\t" ,rows,column);
            scanf("%d" ,&array_[rows][column]);
        }
    }


    for(rows=0;rows<4;rows++) //double for loop calculates sum of 2d array
    {                       // first loop is for rows second for columns
        for(column=0;column<5;column++)
        {
            sum += array_[rows][column]; //formula for calculating sum
        }
    }

    printf("\nThe sum of this 2-D array is %lf /n" ,sum); //displays sum to user

    system("pause");
return EXIT_SUCCESS;
}

第二个问题:如果您有一个数组,但您希望用户指定它的行数和列数,您将如何初始化它?

【问题讨论】:

  • "如果你有一个数组,但是你希望用户指定它的行数和列数,你将如何初始化它?" 阅读后声明它尺寸(并使用 C99 编译器)。

标签: c arrays multidimensional-array double precision


【解决方案1】:

改变

int array_[4][5];

double array_[4][5];

因为如果你离开 int,你基本上是在说你想要一个 int 而不是 double 的二维数组,这就是你可能会收到错误的原因。也正如@whd 所说,将行和列更改为int

【讨论】:

    【解决方案2】:

    我猜你会因为

    而出错
    double rows;
    double column;
    

    并且数组下标必须是整数int 所以改成

    int rows;
    int column;
    

    关于第二个问题,这可能会有所帮助http://www.geeksforgeeks.org/dynamically-allocate-2d-array-c/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-02-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-19
      • 1970-01-01
      相关资源
      最近更新 更多