Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F120753097
exercise05-5.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
Sun, Jul 6, 19:15
Size
1 KB
Mime Type
text/x-c
Expires
Tue, Jul 8, 19:15 (1 d, 23 h)
Engine
blob
Format
Raw Data
Handle
27226370
Attached To
R1106 Programming Concept Rouaze
exercise05-5.cpp
View Options
/*
* exercise05-5.cpp
*
/*
* Any headers you need to include should be specified on the next lines
*/
#include <iostream>
#include <cassert>
#include <hd_exercise05-5.hpp>
int main(int argc, char* argv[])
{
int sizeA[2] = {10, 4};
int sizeB[2] = {4, 4};
double** A = allocateMatrix(sizeA[0],sizeA[1]);
double** B = allocateMatrix(sizeB[0],sizeB[1]);
for (int i=0; i<sizeA[0]; i++)
{
for (int j=0; j<sizeA[1]; j++)
{
A[i][j] = j;
}
}
for (int i=0; i<sizeB[0]; i++)
{
for (int j=0; j<sizeB[1]; j++)
{
B[i][j] = i;
}
}
printMat(A,sizeA[0],sizeA[1]);
printMat(B,sizeB[0],sizeB[1]);
double** C = Multiply(A, B, sizeA, sizeB);
printMat(C, sizeA[0], sizeB[1]);
deallocateMatrix(A, sizeA[0],sizeA[1]);
deallocateMatrix(B, sizeB[0],sizeB[1]);
deallocateMatrix(C, sizeB[0],sizeB[1]);
}
double** Multiply(double **matrix1, double **matrix2, int size1[], int size2[])
{
assert(size1[1] == size2[0]);
double** matrix3 = allocateMatrix(size1[0],size2[1]);
double v1[size1[1]];
double v2[size1[1]];
for (int i=0; i<size1[0]; i++)
{
for (int j=0; j<size2[1]; j++)
{
for (int r=0; r<size1[1]; r++)
{
v1[r] = matrix1[i][r];
v2[r] = matrix2[r][j];
}
for (int k=0; k<size1[1]; k++)
{
matrix3[i][j] += v1[k]*v2[k];
}
}
}
return matrix3;
}
void printMat(double** mat, int m, int n)
{
for (int i=0; i<m; i++)
{
for (int j=0; j<n; j++)
{
std::cout << mat[i][j];
if (j<(n-1))
{
std::cout << " ";
}
else
{
std::cout << std::endl;
}
}
if (i == (m-1))
{
std::cout << std::endl;
}
}
}
double ** allocateMatrix(int m, int n)
{
double ** mat = new double *[m];
for (int i=0; i<m; i++)
{
mat[i] = new double[n];
}
return mat;
}
void deallocateMatrix(double** mat, int m, int n)
{
for (int i=0; i<m; i++)
{
delete[] mat[i];
}
delete[] mat;
}
Event Timeline
Log In to Comment