Access a matrix in the function

Asked

Viewed 59 times

0

I’m having to create a function in a separate file from main. This function will receive the values of a matrix, sum all values per line, store the sum in a vector, and then print the respective vector. However, I am not able to print the desired results. The main archive is ready to receive the matrices.

This is the function code.

#include <iostream>
void sum(float A[], float v[], const short N, const short M)
{
    int i,j;

    for (j=0; j<M; j++)
    {

        for ( i=0; i<N; i++)
        {
            v[i] += A[i*M+j];

        }
    }
    for (i=0; i<N; i++)
    {
        std::cout << v[i] << " ";
    }
}

And this is the main file where I type the matrix, print the matrix and get the function.

#include <iostream>
#include "funcoes.hpp"
using namespace std;

int main()
{
    const short n=3,m=4;
    float a[n][m];
    int i,j;
    float v[n],resul;
    for( i=0; i<n; i++)
    {
        for( j=0; j<m; j++)
        {
            cout << "Digite [" << i+1 << "][" << j+1 << "] : ";
            cin >>a[i][j];
        }
        cout << endl;
    }
    for( i=0; i<n; i++)
    {
        for( j=0; j<m; j++)
        {
            cout << a[i][j] << " ";
        }
        cout << endl;
    }
    sum(&a[n][m],&v[n],n,m);

    return 0;
}

It is worth mentioning that I know I could write everything on main. It would be even easier. However, I I need that the function is created outside the main.

  • What’s wrong with you?

  • I don’t know why you don’t declare your function parameter as a two-dimensional array. Your function call should be: sum(a, v, n, m);.

  • When you do &a[n][m] and &v[n] is referring to the address of the specific position of the array which, by the way, are outside the limits of the array.

2 answers

0

There are other ways to solve the problem, and mine was:

functions.

#include <iostream>

template <size_t rows, size_t cols>
void sum(float (&A)[rows][cols])
{
    float v[rows];
    size_t i,j;

    for (j=0; j<cols; j++)
    {

        for ( i=0; i<rows; i++)
        {
            v[i] += A[i][j];

        }
    }
    
    std::cout << std::endl;
    
    for (i=0; i<rows; i++)
    {
        std::cout << v[i] << " ";
    }
}

main.cpp

#include <iostream>
#include "funcoes.hpp"
using namespace std;


int main()
{
    const short int n=2,m=2;

    float a[n][m];
    int i,j;
    float v[n],resul;
    
    for( i=0; i<n; i++)
    {
        for( j=0; j<m; j++)
        {
            cout << "Digite [" << i+1 << "][" << j+1 << "] : ";
            cin >>a[i][j];
        }
        cout << endl;
    }
    for( i=0; i<n; i++)
    {
        for( j=0; j<m; j++)
        {
            cout << a[i][j] << " ";
        }
        cout << endl;
    }
    
    sum(a);

    return 0;
}

Notice that I used the template <size_t rows, size_t cols> where it tries to "deduce" values, someone with more experience can correct me and clarify the use.

And in addition you can exchange for int the size_t, if you like, in the tests I conducted with size_t, the first result value sometimes returned something like "3243e 10" and etc..

And I believe someone can better explain what was done wrong in using that reference you used.

And finally if you have N and M = 3 and with the iteration with the <operator, then when you do the last A[i*M+j], this will be A[2+3+2] = A[7], this should not exist in a 3 3 dimensional array, correct?

0

Your program has some little mistakes.

First: the v vector you are using to add up has not been initialized and therefore will have a random value that was previously in memory. The following initialization at the time of the declaration can resolve this easily (or if you prefer, loop from 0 to n - 1 by zeroing each element):

float v[n] = {0};

Second: The way the function was declared and the parameters of the matrix and vector are passed are accessing values outside of what was stored in memory. You are passing the address of the last element of the vector in the function call and within the function that last element is treated as if it were the first. Set the call to:

   sum(&a[0][0], v, n, m); 

Browser other questions tagged

You are not signed in. Login or sign up in order to post.