Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F120598780
exercise01-4.cpp
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Sat, Jul 5, 12:33
Size
1 KB
Mime Type
text/x-c
Expires
Mon, Jul 7, 12:33 (1 d, 23 h)
Engine
blob
Format
Raw Data
Handle
27206416
Attached To
R1106 Programming Concept Rouaze
exercise01-4.cpp
View Options
/*
* 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
Log In to Comment