Thread: short matrix problem

  1. #16
    Registered User
    Join Date
    May 2012
    Posts
    210
    Yes you are correct
    Letme show u an example

    Enter the elements of matrix
    1 2 3 4
    99


    Here is your matrix:
    1 2
    3 4

    BUT:
    Enter the elements of matrix
    1 2 3
    99

    Here is your matrix:
    1 2

  2. #17
    Registered User
    Join Date
    May 2012
    Posts
    210
    instead of displaying just 1 2 i want the program to say "invald # of entries" and then exit

    could you please tell me how to do that?

  3. #18
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    So you want to ensure that the user enters complete rows.

    Two possibilities:

    1. Check that the value of d is 0 (i.e., they entered 99 as the first value in a column).

    2. Write it so entering the magic number (99) only works if it's the first value in a column.
    Code:
    for (c = 0; c < 10; c++)
    {
        scanf("%d", &val);
        if (val == 99)
            break;
        matrix[c][0] = val;
        for (d = 1; d < m; d++)  // note that d starts at 1 here
        {
            scanf("%d", &matrix[c][d]);
        }
    }
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  4. #19
    Registered User
    Join Date
    May 2012
    Posts
    210
    when I enter 1,2,3,4,99 this is what I get back from the code
    Here is your matrix:
    1 2
    3 4
    BUT

    when i enter 1,2,3,99 this is what i get back
    Here is your matrix:
    1 2

  5. #20
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Code:
        for( c = 0 ; c < 10 ; c++ )
          {
            for( d = 0; d < m ; d++ )
            {
                scanf("%d",&matrix[c][d]);
                
                if (matrix[c][d] == 99) // 99 is  variable to use as a break
                   break;
                  
            }
            
            if (matrix[c][d] == 99)
              break;
          }
         
      }
      if(d<m)
         printf("\nArray data is missing.\n");
    Detects short rows, (last row only), but won't detect if a full row is missing.

  6. #21
    Registered User
    Join Date
    May 2012
    Posts
    210
    Im so grateful to the two of you but both codes didnt work for me
    The problem i have with oogaboogas code is if they enter 99 first how will the program know when the user has finished entering all entries
    and the problem with adaks code is for some reason it prints the statement everytime

  7. #22
    Registered User
    Join Date
    May 2012
    Posts
    210
    Is there anyway you guys can write this statement in code and tellme where to place it it might work
    Code:
    if( #ofentries != c*m)
    printf("Invalid number of entries);
    exit(0);
    else{
    print

  8. #23
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    How about:
    Code:
    if (d > 0) {
        printf("...");
        exit(1);
    }
    You don't need the else part, since the if part exits.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  9. #24
    Registered User
    Join Date
    Oct 2012
    Posts
    6
    Quote Originally Posted by oogabooga View Post
    Why haven't you answered laserlight's or TheBigH's questions?

    Neko Tan doesn't know what it's talking about. fflush is not needed.

    Indent your code properly. Look at post #7. Is that really how you mean your code to look?
    maybe this happen only on my compiler... it seem like stuck on 2nd scanf (if without fflush)...

  10. #25
    Registered User
    Join Date
    May 2012
    Posts
    210
    OMGSH thankyou soo muchit worked
    but yeah i was just reading about fflush online too cuz mine also got stuck on the 2nd scanf

  11. #26
    Registered User
    Join Date
    May 2012
    Posts
    210
    i had one last question sorry to annoy you so much today
    i want the program to exit if the user inputs a value in the matrix less than 0. i plan on writing it like
    Code:
    if(matrix[c][d] < 0){
    printf("not valid number");
    exit(1);}
    but ive been trying this for a while but i cant seem to understand where exactly in the code i should put it
    ive tired numerous times but nothing has worked out

  12. #27
    Registered User
    Join Date
    Oct 2012
    Posts
    6
    Quote Originally Posted by kiwi101 View Post
    i had one last question sorry to annoy you so much today
    i want the program to exit if the user inputs a value in the matrix less than 0. i plan on writing it like
    Code:
    if(matrix[c][d] < 0){
    printf("not valid number");
    exit(1);}
    but ive been trying this for a while but i cant seem to understand where exactly in the code i should put it
    ive tired numerous times but nothing has worked out
    Code:
     for( c = 0 ; c < 10 ; c++ )      {
            for( d = 0 ; d < m ; d++ )
              {
                scanf("%d",&matrix[c][d]);
                if (matrix[c][d] == 99) // 99 is  variable I declared to use as a break
               break;
     
              if (matrix[c][d] < 0){
              printf ("not valid number");
              getch();
               exit(1);}
     
     
              }
            if (matrix[c][d] == 99)
              break;
          }
      }
    check everytime after scanf. sry, im new here... im have no idea how to edit with color on ur code...
    Last edited by Neko Tan; 10-03-2012 at 10:55 PM.

  13. #28
    Registered User
    Join Date
    May 2012
    Posts
    210
    Thanks!!!! Its working!

    Could you tell me why my multiplication of this matrix and its transpose is coming wrong. Its usually wrong if its not a square matrix.

    Code:
     for( i = 0 ; i < c ; i++ )
        {
          for( d = 0 ; d < m ; d++ )
            {
              transpose[d][i] = matrix[i][d];
            }
        }
    
    
      printf("Transpose of entered matrix :-\n");
    
    
      for( i = 0 ; i < m ; i++ )
        {
          for( d = 0 ; d < c ; d++ )
            {
              printf("%3d\t",transpose[i][d]);
            }
    
    
          printf("\n");
        }
      // printf("The product of matrix and transpose is %d:");
      for( i = 0 ; i < c ; i++ )
        {
          for( d = 0 ; d < m ; d++ )
            {
              product[i][d]=0;
              for(k=0;k<c;k++) {
                product[i][d] = product[i][d] + matrix[i][k] * transpose[k][d];
              }
            }
        }
      printf("\nThe Product Of The Two Matrices Is:\n\n");
      for(i=0; i<c; i++) {
        for(d=0; d<k; d++) {
          printf(" %3d ",product[i][d]);
        }
        printf("\n");
      }
    
    
    
    
    
    
      return 0;

  14. #29
    Registered User
    Join Date
    May 2012
    Posts
    210
    The transpose is right its just the multiplication of the matrix and transpose sometimes comes wrong

  15. #30
    Registered User
    Join Date
    May 2012
    Posts
    210
    sorry about the indentation

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 10-01-2012, 10:57 PM
  2. How come short + int = int but short + uint = long?
    By y99q in forum C# Programming
    Replies: 2
    Last Post: 10-29-2011, 11:16 AM
  3. Please help! short while loop tracking problem.
    By matthayzon89 in forum C Programming
    Replies: 7
    Last Post: 04-22-2010, 12:29 PM
  4. Replies: 2
    Last Post: 04-21-2008, 07:43 PM
  5. Replies: 7
    Last Post: 02-08-2008, 06:31 PM