Like Tree18Likes

thinking process

This is a discussion on thinking process within the C Programming forums, part of the General Programming Boards category; Hey all! So in the code I have posted below, the user can assign upto 4 matrices A,B,C or D. ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    106

    thinking process

    Hey all!
    So in the code I have posted below, the user can assign upto 4 matrices A,B,C or D. Now I want to do amtrices addition between them. I am using a switch statement but I just don't know what to do. I need some help thinking of a way.

    Code:
    #include<stdio.h>
    #include <stdlib.h>
    
    
    void matrix_display(int ****matrix, int row_count, int column_count);
    
    
    
    
    int main (void)
    {
      int row_size1,col_size1,row_size2,col_size2,flag =1;
      int ****A[4];
      int i = 0, j = 0;
      char a,c;
    
    
    
    
    
    
    do
      {
        
    printf("\ncmd> ");
    c = getchar();
    
    if(c == 'A')
    {
    scanf("%d %d",&row_size1,&col_size1);
    
    A[0]= malloc(row_size1*sizeof(int*));
    
    for(i=0;i<row_size1;i++)
        {
          A[0][i]=malloc(col_size1*sizeof(int));
        }
    for(i=0;i<row_size1;i++)
        {
          for(j=0;j<col_size1;j++)
            {
              scanf("%d",&A[0][i][j]);
              //      if (A[i][j] == 99) // 'x' is character variable i declared
                        // break;
            }
        }
     
    matrix_display(A[0], row_size1, col_size1);
          }
     
    
    if(c == 'B')
    {
    scanf("%d %d",&row_size1,&col_size1);
    
    A[1]= malloc(row_size1*sizeof(int*));
     
    for(i=0;i<row_size1;i++)
           {
    A[1][i]=malloc(col_size1*sizeof(int));
           }
    
    for(i=0;i<row_size1;i++)
           {
             for(j=0;j<col_size1;j++)
               {
                 scanf("%d",&A[1][i][j]);
                 //      if (A[i][j] == 99) // 'x' is character variable i declared
                 // break;
               }
           }
    
    matrix_display(A[1], row_size1, col_size1);
    }
    
    
    
    if(c == 'C')
    {
    scanf("%d %d",&row_size1,&col_size1);
     
    A[2]= malloc(row_size1*sizeof(int*));
         
    for(i=0;i<row_size1;i++)
    {
    A[2][i]=malloc(col_size1*sizeof(int));
      }
    
    
    for(i=0;i<row_size1;i++)
           {
             for(j=0;j<col_size1;j++)
               {
                 scanf("%d",&A[2][i][j]);
                 //      if (A[i][j] == 99) // 'x' is character variable i declared
                 // break;
               }
           }
         
    matrix_display(A[2], row_size1, col_size1);
       }
    
    
    
    if(c == 'D')
    {
    scanf("%d %d",&row_size1,&col_size1);
    
    
    A[3]= malloc(row_size1*sizeof(int*));
         
    for(i=0;i<row_size1;i++)
    {
    A[3][i]=malloc(col_size1*sizeof(int));
      }
     
    for(i=0;i<row_size1;i++)
           {
             for(j=0;j<col_size1;j++)
               {
                 scanf("%d",&A[3][i][j]);
                 //      if (A[i][j] == 99) // 'x' is character variable i declared
                 // break;
               }
           }
    matrix_display(A[3], row_size1, col_size1);
       }
     
    if ( c == '+')
       {
         scanf("%c %c", &ptr1, ptr2);
         switch(ptr1);
       
    case 'A':
    ptr1=A[0];
    break;
       
    case 'B':
    ptr1=A[1];
    break;
       
    case 'C':
    ptr1=A[2];
    break;
       
    case 'D':
    ptr1=A[3];
    break;
         
    switch(ptr2);
       
    case 'A':
    ptr2=A[0];
    break;
       
    case 'B':
    ptr2=A[1];
    break;
       
    case 'C':
    ptr2=A[2];
    break;
       
    case 'D':
    ptr2=A[3];
    break;
    }
    }
     while(flag == 1);
    //return 0;
    }
    
    
    void matrix_display(int ****A, int row_size1, int col_size1)
    {
      int i,j;
     printf("The required matrix is\n");
      for(i=0;i<row_size1;i++)
        {
          for(j=0;j<col_size1;j++)
          printf("%d ",A[i][j]);
          printf("\n");
        }
      for(i=0;i<row_size1;i++)
        {
          free(A[i]);
        }
      free(A);
    }
    Last edited by zafy; 11-18-2012 at 11:21 AM.

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    OMG a Four Star Programmer!

    That code is all so horribly wrong. Just stop what you're doing and try something a lot simpler.
    Last edited by iMalc; 11-18-2012 at 11:35 AM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    106
    Please don't hate on my program it took me foreverrrrrrrr to get it working
    but if you really think its necessary for me to change it, im willing to go through that long arduous journey again.
    but if its not that important could you help me with the addition bit

  4. #4
    qny
    qny is offline
    Registered User
    Join Date
    Sep 2012
    Posts
    355

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    106
    come on guys cut some slack

  6. #6
    Registered User
    Join Date
    Nov 2012
    Posts
    106
    look it took me a really long time and effort to come up with this code. I know that having 3 star and 4 star programs isn't advised, but is there any way I could still use this code and add the matrices. I have been working on this for a while now and it would be really frustrating if I had to go back and fix the code again. But I trust your expertise and if you really think I should change it, then could you tell me how I should convert my code. the article doesn't really help they're just complaining in that

  7. #7
    qny
    qny is offline
    Registered User
    Join Date
    Sep 2012
    Posts
    355
    zafy: if you want to be helped, help us help you.

    Make your code nice (mind the indentation, horizontal and vertical spacing).
    Break your code into smaller functions (searching ~200 lines of code for a problem is very very different than searching ~20 lines of code).

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,544
    We basically did all this last time.
    quick question
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Registered User
    Join Date
    Nov 2012
    Posts
    106
    Sorry I honestly thought i indented it right this time.
    well this is my original code that works and only asks for matrices. The code above just has the switch statements added.
    Code:
     
    Code:
    #include<stdio.h>
    #include <stdlib.h>
    
    
    void matrix_display(int ****matrix, int row_count, int column_count);
    
    
    int main (void)
    {
      int row_size1,col_size1,row_size2,col_size2,flag =1;
      int ****A[4];
      int i = 0, j = 0;
      char a,c;
    
    
    do
      {
    printf("\ncmd> ");
    c = getchar();
    
    
    if(c == 'A')
    {
    scanf("%d %d",&row_size1,&col_size1);
    A[0]= malloc(row_size1*sizeof(int*));
    for(i=0;i<row_size1;i++)
    {
    A[0][i]=malloc(col_size1*sizeof(int));
     }
    for(i=0;i<row_size1;i++)
      {
        for(j=0;j<col_size1;j++)
            {
    scanf("%d",&A[0][i][j]);
    
    
            }
        }
    matrix_display(A[0], row_size1, col_size1);
          }
    
    
    if(c == 'B')
    {
    scanf("%d %d",&row_size1,&col_size1);
    A[1]= malloc(row_size1*sizeof(int*));
    for(i=0;i<row_size1;i++)
    {
    A[1][i]=malloc(col_size1*sizeof(int));
     }
    for(i=0;i<row_size1;i++)
      {
        for(j=0;j<col_size1;j++)
          {
     scanf("%d",&A[1][i][j]);
    
    
               }
           }
     matrix_display(A[1], row_size1, col_size1);
     }
    
    
    if(c == 'C')
    {
    scanf("%d %d",&row_size1,&col_size1);
    A[2]= malloc(row_size1*sizeof(int*));
    for(i=0;i<row_size1;i++)
    {
    A[2][i]=malloc(col_size1*sizeof(int));
     }
    for(i=0;i<row_size1;i++)
      {
       for(j=0;j<col_size1;j++)
         {
    scanf("%d",&A[2][i][j]);
    
    
               }
           }
     matrix_display(A[2], row_size1, col_size1);
       }
    
    
    if(c == 'D')
    {
    scanf("%d %d",&row_size1,&col_size1);
    A[3]= malloc(row_size1*sizeof(int*));
    for(i=0;i<row_size1;i++)
    {
    A[3][i]=malloc(col_size1*sizeof(int));
     }
    for(i=0;i<row_size1;i++)
      {
       for(j=0;j<col_size1;j++)
         {
    scanf("%d",&A[3][i][j]);
    
    
               }
           }
    matrix_display(A[3], row_size1, col_size1);
       }
    
    
    }
    while(flag == 1);
    }
    
    
    void matrix_display(int ****A, int row_size1, int col_size1)
    {
    int i,j;
    printf("The required matrix is\n");
    for(i=0;i<row_size1;i++)
      {
        for(j=0;j<col_size1;j++)
          printf("%d ",A[i][j]);
          printf("\n");
        }
    for(i=0;i<row_size1;i++)
        {
          free(A[i]);
        }
    free(A);
    }

  10. #10
    Registered User
    Join Date
    Nov 2012
    Posts
    106
    yeah now I want to add the matrices A and B and I need some advice on how to do it
    I used switch statements like in the code above but I just think I am going in the wrong direction

  11. #11
    Registered User
    Join Date
    Nov 2012
    Posts
    106
    So should I use a switch statement or what?

  12. #12
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,650
    Any sane programmer takes one look at this

    Code:
    int ****A[4];
    and says to his or herself: "This is a person may have an inkling of what they're doing, but just started throwing *s and []s around to get the compiler to shut the hell up."
    Salem likes this.

  13. #13
    Registered User
    Join Date
    Nov 2012
    Posts
    106
    Okay fine I'll change the code.
    where should i start from?

  14. #14
    Registered User
    Join Date
    Nov 2012
    Posts
    106
    some input guys?

  15. #15
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You mention 4 matrices A B C D. So two idea's:

    1) Using 4 matrices: A, B, C, D.

    You have one function which adds two matrices together, and call it twice with the different matrix names.

    or

    2) Using one large matrix with 4 ranges of indices within it to separate out the 4 divisions you want.
    low0 to high0, low1 to high1, low2 to high2, low3 to high3.

    In a nested set of loops, that would be easy to code up.

Page 1 of 5 12345 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Thinking about learning something else
    By tiachopvutru in forum A Brief History of Cprogramming.com
    Replies: 31
    Last Post: 06-24-2008, 02:45 PM
  2. too much thinking?
    By willc0de4food in forum Windows Programming
    Replies: 9
    Last Post: 10-03-2006, 05:34 AM
  3. Child Process & Parent Process Data :: Win32
    By kuphryn in forum Windows Programming
    Replies: 5
    Last Post: 09-11-2002, 12:19 PM
  4. I know you've been thinking about this...
    By Aran in forum A Brief History of Cprogramming.com
    Replies: 43
    Last Post: 11-08-2001, 03:13 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21