Retunr array[]

Hello!
I want to make third function operating with elements of m1[5].
Please, how do I achieve that array m1[5] can be memorised and translated to another function?

)f.e. , third function could multiply each elemnt of m1[5] by 3 and then add them all and count the sum, but it is just example).


The point is just how the array can be memorised and translated to toher functions.
Please, if someone can help!!!
Many thanks!!!

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<stdlib.h>
using namespace std;

void Array1d(int z1[][3]){
srand (time(NULL));
int m1[5];
int a=0;
for (int i=0;i<5;i++){
     for (int j=0;j<3;j++){
              z1[i][j]=rand()% 10;
            //  cout<<d1[i][j]<<" ";
              a+=z1[i][j];
     }
//cout<<endl<<endl;;
m1[i]=a;
a=0;
}
for(int d=0;d<5;d++){
cout<<m1[d]<<" ";
}


}

void Generate(int d1[][3]){
  srand (time(NULL));

for (int i=0;i<5;i++){
     for (int j=0;j<3;j++){
              d1[i][j]=rand()% 10;
              cout<<d1[i][j]<<" ";
              
     }
cout<<endl;
}


}


int main(){


int m[5][3];

Generate(m);
cout<<endl;
Array1d(m);

return 0;
}
See "Arrays as parameters" in http://www.cplusplus.com/doc/tutorial/arrays/

In other words: your main reserves 'm' and calls Generate. It should similarly reserve an 'int [5]' and pass it to Array1d as second parameter.

The other option is dynamic memory allocation. Standard library offers containers, like std::vector and std::array. Containers are objects. Functions can return objects.
Last edited on
Hello!
I tried m1[5], produced in function Array1d, which is not MAIN, send to function MemoArr, which would just cout it.

Where is the mistake, please?
Many thanks!

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<stdlib.h>
using namespace std;

void MemoArr(int ar1[]){
for (int i=0;i<5;i++){
cout<<ar1[i]<<endl;
}
}


void Array1d(int z1[][3]){
srand (time(NULL));
int m1[5];
int a=0;
for (int i=0;i<5;i++){
     for (int j=0;j<3;j++){
              z1[i][j]=rand()% 10;
               a+=z1[i][j];

              cout<<z1[i][j]<<" ";
    }
m1[i]=a;
cout<<"     "<<a;
cout<<endl;
a=0;
}

cout<<endl;
for(int d=0;d<5;d++){
cout<<m1[d]<<" ";
}

cout<<endl<<endl<<endl;
}




int main(){

int ar[5];
int m[5][3];


cout<<"This is Array1d: "<<endl<<endl;
Array1d(m);

cout<<"This is MemoArr! "<<endl<<endl;
MemoArr(ar);
return 0;
}



P.s. I am getting strange numbers, but not the ones that are be in the m1[5] array.???
Last edited on
Topic archived. No new replies allowed.