Page MenuHomec4science

exercise01-4.cpp
No OneTemporary

File Metadata

Created
Sat, Jul 5, 12:33

exercise01-4.cpp

/*
* exercise-template.cpp <--- The name of the source file goes here
*
* <--- Description of the program goes here.
*
* Created on: September 02, 2015 <--- Fill in the date here
* Author: Davide Forti <davide.forti@epfl.ch> <--- Fill in your name (and e-mail) here
*/
/*
* Any headers you need to include should be specified on the next lines
*/
#include <iostream>
#include <cmath>
int main(int argc, char* argv[])
{
double A[2][2] = { {1.0, 2.0}, {3.0, 4.0} };
double B[2][2] = { {1.0, 2.0}, {3.0, 4.0} };
double C[2][2];
double D[2][2];
int L = sizeof(A)/sizeof(*A);
double v1[L];
double v2[L];
std::cout << "Length of matrix A = " << L ;
std::cout << " \n";
// Print initial matrices
std::cout << "Matrix A : \n";
for (int i=0; i<2; i++)
{
for (int j=0; j<L; j++)
{
std::cout << A[i][j] << " ";
}
std::cout << " \n";
}
std::cout << "Matrix B : \n";
for (int i=0; i<L; i++)
{
for (int j=0; j<L; j++)
{
std::cout << B[i][j] << " ";
}
std::cout << " \n";
}
// Calculations for matrix C
for (int i=0; i<L; i++)
{
for (int j=0; j<L; j++)
{
C[i][j] = A[i][j] + B[i][j];
}
}
// Calculations for matrix D
for (int i=0; i<L; i++)
{
for (int j=0; j<L; j++)
{
for (int r=0; r<L; r++)
{
v1[r] = A[i][r];
v2[r] = B[r][j];
}
for (int k=0; k<L; k++)
{
D[i][j] += v1[k]*v2[k];
}
}
}
// Print the results of the matrix C
std::cout << "Matrix C : \n";
for (int i=0; i<L; i++)
{
for (int j=0; j<L; j++)
{
std::cout << C[i][j] << " ";
}
std::cout << " \n";
}
// Print the results of the matrix D
std::cout << "Matrix D : \n";
for (int i=0; i<L; i++)
{
for (int j=0; j<L; j++)
{
std::cout << D[i][j] << " ";
}
std::cout << " \n";
}
return 0;
}

Event Timeline