C ++ 2d数组......在原型上丢失了,并且在任何地方都没有看到

时间:2017-10-12 02:29:33

标签: c++ arrays function prototype

  1. 我需要帮助原型。
  2. 我需要帮助来电。
  3. 我需要有关函数头的帮助。
  4. 我不知道这个网站想要我添加多少细节,但代码说明了一点,我在前三个项目上遇到了问题,但由于某种原因,该网站需要更多上下文?

    #include <iostream>
    using namespace std;
    
    void fillArray(int salesSheet[][COLSIZE], int rowSize);
    // I dont know how to create prototype for 2d array
    int main()
    {
        const int ROWSIZE = 5, COLSIZE = 6;
        int salesSheet[ROWSIZE][COLSIZE], a, rowSize;
    
        fillArray(a[][COLSIZE], int rowSize);
        // I don't know where I messed this up 
        // I messed up the caller too, help?
        return 0;
    }
    void fillArray(int salesSheet[][COLSIZE], int rowSize)
    {
        int r, c;
        cout <<"Enter sales report for each quarter, for your branch." << endl;
        for (c = 0; c < COLSIZE; c++) 
        {
            for (r = 0; r < rowSize; r++)
                cout << "\nBranch " << c+1 << " quarterly sales figures:"
            cin >> salesSheet[r][c];
        }
    }
    

2 个答案:

答案 0 :(得分:0)

#include <iostream>
#include <malloc.h>
using namespace std;
const int ROWSIZE = 5, COLSIZE = 6;
void fillArray(int*** salesSheet, int rowSize, int colSize);

int main()
{
    int** salesSheet = (int**)malloc(COLSIZE*sizeof(int*));
    int* arr = (int*)malloc(ROWSIZE*COLSIZE*sizeof(int));

    for(int i = 0; i < COLSIZE; i++)
      salesSheet[i] = arr+i;

    fillArray(&salesSheet, ROWSIZE, COLSIZE);
    free(salesSheet);
    free(arr);
    return 0;
}
void fillArray(int*** salesSheet, int rowSize, int colSize)
{
    int r, c;
    cout <<"Enter sales report for each quarter, for your branch." << endl;
    for (c = 0; c < COLSIZE; c++) 
    {

        for (r = 0; r < rowSize; r++){
            cout << "\nBranch " << c+1 << " quarterly sales figures:";

            cin >> salesSheet[0][r][c];
        }
    }
}

答案 1 :(得分:0)

#include <iostream>
using namespace std;

const int ROWSIZE = 5, COLSIZE = 6;

void fillArray(int salesSheet[][COLSIZE]);

int main()
{
int salesSheet[ROWSIZE][COLSIZE];

fillArray(salesSheet);

return 0;
}
void fillArray(int salesSheet[][COLSIZE])
{
int r, c;
cout <<"Enter the sales report for each quarter, in you branches column."    
<<  endl;
for (c = 0; c < COLSIZE; c++) 
    {
    for (r = 0; r < ROWSIZE; r++)
        {
            cout << "\nBranch " << c+1 << " quarterly sales figures:" ;
            cin >> salesSheet[r][c];    
        }
    }
}