2维度传递(缺少下标,大小未知)

2 Dimension Passing (missing subscript, unknown size)

本文关键字:未知 下标 2维      更新时间:2023-10-16

我经常得到缺少的下标和未知大小问题。因此,我猜这是一个初学者的问题,但我无法围绕它。如何使我的功能工作并输出到屏幕上?

我试图让两个列充满数字。列[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);
    }
}

它工作:

#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;
    }
}