Suma de matrices

Podrian ayudarme a encontrer que está mal en el código por favor, al momento de copilar no salen errores pero cuando lo ejecuto no sale nada en la funcion que suma las matrices.


#include<iostream>
#include<cmath>
#include<vector>
#include<stdlib.h>
#include<time.h>

void imprima_vector (std::vector <double> );
void imprima_matriz (std::vector<std::vector<double> > );
std::vector<std::vector<double> > suma_matrices (std::vector<std::vector<double> > ,std::vector<std::vector<double> > );
void llenar_aleatorio(std::vector<std::vector<double> >& , int ,int );

int main()
{
using namespace std;
int n=4, m=3;
vector<vector<double> > A(n),B(n) ;

A.resize(n);

for( int i=0; i<A.size () ;i++)
A[i].resize(m);

llenar_aleatorio(A,-9,0);
imprima_matriz(A);
cout<<endl;
B.resize(n);

for( int i=0; i<B.size () ;i++)
B[i].resize(m);
llenar_aleatorio(B,1,9);
imprima_matriz(B);

imprima_matriz(suma_matrices(A,B));




}

void imprima_vector (std::vector <double> v)
{
using namespace std;
cout<<'(';
for (int i=0; i<v.size()-1;i++)
cout<<v[i]<<", ";
cout<<v[v.size()-1]<<')'<<endl;
}

void imprima_matriz (std::vector<std::vector<double> > A)
{
using namespace std;
for(int i=0; i<A.size();i++)
for(int j=0;j<A[i].size();j++)
A[i][j];

for (int i=0; i<A.size();i++)
imprima_vector (A[i]);
}

void llenar_aleatorio(std::vector<std::vector<double> >& Y, int p,int q)
{
srand(time(NULL));
for(int i=0; i<Y.size();i++)
for(int j=0;j<Y[i].size();j++)
Y[i][j]=rand()%(q-p-1)+p;
}

std::vector<std::vector<double> > suma_matrices (std::vector<std::vector<double> > A,std::vector<std::vector<double> > B)
{
using namespace std;
std::vector<std::vector<double> > S(A.size());
for(int i=0; i<A.size();i++)
for(int j=0; j<A.size();j++)
S[i][j]=A[i][j]+B[i][j];

return S;
}
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>
#include <iomanip>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <cmath>

using Vector = std::vector<double>;
using Matrix = std::vector<Vector>;

void imprima_vector( const Vector& );
void imprima_matriz( const Matrix& );
Matrix suma_matrices( const Matrix& A, const Matrix& B );
void llenar_aleatorio( Matrix&, int, int );

int main()
{
    srand( time( 0 ) );

    int n = 4, m = 3;
    Matrix A( n, Vector( m ) ), B( n, Vector( m ) );

    llenar_aleatorio( A, -9, 0 );
    imprima_matriz( A );

    llenar_aleatorio( B, 1, 9 );
    imprima_matriz( B );

    imprima_matriz( suma_matrices( A, B ) );
}

void imprima_vector( const Vector& v )
{
    std::cout << '(';
    for( size_t i = 0; i < v.size() - 1; i++ )
        std::cout << std::setw(3) << v[i] << ", ";
    std::cout << std::setw(3) << v[v.size()-1] << ")\n";
}

void imprima_matriz( const Matrix& A )
{
    for( size_t i = 0; i < A.size(); i++ )
        imprima_vector( A[i] );
    std::cout << '\n';
}

void llenar_aleatorio( Matrix& Y, int p, int q )
{
    for( size_t i = 0; i < Y.size(); i++ )
        for( size_t j = 0; j < Y[i].size(); j++ )
            Y[i][j] = rand() % (q - p - 1) + p;
}

Matrix suma_matrices( const Matrix& A, const Matrix& B )
{
    Matrix S( A.size(), Vector( A[0].size() ) );
    for( size_t i = 0; i < A.size(); i++ )
        for( size_t j = 0; j < A[i].size(); j++ )
            S[i][j] = A[i][j] + B[i][j];
    return S;
}

Topic archived. No new replies allowed.