EDIT: What if I'm trying to do it without global or static and without vector o dynamic vectors?
I'm trying to create two vectors and have a function that pass by address a new vector that is the quotient of each element of the two arrays. i.e. V1 is 1 1 2 2 3 4 V2 is 2 2 1 1 2 2 the result expected is 2 2 2 2 6 8
My problem is when I send the "r" result from the "quoziente" function because I receive random numbers. I think that the problem is that the function only exists during its execution but when it stops running it dies with his variables too. How should I do? I'm sure that I'm passing the right address to "ris". I tried even to print out the elements of the operation and I'm sure that I'm doing the right operation. Any help is really appreciated! Thanks
Here's the code:
1 #include <iostream>
2 using namespace std;
3
4
5
6 void readarray (int* v, int dim) {
7 for(int i=0; i<dim; i++) {
8 cin >> v[i];
9 }
10 }
11
12 void printarray(int* v, int dim) {
13 for(int i=0; i<dim; i++) {
14 cout << v[i] << " ";
15 }
16 cout << endl;
17 }
18
19 int main() {
20 int v1[7];
21 int v2[7];
22 int *ris;
23
24 cout << "V1";
25 readarray(v1,7);
26 cout << "V2";
27 readarray(v2,7);
28 ris = quoziente(v1,v2,7);
29 cout << "V1";
30 printarray(v1,7);
31 cout << "V2";
32 printarray(v2,7);
33 cout << "ris ";
34 printarray(ris,7);
35
36 return 0;
37 }
38
39 int* quoziente (int* v1, int* v2, int dim) {
40 int r[7];
41
42 for(int i=0; i<dim; i++) {
43 r[i] = v1[i] * v2[i];
44 cout << r[i] << " ";
45 }
46 cout << endl;
47 return r;
48 }