Question

In: Computer Science

In the following code down below I am not getting my MatrixElementMult right. Could someone take...

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;
}

Solutions

Expert Solution

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.


Related Solutions

I am getting 7 errors can someone fix and explain what I did wrong. My code...
I am getting 7 errors can someone fix and explain what I did wrong. My code is at the bottom. Welcome to the DeVry Bank Automated Teller Machine Check balance Make withdrawal Make deposit View account information View statement View bank information Exit          The result of choosing #1 will be the following:           Current balance is: $2439.45     The result of choosing #2 will be the following:           How much would you like to withdraw? $200.50      The...
I am having trouble with my assignment and getting compile errors on the following code. The...
I am having trouble with my assignment and getting compile errors on the following code. The instructions are in the initial comments. /* Chapter 5, Exercise 2 -Write a class "Plumbers" that handles emergency plumbing calls. -The company handles natural floods and burst pipes. -If the customer selects a flood, the program must prompt the user to determine the amount of damage for pricing. -Flood charging is based on the numbers of damaged rooms. 1 room costs $300.00, 2 rooms...
I'm getting an error with my code on my EvenDemo class. I am supposed to have...
I'm getting an error with my code on my EvenDemo class. I am supposed to have two classes, Event and Event Demo. Below is my code.  What is a better way for me to write this? //******************************************************** // Event Class code //******************************************************** package java1; import java.util.Scanner; public class Event {    public final static double lowerPricePerGuest = 32.00;    public final static double higherPricePerGuest = 35.00;    public final static int cutOffValue = 50;    public boolean largeEvent;    private String...
Please see if you can correct my code. I am getting an ReferenceError in Windows Powershell...
Please see if you can correct my code. I am getting an ReferenceError in Windows Powershell that says payment is undefined. I am trying to create a main.js file that imports the function from the hr.js file; call the function passing the necessary arguments and log the result to the console. main.js var Dev = require("./hr.js") const { add } = require("./hr.js") var dev_type = 1; var hr = 40; console.log("your weekly payment is " + payment(dev_type, hr)) dev_type =...
I am struggling with c++ and I could use some guidance getting through this project. Below...
I am struggling with c++ and I could use some guidance getting through this project. Below are my directions. The objective of this project is to be able to incorporate structures in a program. The program will have the user enter the information for two movies (details below) and then display a list of recommended movies that have 4 or more stars. 1. You will need to create a structure named MovieInfo that contains the following information about a movie:...
Hi, I am struggling to understand this worksheet my professor gave us for practice. Could someone...
Hi, I am struggling to understand this worksheet my professor gave us for practice. Could someone make any sense of this? The scenario: Sodium is found largely in the extracellular compartment with concentrations between 130-145 mM with intracellular sodium concentrations between 3.5-5mM. This chemical difference gives sodium a large concentration gradient, which when permitted (by opening of a channel or through facilitated transport) will move down its concentration gradient to enter the cell. Sodium also has a favorable electrical gradient;...
I was wondering is someone could tell me why my code isn't compiling - Java ------------------------------------------------------------------------------------------------------------...
I was wondering is someone could tell me why my code isn't compiling - Java ------------------------------------------------------------------------------------------------------------ class Robot{ int serialNumber; boolean flies,autonomous,teleoperated; public void setCapabilities(int serialNumber, boolean flies, boolean autonomous, boolean teleoperated){ this.serialNumber = serialNumber; this.flies = flies; this.autonomous = autonomous; this.teleoperated = teleoperated; } public int getSerialNumber(){ return this.serialNumber; } public boolean canFly(){ return this.flies; } public boolean isAutonomous(){ return this.autonomous; } public boolean isTeleoperated(){ return this.teleoperated; } public String getCapabilities(){ StringBuilder str = new StringBuilder(); if(this.flies){str.append("canFly");str.append(" ");} if(this.autonomous){str.append("autonomous");str.append("...
Can someone explain in detail how to solve the below indefinite integral? I am getting really...
Can someone explain in detail how to solve the below indefinite integral? I am getting really confused when it comes to the U-substitution part and do not understand how 9 ends up in the denominator. I know the correct answer is [-2(ln3x+1)-3x]/9 + C, but I do not know how to get there. ∫(2x)/(3x+1) dx
Develop the following code for quiz (i am getting some errors)in python in such a manner...
Develop the following code for quiz (i am getting some errors)in python in such a manner that it shoulde be having extra 3 attempts(for wrong answrs) for all questions if the user entered wrong answer · for ex: If the user entered correct answer for first question #then 3 attempts will be carried to next questions. If the user entered 3 times wrong answer in 1st question itself means it sholud display as no more attempts and you got o...
In Python I have a code: here's my problem, and below it is my code. Below...
In Python I have a code: here's my problem, and below it is my code. Below that is the error I received. Please assist. Complete the swapCaps() function to change all lowercase letters in string to uppercase letters and all uppercase letters to lowercase letters. Anything else remains the same. Examples: swapCaps( 'Hope you are all enjoying October' ) returns 'hOPE YOU ARE ALL ENJOYING oCTOBER' swapCaps( 'i hope my caps lock does not get stuck on' ) returns 'I...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT