Passing 2D Array - Error

I am passing the array ary into the function fillArray and am getting a "No matching function for call to.." error at the point where I call the function inside of main().
To my knowledge, from what I've been taught and from my textbook, the syntax is correct. Any idea why I am getting this error?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
using namespace std;

int getCols(int);
int COLS = getCols(COLS);
int ROWS;

void fillArray(int [][COLS]);


int main()
{
    int ary[100][100];
    fillArray(ary);
}


int getCols(int c)
{
    cout << "Enter cols ";
    cin >> c;
    return c;
}


void fillArray(int ary[ ][COLS])
{
    for(int j=0; j < ROWS; j++)
    {
        for(int i=0; i < COLS; i++)
        {
            ary[ROWS][COLS] = ary[j][i];
        }
    }
}
Because the array really has 100 columns even though you are only using X amount of columns.
void fillArray(int ary[ ][COLS])
should be

void fillArray(int ary[ ][100])


Also ary[ROWS][COLS] = ary[j][i]; what are you trying to do???

Assign uninitialized values to possibly out of bounds position?


Maybe you mean something like:
ary[j][i] = i * j;

it would be a little more help if we knew how you were trying to fill the arrays such as indexing from 0->row * columns all 0 , all 1 ect...

*The downfall is you probably will have problems if you try and pass an array with less or more columns ex:

 
int ary[100][99];
will have problems since it has 99 instead of 100 columns.

You could do something like this though:

1
2
3
4
5
template <int Rows , int Columns>
void fillArray( int (&arr)[Rows][Columns] )
{
    //stuff
}


http://ideone.com/ru28Ud
Last edited on
Ok, I understand why that needs to be 100. And the fillArray function wasn't really how it was supposed to be, I just had something thrown in there until I started working with it. A new problem that I have now: I have written a function that takes any two rows in an array, adds them together, and stores the result in the second row.

The code:
1
2
3
4
5
6
7
8
9
10
11
12
13
//Function to add any two rows and store the result in the second row.
void addRows(int ary[][100], int &ROWS, int &COLS)
{
    int r1, r2;
    cout << "Enter the number corresponding to the rows you wish to add: " << endl;
    cout << "First Row: ";
    cin >> r1;
    cout << "Second Row: ";
    cin >> r2;
    
    for(int c=0; c < COLS; c++)
        ary[r2][c] = ary[r1][c] + ary[r2][c];
}


The original array:

 1   2   3   4   5   6   7   8   9  10  11  12 
 13  14  15  16  17  18  19  20  21  22  23  24 
 25  26  27  28  29  30  31  32  33  34  35  36 
 37  38  39  40  41  42  43  44  45  46  47  48 
 49  50  51  52  53  54  55  56  57  58  59  60 
 61  62  63  64  65  66  67  68  69  70  71  72 
 73  74  75  76  77  78  79  80  81  82  83  84 
 85  86  87  88  89  90  91  92  93  94  95  96 
 97  98  99 100 101 102 103 104 105 106 107 108 
109 110 111 112 113 114 115 116 117 118 119 120 



The output, when I attempt to add rows 2&4 and store the result in row 4:

 1   2   3   4   5   6   7   8   9  10  11  12 
 13  14  15  16  17  18  19  20  21  22  23  24 
 25  26  27  28  29  30  31  32  33  34  35  36 
 37  38  39  40  41  42  43  44  45  46  47  48 
 74  76  78  80  82  84 
 86  88  90  92  94  96 
 61  62  63  64  65  66  67  68  69  70  71  72 
 73  74  75  76  77  78  79  80  81  82  83  84 
 85  86  87  88  89  90  91  92  93  94  95  96 
 97  98  99 100 101 102 103 104 105 106 107 108 
109 110 111 112 113 114 115 116 117 118 119 120


WHY?
Guys, disregard ^. Stupid mistake on my part.
Topic archived. No new replies allowed.