我不断得到一个缺失的下标和未知的大小问题。所以,我猜这是一个初学者的问题,但我无法理解它。如何使我的功能工作并输出到屏幕?
我想让两列填充数字。列[0]由rand()输入,然后通过等式将列[1]转换为新数字。我期待输入1-10行。
// function prototypes
void arrayProgram(double ma[][2], int xSize);
int main()
{
const int arraySize = 5;
double ma[arraySize][arraySize];
// if I change double ma[1][2];
// I get an argument of type 'int' is incompatible of type "double(*)[2]
arrayProgram(ma, arraySize);
}//end main
void arrayProgram(double ma[][2], int xSize)
{
int i = 0;
for (i = 0; i < xSize; ++i)
{
ma[i][0] = rand();
ma[i][1] = (ma[i][0] * (20 / 25.0) + 64);
}
}
答案 0 :(得分:0)
有效:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
// function prototypes
void arrayProgram(double ma[][2], int xSize);
int main()
{
const int arraySize = 1;
double ma[arraySize][2];
srand ( time(NULL) ); // setting seed value
rand(); // first random number
arrayProgram(ma, arraySize);
}//end main
void arrayProgram(double ma[][2], int xSize)
{
int i = 0;
for (i = 0; i < xSize; ++i)
{
ma[i][0] = rand();
ma[i][1] = (ma[i][0] * (20 / 25.0) + 64);
std::cout << ma[i][0] << '\t' << ma[i][1] << std::endl;
}
}