1
0
Fork 0
This repository has been archived on 2021-11-03. You can view files and clone it, but cannot push or open issues or pull requests.
uni-m.cds-adv-prog/ex2.5/ndim.cpp

78 lines
1.4 KiB
C++

/*
*
*/
#include "iostream"
#define Y_SIZE 3
double* multiply( double ivec[], int N, double mtx[][Y_SIZE] );
int main() {
int Nx = 4;
double ivec[Nx] = {1, 2, 50, 3.141517181920};
double mtx[Nx][Y_SIZE] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12}};
// print vector
std::cout << "Vector = ";
std::cout << "[ ";
for ( int i = 0; i < Nx; i++){
std::cout << ivec[i] << ", " ;
}
std::cout << "]" << std::endl;
// print matrix
std::cout << "Matrix = ";
std::cout << "[";
for ( int i = 0; i < Nx; i++ ){
std::cout << "[ ";
for ( int j = 0; j < Y_SIZE; j++ ){
std::cout << mtx[i][j] << ", " ;
}
std::cout << "]," << std::endl;
}
std::cout << "]" << std::endl;
double * nvec = multiply( ivec, Nx, mtx );
// print new vector
std::cout << "Result = ";
std::cout << "[ ";
for ( int i = 0; i < Y_SIZE; i++){
std::cout << nvec[i] << ", ";
}
std::cout << "]" << std::endl;
delete nvec;
// print the matrix as it is stored in memory
std::cout << "In-Memory Matrix = ";
std::cout << "[" << std::endl;
for ( int i = 0; i < Nx*Y_SIZE; i++ ){
std::cout << mtx[0][i] << ", ";
}
std::cout << std::endl;
std::cout << "]" << std::endl;
}
double* multiply( double ivec[], int N, double mtx[][Y_SIZE] ){
//allocate the new vector of length N
double * nvec = new double[Y_SIZE];
for ( int j = 0; j < Y_SIZE; j++ ){
for ( int i = 0; i < N; i++ ){
nvec[j] += ivec[i]*mtx[i][j];
}
}
return nvec;
}