In: Computer Science
In the following code down below I am not getting my MatrixElementMult right. Could someone take a look at it and help fix it? Also, when I print out the matrices I don't want the decimals. I know it's a format thing but I'm new to C and not getting it. Thanks!
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#define N 8
typedef struct _Matrix {
double element[N][N];
} Matrix;
void PrintMatrix(Matrix a){
int i,j;
for(i=0;i<N;i++){
for(j=0;j<N;j++){
printf(" %.1f ",a.element[i][j]);
}
printf("\n");
}
printf("\n\n");
}
float ComputeAverage(Matrix a){
int sum = 0, i,j , total = N*N;
for(i=0;i<N;i++){
for(j=0;j<N;j++){
sum += a.element[i][j];
}
}
return sum/total;
}
Matrix Add(Matrix a, int dc){
int i,j;
for(i=0;i<N;i++){
for(j=0;j<N;j++){
a.element[i][j] += dc;
}
}
return a ;
}
Matrix MatrixTranspose(Matrix a){
int i,j;
Matrix M = {{154, 123, 123, 123, 123, 123, 123, 136,
192, 180, 136, 154, 154, 154, 136, 110,
254, 198, 154, 154, 180, 154, 123, 123,
239, 180, 136, 180, 180, 166, 123, 123,
180, 154, 136, 167, 166, 149, 136, 136,
128, 136, 123, 136, 154, 180, 198, 154,
123, 105, 110, 149, 136, 136, 180, 166,
110, 136, 123, 123, 123, 136, 154, 136}};
for(i=0;i<N;i++){
for(j=0;j<N;j++){
M.element[i][j]=a.element[j][i];
}
}
return M;
}
Matrix MatrixElementDiv(Matrix a, Matrix b){
int i,j;
for(i=0;i<N;i++){
for(j=0;j<N;j++){
a.element[i][j] = a.element[i][j]/b.element[i][j];
}
}
return a;
}
Matrix MatrixElementMult(Matrix a, Matrix b){
int i,j;
for(i=0;i<N;i++){
for(j=0;j<N;j++){
a.element[i][j] = a.element[i][j]*b.element[i][j];
}
}
return b;
}
Matrix Q50 = {{16, 11, 10, 16, 24, 40, 51, 61,
12, 12, 14, 19, 26, 58, 60, 55,
14, 13, 16, 24, 40, 57, 69, 56,
14, 17, 22, 29, 51, 87, 80, 62,
18, 22, 37, 56, 68,109,103, 77,
24, 35, 55, 64, 81,104,113, 92,
49, 64, 78, 87,103,121,120,101,
72, 92, 95, 98,112,100,103, 99}
};
int main(int argc, const char * argv[])
{
Matrix M = {{154, 123, 123, 123, 123, 123, 123, 136,
192, 180, 136, 154, 154, 154, 136, 110,
254, 198, 154, 154, 180, 154, 123, 123,
239, 180, 136, 180, 180, 166, 123, 123,
180, 154, 136, 167, 166, 149, 136, 136,
128, 136, 123, 136, 154, 180, 198, 154,
123, 105, 110, 149, 136, 136, 180, 166,
110, 136, 123, 123, 123, 136, 154, 136}};
// need to implement PrintMatrix
PrintMatrix(M);
// need to implement ComputeAverage
float ave = ComputeAverage(M);
// need to implement round
int dc = round(ave);
printf("Ave = %d\n",dc);
// need to implement Add
Matrix M2 = Add(M, -dc);
// need to implement PrintMatrix
PrintMatrix(M2);
// need to implement MatrixTranspose
Matrix T2 = MatrixTranspose(M);
PrintMatrix(T2);
// need to implement MatrixElementMult
Matrix R = MatrixElementMult(Q50, M);
PrintMatrix(R);
// need to implement MatrixElementDiv
Matrix C = MatrixElementDiv(R, Q50);
PrintMatrix(C);
return EXIT_SUCCESS;
}
Program:
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#define N 8
typedef struct _Matrix {
double element[N][N];
} Matrix;
void PrintMatrix(Matrix a){
int i,j;
for(i=0;i<N;i++){
for(j=0;j<N;j++){
printf(" %d ", (int)a.element[i][j]);
}
printf("\n");
}
printf("\n\n");
}
float ComputeAverage(Matrix a){
int sum = 0, i,j , total = N*N;
for(i=0;i<N;i++){
for(j=0;j<N;j++){
sum += a.element[i][j];
}
}
return sum/total;
}
Matrix Add(Matrix a, int dc){
int i,j;
for(i=0;i<N;i++){
for(j=0;j<N;j++){
a.element[i][j] += dc;
}
}
return a ;
}
Matrix MatrixTranspose(Matrix a){
int i,j;
Matrix M = {{154, 123, 123, 123, 123, 123, 123, 136,
192, 180, 136, 154, 154, 154, 136, 110,
254, 198, 154, 154, 180, 154, 123, 123,
239, 180, 136, 180, 180, 166, 123, 123,
180, 154, 136, 167, 166, 149, 136, 136,
128, 136, 123, 136, 154, 180, 198, 154,
123, 105, 110, 149, 136, 136, 180, 166,
110, 136, 123, 123, 123, 136, 154, 136}};
for(i=0;i<N;i++){
for(j=0;j<N;j++){
M.element[i][j]=a.element[j][i];
}
}
return M;
}
Matrix MatrixElementDiv(Matrix a, Matrix b){
int i,j;
for(i=0;i<N;i++){
for(j=0;j<N;j++){
a.element[i][j] = a.element[i][j]/b.element[i][j];
}
}
return a;
}
Matrix MatrixElementMult(Matrix a, Matrix b){
int i,j,k,s;
Matrix c;
for(i=0;i<N;i++)
{
for(k=0;k<N; k++)
{
s=0;
for(j=0;j<N;j++){
s=s + a.element[i][j]*b.element[j][k];
}
c.element[i][k]=s;
}
}
a = c;
return b;
}
Matrix Q50 = {{16, 11, 10, 16, 24, 40, 51, 61,
12, 12, 14, 19, 26, 58, 60, 55,
14, 13, 16, 24, 40, 57, 69, 56,
14, 17, 22, 29, 51, 87, 80, 62,
18, 22, 37, 56, 68,109,103, 77,
24, 35, 55, 64, 81,104,113, 92,
49, 64, 78, 87,103,121,120,101,
72, 92, 95, 98,112,100,103, 99}
};
int main(int argc, const char * argv[])
{
Matrix M = {{154, 123, 123, 123, 123, 123, 123, 136,
192, 180, 136, 154, 154, 154, 136, 110,
254, 198, 154, 154, 180, 154, 123, 123,
239, 180, 136, 180, 180, 166, 123, 123,
180, 154, 136, 167, 166, 149, 136, 136,
128, 136, 123, 136, 154, 180, 198, 154,
123, 105, 110, 149, 136, 136, 180, 166,
110, 136, 123, 123, 123, 136, 154, 136}};
// need to implement PrintMatrix
PrintMatrix(M);
// need to implement ComputeAverage
float ave = ComputeAverage(M);
// need to implement round
int dc = round(ave);
printf("Ave = %d\n",dc);
// need to implement Add
Matrix M2 = Add(M, -dc);
// need to implement PrintMatrix
PrintMatrix(M2);
// need to implement MatrixTranspose
Matrix T2 = MatrixTranspose(M);
PrintMatrix(T2);
// need to implement MatrixElementMult
Matrix R = MatrixElementMult(Q50, M);
PrintMatrix(R);
// need to implement MatrixElementDiv
Matrix C = MatrixElementDiv(R, Q50);
PrintMatrix(C);
return EXIT_SUCCESS;
}
Output:
154 123 123 123 123 123 123 136
192 180 136 154 154 154 136 110
254 198 154 154 180 154 123 123
239 180 136 180 180 166 123 123
180 154 136 167 166 149 136 136
128 136 123 136 154 180 198 154
123 105 110 149 136 136 180 166
110 136 123 123 123 136 154 136
Ave = 148
6 -25 -25 -25 -25 -25 -25 -12
44 32 -12 6 6 6 -12 -38
106 50 6 6 32 6 -25 -25
91 32 -12 32 32 18 -25 -25
32 6 -12 19 18 1 -12 -12
-20 -12 -25 -12 6 32 50 6
-25 -43 -38 1 -12 -12 32 18
-38 -12 -25 -25 -25 -12 6 -12
154 192 254 239 180 128 123 110
123 180 198 180 154 136 105 136
123 136 154 136 136 123 110 123
123 154 154 180 167 136 149 123
123 154 180 180 166 154 136 123
123 154 154 166 149 180 136 136
123 136 123 123 136 198 180 154
136 110 123 123 136 154 166 136
154 123 123 123 123 123 123 136
192 180 136 154 154 154 136 110
254 198 154 154 180 154 123 123
239 180 136 180 180 166 123 123
180 154 136 167 166 149 136 136
128 136 123 136 154 180 198 154
123 105 110 149 136 136 180 166
110 136 123 123 123 136 154 136
9 11 12 7 5 3 2 2
16 15 9 8 5 2 2 2
18 15 9 6 4 2 1 2
17 10 6 6 3 1 1 1
10 7 3 2 2 1 1 1
5 3 2 2 1 1 1 1
2 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
Process returned 0 (0x0) execution time : 0.738 s
Press any key to continue.
N.B. I have modified the code of MatrixElementMult and also modified the output. Whether the output is mismatch then please provide me the sample output.