二维浮点数

时间:2015-11-14 03:36:49

标签: c arrays multidimensional-array double precision

我有两个问题:先关闭.....我有一个问题让这个工作。它要求输入我的程序的数字是双精度。我的编译器告诉我它希望我的数组是一个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;
}

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

2 个答案:

答案 0 :(得分:0)

我猜你因为

而得到错误
double rows;
double column;

和数组下标必须是整数int 所以把它改成

int rows;
int column;

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

答案 1 :(得分:0)

更改

int array_[4][5];

double array_[4][5];

因为如果你离开int,你基本上就是说你需要一个 int 的二维数组而不是 double 这就是为什么你可能得到的错误。并且正如@ whd所说,将行和列更改为int

相关问题