Thread: Whats wrong

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    8

    Whats wrong

    Code:
    /* Matrix */
    
    #include <stdio.h>
    
    void SetMatrixA();
    void AddMatrixBtoA();
    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");
            printf("\nPlease enter your choice: ");
            scanf("%d", &choice);
               
            switch(choice)  
            {
            case 1:
            SetMatrixA();   
            break;
               
            case 2:
            AddMatrixBtoA(5, 5);
            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");
            }
            }
    Last edited by Dave_Sinkula; 03-07-2008 at 01:00 PM. Reason: Tagged

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> 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. #3
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    >> 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

    [edit] Holy S***. I'm turning into Daved [/edit]
    Last edited by Brad0407; 03-07-2008 at 01:02 PM.
    Don't quote me on that... ...seriously

  4. #4
    Registered User
    Join Date
    Mar 2008
    Posts
    8

    Having trouble with Multiplication and Transpose

    /code/
    /* Matrix */

    #include <stdio.h>

    void SetMatrixA();
    void AddMatrixBtoA();
    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");
    printf("\nPlease enter your choice: ");
    scanf("%d", &choice);

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

    case 2:
    AddMatrixBtoA(5, 5);
    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. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    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];
    [edit]What are rowsA and columnsA here?
    Code:
          case 4:
             TransposeMatrixA(rowsA,columnsA);
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    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. #7
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Geesh, the correct use was to be
    Code:
       your code

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    <<threads merged and bouncing quickly>>
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  9. #9
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    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. #10
    Registered User
    Join Date
    Mar 2008
    Posts
    8
    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++)
              {
              printf("Please enter A[%d][%d]",x,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++)
              {
              printf("Please enter B[%d][%d]",x,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");
            }
    }
    void AddMatrixAtoB()
    {
            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");
            printf("\nPlease enter your choice:");
            scanf("%d",&select);
            
    switch(select)
    {
            case 1:
            {  
            setMatrixA();
            break;
            }
             case 2:
            {
            setMatrixB();
            AddMatrixAtoB();
            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
    Last edited by Dave_Sinkula; 03-07-2008 at 02:18 PM. Reason: Fixed tags for the 3rd and last time.

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by matrixhelp View Post
    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");
            printf("\nPlease enter your choice:");
            scanf("%d",&select);
    What else did you expect this to do? Don't you remember writing the menu?

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    [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. #13
    Registered User
    Join Date
    Mar 2008
    Posts
    8
    [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");
    printf("\nPlease enter your choice:");
    scanf("&#37;d",&select);

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

    case 2:
    {
    setMatrixB();
    AddMatrixAtoB();
    break;
    }

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

    [/code]

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

    why does it print like this

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by matrixhelp View Post
    [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 subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 10:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM