Pass vectors from one function to another function

I have a code to solve linear system equations with Gauss-Seidel method, I defined three functions, one to get values of the coefficient matrix, one to get values of column matrix of independent coefficients, and the last one with the numeric method.

The values of matrices are introduced dynamically using two dimension vectors. But as those vectors are defined in each function, the functions containing the numeric method needs matrices. I do not know how to pass those vectors from functions to the other functions. Here is how my code works:


#include <iostream>
#include <cmath>
#include <vector>
using namespace std;


// Se crea una funcion para ingresar los valores de la matriz de coeficientes

double matrix_coef(int size){
double a;
vector< vector<double> > mat_coef;

for (int i = 0; i < size; i++) {
vector<double> fila;
for (int j = 0; j < size; j++) {
cout<< "Ingrese el valor para la columna "<< j+1<<" correspondiente a la fila "<<i+1<<" \n";
cin>> a;
fila.push_back(a); // Agrega un elemento (columna) a la fila

}
mat_coef.push_back(fila); // Agrega la fila a la matriz principal (vector mat_coef)
}
cout << "La matriz de coeficientes ingresada es: \n\n";

for (int k=0; k<size; k++ ){
cout <<"\n\n |";
for (int l=0; l<size; l++){
cout <<" " <<mat_coef [k][l];
}
cout<<" |";



}
return 0;
}

//Se crea una funcion para ingresar la matriz columna de coeficientes independientes

double indep_coef(int size){

double a;
vector< vector<double> > mat_coef_indep;

for (int i = 0; i < size; i++) {
vector<double> fila;
for (int j = 0; j < 1; j++) {
cout<< "Ingrese el valor para la columna "<< j+1<<" correspondiente a la fila "<<i+1<<" \n";
cin>> a;
fila.push_back(a); // Agrega un elemento (columna) a la fila

}
mat_coef_indep.push_back(fila); // Agrega la fila a la matriz principal (vector mat_coef)

}
cout << "La matriz de coeficientes independientes ingresada es: \n\n";
for (int k=0; k<size; k++ ){
cout <<"\n\n |";
for (int l=0; l<1; l++){
cout <<" " <<mat_coef_indep [k][l];
}
cout<<" |";
}
return 0;
}

//Se crea una funcion para el metodo de Gauss-Seidel
//Para resolver iterativamente sistemas de ecuaciones lineales
//Aqui se ingresan los valores iniciales para empezar a iterar

double gauss_seidel(int size, ......){

//This is where the code develops the numerical method
//That series of dotes are where I think vectors should be for this function


Sorry for Spanish comments, thanks in advance


you would pass them like you would pass any other variable BUT you will need to pass them by reference,as far as I know vectors can not be copied directly so operator= is forbidden on vectors(as far as I know) but that's not the point,you will want to do something like this


1
2
3
4
5
6
7
8

void function(vector<int> &a,vector<int> &b){

    // now you can operate on them vectors

}

Last edited on
> as I know vectors can not be copied directly so operator= is forbidden on vectors
you are wrong


> double matrix_coef(int size)
¿what's the point of your return value?
you may have vector< vector<double> > matrix_coef(int size)
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
vector< vector<double> > set_matrix_coef( int size )        // the matrix
{
   vector< vector<double> > mat_coef;
   // your code to set the matrix coefficients
   // ...
   return mat_coef;
}


vector<double> set_rhs( int_size )     // the rhs of the equation (a single column vector>
{
   vector<double> b;
   // your code to set the RHS
   // ...
   return b;
}


vector<double> gauss_seidel( const vector< vector<double> > &A, const vector<double> &b )
{
   vector<double> x;
   // your code to solve iteratively A x = b by Gauss-Seidel method
   // ...
   return x;
}
> as I know vectors can not be copied directly so operator= is forbidden on vectors
you are wrong


really they can be copied?

Yup seems like they can be copied,I must be mixing it up with something else,I know arrays can't be copied using the assignment operator,something(probably a few things) in the STL are not copyable.
Last edited on
"Pass vectors from one function to another function" is conceptually off the mark. We do pass data between calling function and called function, but that is not the pair of functions that you were thinking of.

1
2
3
4
5
6
int main() {
  constexpr int N {42};
  auto foo = set_matrix_coef( N );
  auto bar = set_rhs( N );
  auto gaz = gauss_seidel( foo, bar );
}

Function main() passes N to function set_matrix_coef() and receives a vector that it stores in object foo.
Function main() passes N to function set_rhs() and receives a vector that it stores in object bar.
Function main() passes foo and bar to function gauss_seidel() and receives a vector that it stores in object gaz.

Caller can pass data to called function by value or by reference.
Caller can receive data from called function as return value or by reference.

A function can access data that is in the same scope as the function. For example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
struct Solver {
  Solver( int size ) : N(size) {}
  void set_matrix_coef();
  void set_rhs();
  vector<double> gauss_seidel();
private:
  const int N;
  vector<double> b;
  vector< vector<double> > mat_coef;
};

int main() {
  Solver magic( 42 );
  magic.set_matrix_coef();
  magic.set_rhs();
  auto gaz = magic.gauss_seidel();
}

The member functions of the struct can access the member variables of the struct.

We don't usually say that we would "pass data from magic.set_rhs() to magic.gauss_seidel()".
No, we might say that both magic.set_rhs() and magic.gauss_seidel() access/modify the attributes of magic.
Topic archived. No new replies allowed.