【发布时间】: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