I encountered a need to define a function, parameters of which take arguments from main() function. Here's the code:
#include <iostream>
using namespace std;
void unravel(char arr[][column], int field[][column], int x, int y) { //revealing adjoined cells
for (int minusrows = -1; minusrows < 2; minusrows++){
for (int minuscolumns = -1; minuscolumns < 2; minuscolumns++){
arr[x + minusrows][y + minuscolumns] = field[x + minusrows][y + minuscolumns] + '0';
}
}
}
int main (){
int row, column;
cin >> row >> column;
char a[row][column];
int field[row][column];
for (int i = 0; i < row; i++){ //filling a with asterisks, field with integers
for (int j = 0; j < column; j++){
a[i][j] = '*';
field[i][j] = i + j;
}
}
int x = 2, y = 3;
unravel(a, field, x, y);
for (int i = 0; i < 5; i++){ //printing out the array a
for (int j = 0; j < 5; j++){
cout << a[i][j] << " ";
}
cout << endl;
}
return 0;
}
And here are the errors that I get:
main.cpp:4:25: error: ‘column’ was not declared in this scope
void unravel(char arr[][column], int field[][column], int x, int y) {
^~~~~~
main.cpp:4:32: error: expected ‘)’ before ‘,’ token
void unravel(char arr[][column], int field[][column], int x, int y) {
^
main.cpp:4:34: error: expected unqualified-id before ‘int’
void unravel(char arr[][column], int field[][column], int x, int y) {
^~~
So the goal is to change the values around a cell in a 2D array a[ ][ ] that initially consists of asterisks (*) to the values of an int 2D array field[ ][ ] of the same indices. For that is responsible the function unravel(). The code worked perfectly when I had integer 5 instead of row and column.
#include <iostream>
using namespace std;
void unravel(char arr[][5], int field[][5], int x, int y) { //revealing adjoined cells
for (int minusrows = -1; minusrows < 2; minusrows++){
for (int minuscolumns = -1; minuscolumns < 2; minuscolumns++){
arr[x + minusrows][y + minuscolumns] = field[x + minusrows][y + minuscolumns] + '0';
}
}
}
int main (){
char a[5][5];
int field[5][5];
for (int i = 0; i < 5; i++){ //filling a with asterisks, field with integers
for (int j = 0; j < 5; j++){
a[i][j] = '*';
field[i][j] = i + j;
}
}
int x = 2, y = 3;
unravel(a, field, x, y);
for (int i = 0; i < 5; i++){ //printing out the array a
for (int j = 0; j < 5; j++){
cout << a[i][j] << " ";
}
cout << endl;
}
return 0;
}
Therefore the question: is it possible to have some data stored from the main() function so that the function unravel() would work as I expect it to? I know that variables are defined within the scope. Anything else I saw about the errors I got didn't have much related to my problem. Sorry if the post seems long. Thanks in advance.