# Whats wrong

This is a discussion on Whats wrong within the C Programming forums, part of the General Programming Boards category; Code: /* Matrix */ #include <stdio.h> void SetMatrixA(); void AddMatrixBtoA(); void DisplayMatrixA(); void MultiplyMatrixBtoA(); void TransposeMatrixA(); int rowsA; int rowsB; ...

1. ## Whats wrong

Code:
```/* Matrix */

#include <stdio.h>

void SetMatrixA();
void DisplayMatrixA();
void MultiplyMatrixBtoA();
void TransposeMatrixA();
int rowsA;
int rowsB;
int columnsA;
int columnsB;
int A1[5][5];
int C1[5][5];

int main(void)
{
int rowsA;
int columnsA;
int A[5][5];
int choice;

printf("\n");
printf("Matrix A\n");

for(columnsA=0; columnsA<3; columnsA++)
{
for(rowsA=0; rowsA<3; rowsA++)
{
A[0][0]= 1.0;
A[0][1]= 0.0;
A[0][2]= 0.0;
A[1][0]= 0.0;
A[1][1]= 1.0;
A[1][2]= 0.0;
A[2][0]= 0.0;
A[2][1]= 0.0;
A[2][2]= 1.0;

printf("&#37;5d", A[rowsA][columnsA]);
}
printf("\n");
}
while(1)
{
printf("(1)Set Matrix A.\n");
printf("(2)Add a Matrix B\n");
printf("(3)Multiply with Matirx B.\n");
printf("(4)Transpose Matrix A.\n");
printf("(5)Exit.\n");
scanf("%d", &choice);

switch(choice)
{
case 1:
SetMatrixA();
break;

case 2:
break;

case 3:
MultiplyMatrixBtoA(5, 5);
break;

case 4:
TransposeMatrixA(rowsA,columnsA);
break;

case 5:
return 0;
}
}
}

void SetMatrixA()
{
int rowsA;
int columnsA;
int i;
int j;

printf("Please enter the number of rows for A: ");
scanf("%d", &rowsA);
printf("Please enter the number of columns for A: ");
scanf("%d", &columnsA);
printf("Please enter Elements of the first Matrix:\n");

for(i=0; i<rowsA; i++)
{
for(j=0; j<columnsA; j++)
{
printf("A[%d][%d]: ", i,j);
scanf("%d", &A1[i][j]);
}
}
printf("\nMatrix A is:\n\n");

for(i=0; i<rowsA; i++)
{
for(j=0; j<columnsA; j++)
{
printf("%5d", A1[i][j]);
}
printf("\n");
}

}
void DisplayMatrixA(int rowsA, int columnsA)
{
int i;
int j;

for(i=0; i< rowsA; i++)
{
for(j=0; j< columnsA; j++)
{
printf("%5d", A1[i][j]);
}
printf("\n");
}
}

void AddMatrixBtoA(int i, int j)
{
int B1[5][5];

printf("Please enter the number of rows for B: ");
scanf("%d", &rowsB);
printf("Please enter the number of columns for B: ");
scanf("%d", &columnsB);
printf("Please enter Elements of the second Matrix: \n");

for(i=0; i<rowsB; i++)
{
for(j=0; j<columnsB; j++)
{
printf("B[%d][%d]: ", i,j);
scanf("%d", &B1[i][j]);
}
}
printf("\nMatrix B is:\n\n");
DisplayMatrixA(rowsB, columnsB);

printf("\nMatrix C is:\n\n");

for(i=0; i<rowsB; i++)
{
for(j=0; j<columnsB; j++)
{
C1[i][j]= A1[i][j] + B1[i][j];
printf("%5d", C1[i][j]);
}
printf("\n");
}
}

void MultiplyMatrixBtoA(int rowsB, int columnsB, int columnsA, int rowsA)
{
int i;
int j;
int k;
int B1[5][5];
int n;

printf("Please enter the number of rows for B: ");
scanf("%d", &rowsB);
printf("Please enter the number of columns for B: ");
scanf("%d", &columnsB);
printf("Please enter Elements of the second Matrix: \n");

for(k=0; k<rowsB; k++)
{
{
for(n=0; n<columnsB; n++)
{
printf("B[%d][%d]: ", k,n);
scanf("%d", &B1[k][n]);
}
}

printf("\nMatrix B is:\n\n");
DisplayMatrixA(rowsB, columnsB);
}
for(i=0; i<rowsA; i++)
{
for(n=0; n<columnsB; n++)
{
C1[i][n]=0;
for(k=0; k<columnsA; k++)
C1[i][n]=C1[i][n] + A1[i][k] * B1[k][n];
}
printf("\nMultiplication of Matrix:-\n");
for(i=0; i<rowsA; i++)
for(i=0; i<rowsA; i++)
{
for(n=0; n<columnsB; n++)
printf("5%d", C1[i][n]);
printf("\n");
}

}
}

void TransposeMatrixA(int rowsA, int columnsA)
{
int i;
int j;
int A2[5][5];

A2[j][i]= A1[i][j];
printf("\n\nTranspose of Matrix is:\n\n");
for(i=0; i<rowsA; i++)
{
for(j=0; j<columnsA; j++)
{
printf("%5d", A2[i][j]);
}
printf("\n");
}
}```

2. >> Whats wrong

The only things I can see wrong at first glance have to do with your post. It's best to post C code in the C forum. You should post code in [code][/code] tags. And if you are having a problem you should post your question and details of why you think something is wrong rather than just dumping code.

3. >> What's wrong
You didn't use [code][/code] tags and you posted C code in the C++ forum. You also didn't tell us anything about:
1. What input you're giving the code
2. What output you expect
3. What output you get
4. Whether it even compiles and any warnings or error messages you get

 Holy S***. I'm turning into Daved [/edit]

4. ## Having trouble with Multiplication and Transpose

/code/
/* Matrix */

#include <stdio.h>

void SetMatrixA();
void DisplayMatrixA();
void MultiplyMatrixBtoA();
void TransposeMatrixA();
int rowsA;
int rowsB;
int columnsA;
int columnsB;
int A1[5][5];
int C1[5][5];

int main(void)
{
int rowsA;
int columnsA;
int A[5][5];
int choice;

printf("\n");
printf("Matrix A\n");

for(columnsA=0; columnsA<3; columnsA++)
{
for(rowsA=0; rowsA<3; rowsA++)
{
A[0][0]= 1.0;
A[0][1]= 0.0;
A[0][2]= 0.0;
A[1][0]= 0.0;
A[1][1]= 1.0;
A[1][2]= 0.0;
A[2][0]= 0.0;
A[2][1]= 0.0;
A[2][2]= 1.0;

printf("%5d", A[rowsA][columnsA]);
}
printf("\n");
}
while(1)
{
printf("(1)Set Matrix A.\n");
printf("(2)Add a Matrix B\n");
printf("(3)Multiply with Matirx B.\n");
printf("(4)Transpose Matrix A.\n");
printf("(5)Exit.\n");
scanf("%d", &choice);

switch(choice)
{
case 1:
SetMatrixA();
break;

case 2:
break;

case 3:
MultiplyMatrixBtoA(5, 5);
break;

case 4:
TransposeMatrixA(rowsA,columnsA);
break;

case 5:
return 0;
}
}
}

void SetMatrixA()
{
int rowsA;
int columnsA;
int i;
int j;

printf("Please enter the number of rows for A: ");
scanf("%d", &rowsA);
printf("Please enter the number of columns for A: ");
scanf("%d", &columnsA);
printf("Please enter Elements of the first Matrix:\n");

for(i=0; i<rowsA; i++)
{
for(j=0; j<columnsA; j++)
{
printf("A[%d][%d]: ", i,j);
scanf("%d", &A1[i][j]);
}
}
printf("\nMatrix A is:\n\n");

for(i=0; i<rowsA; i++)
{
for(j=0; j<columnsA; j++)
{
printf("%5d", A1[i][j]);
}
printf("\n");
}

}
void DisplayMatrixA(int rowsA, int columnsA)
{
int i;
int j;

for(i=0; i< rowsA; i++)
{
for(j=0; j< columnsA; j++)
{
printf("%5d", A1[i][j]);
}
printf("\n");
}
}

void AddMatrixBtoA(int i, int j)
{
int B1[5][5];

printf("Please enter the number of rows for B: ");
scanf("%d", &rowsB);
printf("Please enter the number of columns for B: ");
scanf("%d", &columnsB);
printf("Please enter Elements of the second Matrix: \n");

for(i=0; i<rowsB; i++)
{
for(j=0; j<columnsB; j++)
{
printf("B[%d][%d]: ", i,j);
scanf("%d", &B1[i][j]);
}
}
printf("\nMatrix B is:\n\n");
DisplayMatrixA(rowsB, columnsB);

printf("\nMatrix C is:\n\n");

for(i=0; i<rowsB; i++)
{
for(j=0; j<columnsB; j++)
{
C1[i][j]= A1[i][j] + B1[i][j];
printf("%5d", C1[i][j]);
}
printf("\n");
}
}

void MultiplyMatrixBtoA(int rowsB, int columnsB, int columnsA, int rowsA)
{
int i;
int j;
int k;
int B1[5][5];
int n;

printf("Please enter the number of rows for B: ");
scanf("%d", &rowsB);
printf("Please enter the number of columns for B: ");
scanf("%d", &columnsB);
printf("Please enter Elements of the second Matrix: \n");

for(k=0; k<rowsB; k++)
{
{
for(n=0; n<columnsB; n++)
{
printf("B[%d][%d]: ", k,n);
scanf("%d", &B1[k][n]);
}
}

printf("\nMatrix B is:\n\n");
DisplayMatrixA(rowsB, columnsB);
}
for(i=0; i<rowsA; i++)
{
for(n=0; n<columnsB; n++)
{
C1[i][n]=0;
for(k=0; k<columnsA; k++)
C1[i][n]=C1[i][n] + A1[i][k] * B1[k][n];
}
printf("\nMultiplication of Matrix:-\n");
for(i=0; i<rowsA; i++)
for(i=0; i<rowsA; i++)
{
for(n=0; n<columnsB; n++)
printf("5%d", C1[i][n]);
printf("\n");
}

}
}

void TransposeMatrixA(int rowsA, int columnsA)
{
int i;
int j;
int A2[5][5];

A2[j][i]= A1[i][j];
printf("\n\nTranspose of Matrix is:\n\n");
for(i=0; i<rowsA; i++)
{
for(j=0; j<columnsA; j++)
{
printf("%5d", A2[i][j]);
}
printf("\n");
}
}

/code//

5. main.c: In function `TransposeMatrixA':
main.c:211: warning: 'i' might be used uninitialized in this function
main.c:212: warning: 'j' might be used uninitialized in this function
Code:
``` int i;
int j;
int A2[5][5];

A2[j][i]= A1[i][j];```
What are rowsA and columnsA here?
Code:
```      case 4:
TransposeMatrixA(rowsA,columnsA);```

6. Bad indention and incorrect use of

Code:
`blah`

Also can you please elaborate on your exact error conditions. What is the input and what should the output be?

7. Geesh, the correct use was to be
Code:
`   your code`

8. <<threads merged and bouncing quickly>>

9. Lol, seems too many of us jumped on this case and added notes roughly at the same time.

My quick thoughts are.

Whats the purpose of

Code:
```for(k=0; k<rowsB; k++)
{
{```
Also be cautious of the use of scanf. This is ideal for reading from known input, but for user input they can enter anything thus overflowing any data buffers set aside for your variables.

10. Code:
```/*Matrix*/

#include<stdio.h>
int i=0;
int j=0;
int k=0;
int col=3;
int row=3;
int rowb=0;
int t;
int colb=0;
int rowc;
int colc;
int select;
double A[5][5]={{1.0,0.0,0.0},{0.0,1.0,0.0},{0.0,0.0,1.0}};
double B[5][5];
double C[5][5];

void TransposeA()
{
for(j=0;j<col;j++)
{
for(i=0;i<row;i++)
{
C[j][i]= A[i][j];
}

}
t=col;
col=row;
row=t;

for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
A[i][j]= C[i][j];
}
}
}

void MultMatrixBtoA()
{
for(i=0; i<row; i++)
{
for(j=0; j<colb; j++)
{
C[i][j]=0;
for(k=0;k<col;k++)
{
C[i][j]+=A[i][k]*B[k][j];
}
}
}

for(i=0; i<row; i++)
{
for(j=0; j<colb; j++)
{
A[i][j] = C[i][j];
}
}
col = colb;
}

void setMatrixA()
{
int x=0;
int y=0;

printf("Please enter the number of rows A: ");
scanf("&#37;d",&row);
printf("Please enter the number of columns for A: ");
scanf("%d",&col);

for(x = 0; x < row; x++)
{
for(y = 0; y < col; y++)
{
scanf("%lf",&A[x][y]);
}
}

printf("\n");
}
void setMatrixB()
{
int x=0;
int y=0;

printf("Please enter the number of rows for B: ");
scanf("%d",&rowb);
printf("Please enter the number of columns for B: ");
scanf("%d",&colb);

for(x = 0; x < rowb; x++)
{
for(y = 0; y < colb; y++)
{
scanf("%lf",&B[x][y]);
}
}
printf("\n");
}
void DisplayMatrixA()
{
for(i=0;i<row;i++)
{
if(i==1)
{
printf("A = ");
}
else
{
printf(" ");
}
printf("|");

for(j=0; j<col;j++)
{
printf(" %.1f ",A[i][j]);
}
printf("|");
printf("\n");
}
}
{
int x=0;
int y=0;

for(x = 0; x < row; x++)
{
for(y = 0; y < col; y++)
{
C[x][y]=A[x][y]+B[x][y];
}
}
for(j=0;j<col;j++)
for(i=0;i<row;i++)
{
A[i][j]= C[i][j];
}
}

int main(void)
{
int select;

while(select !=5)
{
DisplayMatrixA();

printf("\n(1)Set matrix A:\n ");
printf("(2)Add a matrix B:\n ");
printf("(3)Multiply with a matrix B:\n ");
printf("(4)Transpose\n");
printf("(5)Exit\n");
scanf("%d",&select);

switch(select)
{
case 1:
{
setMatrixA();
break;
}
case 2:
{
setMatrixB();
break;
}

case 3:
{
setMatrixB();
MultMatrixBtoA();
break;
}

case 4:
{
TransposeA();
break;
}

case 5:
{
break;
}

default:
{
printf("Invalid choice");
break;
}
}
}
return (0);
}```
when I compile this code,
it prints
(1)....
(2)...
(3)...
(4)....

why does it do that

11. Originally Posted by matrixhelp
why does it do that
Code:
```        printf("\n(1)Set matrix A:\n ");
printf("(2)Add a matrix B:\n ");
printf("(3)Multiply with a matrix B:\n ");
printf("(4)Transpose\n");
printf("(5)Exit\n");
scanf("%d",&select);```
What else did you expect this to do? Don't you remember writing the menu?

12. [code/] should be [/code]

Use Go Advanced and Preview Post to check your post before you submit to make sure it looks right. It might be annoying that we keep harping about this, but it is annoying to try to read code and help someone when it is formatted without the tags.

13. [code/]
while(select !=5)
{
DisplayMatrixA();

printf("(1)Set matrix A:\n ");
printf("(2)Add a matrix B:\n ");
printf("(3)Multiply with a matrix B:\n ");
printf("(4)Transpose\n");
printf("(5)Exit\n");
scanf("&#37;d",&select);

switch(select)
{
case 1:
{
setMatrixA();
break;
}

case 2:
{
setMatrixB();
break;
}

case 3:
{
setMatrixB();
MultMatrixBtoA();
break;
}

[/code]

it still does it
(1)
(2)
(3)
(4)
(5)

why does it print like this

14. Originally Posted by matrixhelp
[code/]
why does it print like this
My Hat of Guessing thinks you're actually asking about the spaces before 2 and 3 and 4; this would be obvious if you would master code tags -- look at anybody else's posts, or yours that have been fixed, and look at how the code tags are supposed to go.

Anyway: Because you tell it to. See all those spaces inside the quotes in your printf statements? They get printed out too.

Popular pages Recent additions