Thread: short matrix problem

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    210

    short matrix problem

    In this program I only input the number of columns and the entries of the matrix. The program then determines the number of rows. The program works perfectly for the correct number of entries like 4 entries for a 2 by 2 matrix. However it doesnt work for 3 entries for a 2 by 2 matrix. Please reply fast
    Code:
    
    
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    
    int  main()
    {
      int m,x, n, c = 0, d,k, matrix[10][10], transpose[10][10], product[10][10];
    
    
      printf("Enter the number of columns of matrix ");
      scanf("%d",&m);
      if(m<=0){
        printf("You entered a invalid value.");
        exit(0);
      }
      else{
        printf("Enter the elements of matrix \n");
    
    
        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] == 99)
              break;
          }
      }
    
    
      printf("\nHere is your matrix:\n");
      int i;
    
    
      for(i=0;i<c;i++)
        {
          for(d=0;d<m;d++)
            {
    
    
     printf("%3d ",matrix[i][d]);
            }
    printf("\n");
        }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by kiwi101
    Please reply fast
    fast

    (If I wanted to be nasty, I would have let my reply stop here, but I'll be kind. Next time, don't insist that people "reply fast".)

    Quote Originally Posted by kiwi101
    However it doesnt work for 3 entries for a 2 by 2 matrix.
    How does it not work? Also, you should indent your code properly to make it easier to read.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    210
    sorry
    i was just getting tensed since ive bin working on this for ages and i cant get any ideas

    it does work if i put 3 entries but not the way it shoud like itll give me garbage values.

  4. #4
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    Do you want an m x m, or a 10 x m matrix?
    Code:
    while(!asleep) {
       sheep++;
    }

  5. #5
    Registered User
    Join Date
    Oct 2012
    Posts
    6
    u miss fflush()... u need to clear ur buffer rite after input from user else ur next scanf will still take ur previous entered number as input(in the case that next scanf very close together)


    question for u: are u on a open book test or something?

  6. #6
    Registered User
    Join Date
    May 2012
    Posts
    210
    no im not i just have this due in a couple of hours.

    Could u elaborate? ive neve rused fflush

  7. #7
    Registered User
    Join Date
    May 2012
    Posts
    210
    Okay I have an idea I just dont know how to quite put it down. If the user doesnt enter m*n entries then the program should just exit
    kindof like this
    Code:
     if(m*c%m != 0){
                printf("NO");
                exit(0);
              }
                else{
    print

  8. #8
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    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?
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  9. #9
    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. #10
    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

  11. #11
    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.

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

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

  14. #14
    Registered User
    Join Date
    May 2012
    Posts
    210
    I did reply to laserlight.
    Look im just trying to brainstorm i want the matrix to be a m*n matrix.
    i want my code to exit if the user doesnt enter m*n values.
    Could you please give me some direction?
    Im waitng here

  15. #15
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    If it's supposed to be m x n why don't you set n to anything? You're not even using it.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

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