return array

I have 2 int arrays. And i need to compare them. And i want to return an array, so i can use it again, but i dont know how to return it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int vordlemine(int a[][4], int b[][4]){
    int sum1 = 0;
    int sum2 = 0;
    for(int i = 0; i <4; i++){
            for(int j=0; j<4; j++){
                   sum1 = sum1 + a[i][j];
                   sum2 = sum2 + b[i][j];
                   }
            }
    if(sum1<sum2){
         return b;
                  }
    else{
         return a;
         }
    }


and the line where i use it valjastamine(vordlemine(vordlemine(matrix1, matrix2), matrix3));
valjastamine just prints out the array, whose sum is the biggest.
Your program is too complicated. From what I understand you just want to find out the max sum from matrix1, matrix2 and matrix3.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int vordlemine(int a[][4], int b[][4],int c[][4]){
    int sum1 = 0;
    int sum2 = 0;
    int sum3=0;
    for(int i = 0; i <4; i++){
            for(int j=0; j<4; j++){
                   sum1 = sum1 + a[i][j];
                   sum2 = sum2 + b[i][j];
                   sum3+=c[i][j];
                   }
            }
    if(sum1>sum2 && sum1>sum3){
         return 1;//returns 1 if matrix1 has the biggest sum
                  }
   if(sum2>sum1 && sum2>sum3){
         return 2;//returns 2 if matrix2 has the biggest sum
         }
  else return 3;//returns 3 if matrix3 has the biggest sum
    }

So now you know what matrix has the biggest sum. You don't need to return the whole array you just need to know which 1 is. Feel free to post your questions. Also telling me what you want to do would be easier!
First you don't need to return array from your function because arrays are passed by reference to the function.
So what ever you do with your array this is done direcly, no copy of array is made.
You can't change this behaviour with plain arrays.


*BUT* C++ 11 gives us powerfull sintax!
for example you can solve this like that:

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
auto vordlemine(int a[][4], int b[][4]) -> decltype (a)
{
    int sum1 = 0;
    int sum2 = 0;
    for(int i = 0; i <4; i++){
		for(int j=0; j<4; j++){
			sum1 = sum1 + a[i][j];
            sum2 = sum2 + b[i][j];
            }
        }
    if(sum1 < sum2){
          return a;
   return b;
   }
}


// make a test like that
int main ()
{
	int x[2][4] = { {0,1,2,3}, {4,5,6,7} };
	int y[2][4] = { {8,9,10,11} , {12,13,14,15} };

	auto z = vordlemine(x,y);

	for (short i = 0; i < 2; ++i)
		for (short j = 0; j < 4; ++j)
			cout << z[i][j] << endl;

	return 0;
}


Kind regards
Last edited on
Well, this is somewhat of an assignment given by instructors. But, since this is a internet based course, the instructors are rarely on, and the tutorials dont cover everythign needed to complete these assingments. So everyone who is in that course, must look for info and help outside the course. and the condidion was, that vordlemine gets 2 arguments not 3, so i cant take all 3 and compare them.
I have similar problem with one other assingnment too.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int ava_fail(char failinimi[]){
    char sisend_rida[30];
    int matrix[3][3];
    
    ifstream fin(failinimi);
    
    for(int i = 0; i <= 3 && ! fin.eof(); i++){
            fin.getline(sisend_rida, 30);
            for(int j = 0; j <= 3; j++){
                    if(j == 0){
                         matrix[i][j] = atoi(strtok(sisend_rida," ")); 
                         }
                    else{
                         matrix[i][j] = atoi(strtok(NULL," ")); 
                         }
                    }
            }
            return matrix;
    }

This takes the filname as argument, opens the file, reads 3 lines, fills the matrix with the numbers on those 3 lines, and then returns the matrix to main function. It would be easyer, but we have to use functions. This return array, is the only problem i have with these 2 programs. EVerything esle is running fine.
You got the answer to you first assignment, in my post abowe.

Honestly I didn't know it's about assignment so sory, I shouldn't give you complete solution :(

For your second assignment problem you already have the answer :)
Last edited on
Well, problem is, i cant use c++11 cause i dont have the compilator for that. And i dont know if instructors have it. Yes i get your solution, i just need to modify it so it would meet the rquirements.

I have an idea about the segond assignment. You dont have to tell me how to do it, just tell me if it is possible. Instead of sending matrix tothe function, i send pointer to the function. And i return the pointer, and then there is some magic and the matrix is filled.

In course materials, we dont have anything said about stuff like this. And i have been unable to find it on internet also.
Last edited on
Well you could use a global array and copy your max sum matrix in it. For example you want to print from arrays a,b,c the 1 which has the max sum: you compare a with b and copy the 1 you need into a global matrix called z and then compare z wtih c and copy the "max array" into d. And then print d. You don't a complex program for this.
vastrolorde,

Your teacher sucks, tell him to teach you C++11 stuff as this is the part of C++ standard.

many compilers like gcc and VC++ already support a part of C++11.
Visual studio beta 11 and 2010 support it(a part).

Here is your code that works without C++11
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
int* vordlemine(int a[][4], int b[][4])
{
    int sum1 = 0;
    int sum2 = 0;
    for(int i = 0; i <4; i++){
		for(int j=0; j<4; j++){
			sum1 = sum1 + a[i][j];
            sum2 = sum2 + b[i][j];
            }
        }
    if(sum1 < sum2){
          return reinterpret_cast<int*>(a);
   return reinterpret_cast<int*>(b);
   }
}


// make a test like that
int main ()
{
	int x[2][4] = { {0,1,2,3}, {4,5,6,7} };
	int y[2][4] = { {8,9,10,11} , {12,13,14,15} };

	int* z = vordlemine(x,y);

	for (short i = 0; i < 8; ++i)
			cout << z[i] << endl;
	cin.ignore();
	return 0;
}
Last edited on
Ugh.... THat sum comparisond i got it running after i modified your first solution. The second i got running when i declared global array. So thank you for your help.
arrays are passed by default in general, so there is no reason to return an array . Just use a void function to modify arrays.
Well, if you loom my original plan, thne it requires returning an array. But i modified it differently. added about 10 lines and it works like a charm.
If you want that your function returns one of its two parameters, that is a pointer to an array of 4 integers you should declare ( in this case define) it the following way

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int ( * vordlemine( int a[][4], int b[][4] ) )[4]
{
    int sum1 = 0;
    int sum2 = 0;

    for ( int i = 0; i < 4; i++ )
    {
            for ( int j=0; j < 4; j++ )
            {
                   sum1 += a[i][j];
                   sum2 += b[i][j];
            }
    }
            
    return ( ( sum1 < sum2 ) ? b : a );
}
Last edited on
Topic archived. No new replies allowed.