Thread: short matrix problem

  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 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++;
    }

  4. #4
    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?

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

  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
    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

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

  11. #11
    Registered User
    Join Date
    May 2012
    Posts
    210
    m = # of rows
    in this program my # of rows is called c

    n= # of columns
    in this program my # of colums is called m

    so a c*m matrix

  12. #12
    Registered User
    Join Date
    May 2012
    Posts
    210
    Could you guys please not make fun of me and help me plz

  13. #13
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Yes, I see your stressed out, so I deleted my funny post.

    Quickly, what's the problem?

  14. #14
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Your variable names are terrible. Why is n called m and m called c???

    Anyway, I think I'm beginning to understand what you're up to here. You want the user to enter the number of columns (but NOT the number of rows). Then the user inputs rows of data until they decide to stop by entering the number 99.

    Is that correct?

    Can you show an exact example of what doesn't work?
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  15. #15
    Registered User
    Join Date
    May 2012
    Posts
    210
    Thanks
    let me explain it with an example please tell me if you get it
    when the user enters 2 as the # of columns. And then enter 1,2,3,4 as the input. The program automatically displays a 2 by 2 matrix with 1,2,3,4 entries. However my problem is that if that same user accidentally only entered 1,2,3 then I want the program to exit using the exit function. But I don't know how to write it properly could you please tell me how i should write it.

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