passing to 2D Array into function to initialize the content

I am new into C++ and need help please.i am in need in generating and initializing 2D array using function. basically my code is to pass on array and some integers into function then the function supposed to return back genarted table with some values based on the passed parameters.
every time i run the program, it gave an error about the pointer i am using

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

int main()
{
int RPM_Upper_Limit, RPM_Step_Up__Duration, RPM_Steps_Number, Table_Index;
cout << "Enter the RPM limit ";
cin>> RPM_Upper_Limit;
cout << "Enter the RPM step up duration ";
cin>> RPM_Step_Up__Duration;
cout << "Enter the RPM steps number ";
cin>> RPM_Steps_Number;

cout << "Enter Table_Index ";
cin>> Table_Index;

int RPM_Table [2][2];

Stair_Case_Generated_Table(RPM_Table, RPM_Upper_Limit, RPM_Step_Up__Duration, RPM_Steps_Number, Table_Index);
}

// my function that generate and initialize the table//
// it will basically add certain value to each field//

int Stair_Case_Generated_Table(RPM_Table, RPM_Upper_Limit, RPM_Step_Up__Duration, RPM_Steps_Number, Table_Index)
{
int Table [RPM_Steps_Number][];

int RPM_Value_Per_Step = RPM_Upper_Limit/RPM_Steps_Number, j;
if (Table_Index =1)
{

 RPM_Value_Per_Step *=1;
}

if (Table_Index =2)
{
    RPM_Value_Per_Step *=2;
}
 if (Table_Index =3)
{
    RPM_Value_Per_Step *=3;
}
 if (Table_Index =4)
{
    RPM_Value_Per_Step *=4;
}


for(int i =0; i<RPM_Steps_Number;i++)
{
    for(j=0;j<2;j++)
    {
        Table[i][j] = Step_Time;
        j++;
        Table[i][j] = RPM_Value_Per_Step;
        Step_Time+=0;
        RPM_Value_Per_Step +=RPM_Value_Per_Step;
        Step_Time+=1000;
    }

}

return Table;
}
At line 23: Within the header of a function definition each argument needs a type specifier:
1
2
3
4
5
6
7
8
int Stair_Case_Generated_Table(
     int ** RPM_Table
   , int RPM_Upper_Limit
   , int RPM_Step_Up__Duration
   , int RPM_Steps_Number
   , int Table_Index
) {
 ...


Also, a function must be declared BEFORE it could used. Move your definition of this function before main(), or add a prototype declaration before main().

At line 25: At an array each field size must be specified:
int Table [RPM_Steps_Number][2];

At line 62: All arrays will return as pointers, so you cannot return your Table array, because it will deleted if your function returns. Instead you have to define your array at main() or global and pass it as an argument to the function. Beware, arrays will be passed as pointers to a function.
Last edited on
Thanks. I did what you suggested and still getting the below error
"
C:\SW Training\TestExample\test\main.cpp|66|error: cannot convert 'int (*)[2]' to 'int**' for argument '1' to 'int Stair_Case_Generated_Table(int**, int, int, int, int)'

"



Here is what i did :


#include <cstdlib>
#include <iostream>
#include <ctime>
#include<iomanip>
using namespace std;

//void Stair_Case_Generated_Table(int RPM_Table[2][2])
int Stair_Case_Generated_Table(int **RPM_Table, int RPM_Upper_Limit, int RPM_Step_Up__Duration, int RPM_Steps_Number, int Table_Index)
{
int Table [RPM_Steps_Number][2];

int RPM_Value_Per_Step = RPM_Upper_Limit/RPM_Steps_Number, j;
int Step_Time = 1000;
if (Table_Index =1)
{

RPM_Value_Per_Step *=1;
}

if (Table_Index =2)
{
RPM_Value_Per_Step *=2;
}
if (Table_Index =3)
{
RPM_Value_Per_Step *=3;
}
if (Table_Index =4)
{
RPM_Value_Per_Step *=4;
}


for(int i =0; i<RPM_Steps_Number;i++)
{
for(j=0;j<2;j++)
{
Table[i][j] = Step_Time;
j++;
Table[i][j] = RPM_Value_Per_Step;
Step_Time+=0;
RPM_Value_Per_Step +=RPM_Value_Per_Step;
Step_Time+=1000;
}

}

}


int main()
{
int RPM_Upper_Limit, RPM_Step_Up__Duration, RPM_Steps_Number, Table_Index;
cout << "Enter the RPM limit ";
cin>> RPM_Upper_Limit;
cout << "Enter the RPM step up duration ";
cin>> RPM_Step_Up__Duration;
cout << "Enter the RPM steps number ";
cin>> RPM_Steps_Number;

cout << "Enter Table_Index ";
cin>> Table_Index;

int RPM_Table [RPM_Steps_Number][2];

Stair_Case_Generated_Table(RPM_Table, RPM_Upper_Limit, RPM_Step_Up__Duration, RPM_Steps_Number, Table_Index);
}



I managed to get the code fixed. but can i ask how can i return the array from the function and use it in the main function please. below is the code.

#include <cstdlib>
#include <iostream>
#include <ctime>
#include<iomanip>
using namespace std;

int Stair_Case_Generated_Table(int ** RPM_Table, int RPM_Upper_Limit, int RPM_Step_Up__Duration, int RPM_Steps_Number, int Table_Index)
{
int Table [RPM_Steps_Number][2];// working ???????????????


//RPM_Table [RPM_Steps_Number][2];

int RPM_Value_Per_Step = RPM_Upper_Limit/RPM_Steps_Number, j;
int Step_Time = 1000;

if(Table_Index !=0)
{

for(int i =0; i<RPM_Steps_Number;i++)
{
for(j=0;j<2;j++)
{
Table[i][j] = Step_Time;
j++;
// Table[i][j] = RPM_Value_Per_Step;
Table[i][j] = RPM_Value_Per_Step * Table_Index; // added
Step_Time+=0;
RPM_Value_Per_Step +=RPM_Value_Per_Step;
Step_Time+=1000;
}

}

for(int i =0; i<RPM_Steps_Number;i++)
{
for(j=0;j<2;j++)
{
cout<< Table[i][j] << " ";
}
cout<< endl;
}
}
else
{
cout<< " Hello There!!!!!";
}
}

int main()
{
int RPM_Upper_Limit, RPM_Step_Up__Duration, RPM_Steps_Number = 0, Table_Index;

cout << "Enter the RPM limit ";
cin>> RPM_Upper_Limit;

cout << "Enter the RPM step up duration ";
cin>> RPM_Step_Up__Duration;

cout << "Enter the RPM steps number ";
cin>> RPM_Steps_Number;

cout << "Enter Table_Index greater than 0 ";
cin>> Table_Index;

int **RPM_Table;



Stair_Case_Generated_Table(RPM_Table, RPM_Upper_Limit, RPM_Step_Up__Duration, RPM_Steps_Number, Table_Index);

cout<< " You reach this point";
//
int j;

// Can you tell me how can I use the generated table in the Stair_Case_Generated_Table function in my main function

for(int i =0; i<RPM_Steps_Number;i++)
{
for(j=0;j<2;j++)
{
cout<< RPM_Table[i][j] << " ";
}
cout<< endl;
}
}
You can't do that in the line with your question marks. Because you can't return the values of an array. It will always returned as pointer to the values. But the values are lying in your function and will get invalid when you returned from this function.
Therefore you must create your array outside of your function, this means: in main() or at global scope.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void fnc( int[][] array)
{
   // do something with your array

  // You don't need return the array, because it' will be passed as pointer
  // and retains all changes
}

int main()
{
   int arr[2][3];  // create your array in main

   fnc( arr );   // call your separate function

  // your array holds the changed values
}


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