int to int

Hey guys. I'm having a problem compiling my code. I'll post my code now.

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
#include <iostream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

using namespace std;

const int COLS = 20, rows = 20;
int board[rows][COLS];
void fillBoard(int board[rows][COLS]);
void showBoard(int board[rows][COLS]);
void findPeaks(int board[rows][COLS]);

int main()
{

    fillBoard(board[rows][COLS]);
    showBoard(board[rows][COLS]);
    findPeaks(board[rows][COLS]);

    return 0;
}

void fillBoard(int board[rows][COLS])
{
    srand(time(NULL));

    for(int i = 0; i < COLS; i++) {
        for (int k = 0; k < rows; k++){
            board[k][i] = (rand() % 50 + 1);
        }
        }

}

void showBoard(board[rows][COLS])
{

    for(int m = 0; m <COLS; m++){
        for(int n = 0; n < rows; n++)
            cout << board[n][m] << " ";
    }

}

void findPeaks(board[rows][COLS])
{



}


When I call my functions, the build log keeps telling me "error: invalid conversion from 'int' to 'int (*)(20)' [-fmpermissive]" and I don't know how to solve this problem. Any help would be appreciated. Thanks.
Last edited on
its not int to int, its int to int* * means "pointer". Pointers and arrays are quasi interchangeable. So somewhere you have something akin to this:

int a[10];
int x;
x = a; //cannot convert int* to int

so in main
fillBoard(board[rows][COLS]); //this is an integer. board[rows][cols] is a specific location in your 2d array, its the location at rows, cols. Its also out of range, but that is because you did not understand.

try fillBoard(board); //board is a 2d array, the function takes a 2d array, so it matches. board[x] is a 1d array. board[x][y] is an integer.

you may need to add at least a ; to findPeaks just to make it compile depending on your compiler and settings.

you left off 'int' on the later functions parameters, which won't compile and will likely give an even more cryptic error message.
Last edited on
int a[10];
int x;
x = a; //cannot convert int* to int
The only thing I could guess it would be would be the inclusion of rows and COLS in the array size but I can't do any work on the array if I don't pass the size to it as well. I tried removing the rows and COLS from both the prototype's and calling and it still says the same thing. The problem was with rows. Thanks for your help.
Last edited on
I added some more above. You are welcome.
Ok. Thanks. I've got one more error message that I need to get rid of until I can compile. Would you be able to help?
it compiled and ran for me after I fixed everything in my edited post above.
but what is it?
did you get the missing ints?

watch the messages too. it takes a while to understand c++ compiler messages; many of them are relics from the 80s and somewhat odd. Once you get it, its a huge benefit to see what its saying and fix it immediately instead of pondering what the heck it is going on about. Its one of the big ah-ha moments of c++. It almost pays to screw up code just to see what it will say about it for a while.
Last edited on
I've corrected the findPeaks and added all the ints in the parameters. Now it says that the storage size of board isn't known.

I'll post my updated code

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
#include <iostream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

using namespace std;

const int COLS = 20, rows = 20;
int       board[][COLS];
void      fillBoard(int board);
void      showBoard(int board);
void      findPeaks(int board);

int main()
{
    fillBoard(board);
    showBoard(board);
    findPeaks(board);

    return 0;
}

void fillBoard(int board[][COLS])
{
    int k, i;

    srand(time(NULL));

    for(i = 0; i < COLS; i++) {
            board[k][i] = (rand() % 50 + 1);
        for (k = 0; k < rows; k++){
            board[k][i] = (rand() % 50 + 1);
        }
        }

}

void showBoard(int board[][COLS])
{

    for(int m = 0; m <COLS; m++){
        for(int n = 0; n < rows; n++)
            cout << board[n][m] << " ";
    }

}
Last edited on
Fixed:
arrays have to have a size at compile time. [] won't do.

it seems you are not really understanding arrays as parameters quite yet. do my changes make sense?


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
#include <iostream>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <ctime>

using namespace std;

const int COLS = 20, rows = 20;
int       board[rows][COLS];
void      fillBoard(int board[][COLS]);
void      showBoard(int board[][COLS]);
void      findPeaks(int board[][COLS]);

int main()
{
    fillBoard(board);
    showBoard(board);
  //  findPeaks(board);

    return 0;
}

void fillBoard(int board[][COLS])
{
    int k, i;

    srand(time(NULL));

    for(i = 0; i < COLS; i++) {
            board[k][i] = (rand() % 50 + 1);
        for (k = 0; k < rows; k++){
            board[k][i] = (rand() % 50 + 1);
        }
        }

}

void showBoard(int board[][COLS])
{

    for(int m = 0; m <COLS; m++){
        for(int n = 0; n < rows; n++)
            cout << board[n][m] << " ";
    }

}
Last edited on
Topic archived. No new replies allowed.