Algorithm of sum of square matrices - Divide and win

0

I am having problems to implement an algorithm that returns as a result the sum of two square matrices through the divide and conquer method.

void suma_matriz(int matriz1[MAX][MAX], matriz2[MAX][MAX]) 
{ 

 int i = 0; 
 int j = 0; 
 int aux[MAX][MAX]; 
 if (i < MAX) { 
   aux[j][i] = matriz[i][j]; 
   i++; 
 } 
 else if (j < MAX) { 
   aux[j][i] = matriz[i][j]; 
   j++; 
 } 
}
    
asked by sobrius 16.07.2018 в 22:42
source

1 answer

2

It's rare that you have problems with the Divide and Conquer algorithm (DyV) because you do not even use that algorithm. According to Wikipedia :

Divide and Conquer.

  

In the computer science , the term divide and conquer (DYV) < /> refers to one of the most important design paradigms algorithmic . The method is based on the resolution recursive of a problem dividing it into two or more subproblems of the same or similar type. The process continues until they become simple enough to be resolved directly. In the end, the solutions to each of the subproblems combine to give a solution to the original problem.

Your code does not even make use of recursion, so it can hardly be a DyV. On the other hand, a DyV algorithm for adding matrices does not seem to make sense since it is an addition operation for each component of each coordinate:

C mn = A mn + B mn

Problems.

In view of your code (which DyV does not use) and the little sense that DyV has in this context, I think that you do not really intend to use DyV to add matrices. If so, your code has several problems that you should solve.

You pass the matrices by copy.

Your function suma_matriz is receiving two matrices of MAX × MAX size by copy, this can be a performance problem if the matrices are large, better passes the matrices by reference:

void suma_matriz(const int (&matriz1)[MAX][MAX], const (&matriz2)[MAX][MAX])

Since you are not going to modify the input matrices either, it must be a constant reference.

The size of the matrices is fixed.

Your suma_matriz function can only receive matrices of MAX × MAX size, this is very inflexible, better use a template:

template <std::size_t MAX>
void suma_matriz(const int (&matriz1)[MAX][MAX], const (&matriz2)[MAX][MAX])

In this way, the compiler will deduct the size of the matrices without the need to create a constant MAX .

You do not add anything.

Your suma_matriz function should somehow return the sum between matriz1 and matriz2 , but you do not return anything. Since C ++ does not allow to return matrices, the way to return a sum would be to facilitate a non-constant reference parameter:

template <std::size_t MAX>
void suma_matriz(const int (&matriz1)[MAX][MAX], const (&matriz2)[MAX][MAX],
    (&resultado)[MAX][MAX])

In resultado the sum of matriz1 and matriz2 would be saved.

You do not add anything, part two.

In your code:

if (i < MAX) { 
  aux[j][i] = matriz[i][j]; 
  i++; 
} 
else if (j < MAX) { 
  aux[j][i] = matriz[i][j]; 
  j++; 
} 

You do not even have a sum, because you do not have a loop to go through the fields of the matrix: surely you wanted to do this:

for (int i = 0; i != MAX; ++i)
    for (int j = 0; j != MAX; ++j)
        resultado[i][j] = matriz1[i][j] + matriz2[i][j];

Proposal.

With these corrections your code could look like this:

template <std::size_t MAX>
void suma_matriz(const int (&matriz1)[MAX][MAX], const (&matriz2)[MAX][MAX],
    (&resultado)[MAX][MAX])
{
    for (int i = 0; i != MAX; ++i)
        for (int j = 0; j != MAX; ++j)
            resultado[i][j] = matriz1[i][j] + matriz2[i][j];
}
    
answered by 17.07.2018 в 08:56