how to get rows in a matrix when only columns are given

This is a discussion on how to get rows in a matrix when only columns are given within the C Programming forums, part of the General Programming Boards category; The program starts with a matrix with a maximum dimensions of 10 by 10. I have to ask the user ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    39

    Smile how to get rows in a matrix when only columns are given

    The program starts with a matrix with a maximum dimensions of 10 by 10. I have to ask the user to input the number of columns he would like the matrix to be. Once he enters that data he has to enter the values. For example he enters 2, therefore he has a maximum of 20 characters to enter. The problem is what if he enters only 13, or 14, or 6? How would I calculate the number of rows?
    I'm planning on putting a break, for example I tell the user to enter 'x' to stop the input. How can I go ahead and do this?

    Thanks in advance

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Read things in as string rather than as ints, then check whether they typed digits or an x.

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    39
    Thanks, but my main question is how to set up the rows after the number of columns has been defined and the values have been entered

  4. #4
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    So are you asking how to dynamically resize an array? That is what it sounds like, take a look at the realloc() function. If you are allowed to ask how many inputs there will be prior too however, you could always use a variable length array (VLA).
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,288
    It sounds to me like you don't. If they enter a number greater than ten then clearly it wont fit in a 10 by 10 matrix and so an error should be given.

    I'd use an if-statement to check the entered value against 'x' and then perhaps a break statement. Really would be best if you just try it and post that code if you have problems.
    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"

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    If the user chooses 2 columns, and then enters 6 numbers, you want to enter it into the matrix like this?

    Code:
    5 2 0 0 0 0 0 0 0 0
    6 8 0 0 0 0 0 0 0 0
    1 4 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0
    You can keep your 10 x 10 matrix size if you want, just set a column max int variable (colMax = 2), and use your count of entered numbers to calculate the rowMax you'll need:
    Code:
    if(count % 2 == 0) 
       rowMax = count /colMax  
    else
       rowMax = count / colMax + 1 (handles odd count of numbers
    
    //then a run through the data could be:
    
    for(r = 0, count = 0;r < rowMax;r++) { //dataMax = number of data values entered
       for(c = 0;c < colMax;c++) {
          printf("%d", matrix[r][c]);
          if(++count == dataMax)         //break out of the column loop
             break;
       }
       if(count == dataMax)              //break out of the row loop
          break;
    }
    Another way would be to first, set all the matrix elements to a value that is completely impossible for your data (-99 perhaps, for positive data). The data can be scanned through by using that "out of bounds" value.

    Code:
    #define OB -99  
    //(Just below your include file list. OB = out of bounds)
    
    for(row = 0;row < 10; row++) {
       for(col = 0; col < 10; col++) {
          if(matrix[row][col] == OB) 
             break;
          printf("%d", matrix[row][col]);
       }
    }
    If you need to work with the data a great deal, creating a matrix just the right size with malloc or calloc, might well be a better way to do this.
    Last edited by Adak; 09-10-2011 at 02:33 AM.

  7. #7
    Registered User
    Join Date
    Feb 2011
    Posts
    39

    Smile

    Thanks everyone

    iMalc> yes I know I have to check for errors. I'm planning on putting a break after the data entry to check if the input is not a number then the program will stop.

    Adak, that's exactly what I'm trying to do. But I'm not planning on keeping the zeroes. It's of no help. I just couldn't figure out how to calculate the number of rows. The forst part of your code is great
    Code:
     
      rowMax = count /colMax  
    else
       rowMax = count / colMax + 1 (handles odd count of numbers
    I'm going to go ahead and keep working on my program.

    Thanks again!

  8. #8
    Registered User
    Join Date
    Feb 2011
    Posts
    39
    I was working on my program and found a mistake with the code to fins the number of rows.

    The code checks if the number is divisible by 2 and makes the rows equals to the number of items divided by the number of columns. This is not the case for all of them. For example I have 5 columns and 14 items. 14/2= remainder 0. Rows = 14/5 = 2. I need 3 rows to fit all the data.

    I added something to the code but it doesn't seem to fix it
    Code:
     
    
    if(i%2==0){
        if((i%2)%2==0)
         rows = (i/cols);
        else
         rows = (i/cols)+1;
       }
    else   
        rows = (i/cols)+1;

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Why do you keep dividing by 2? Why not divide by cols instead?

  10. #10
    Registered User
    Join Date
    Sep 2011
    Posts
    111
    I am assuming you are only dealing with square matrices.

    Then columns = rows in all cases.

    A matrix can be different dimensions, such as 1x2 2x5 5x2 10x1 1x10 up to a max size of 10x10. A square matrix is when the rows = columns, take a look at Matrix (mathematics) - Wikipedia, the free encyclopedia to see more about matrices.

    If you are not dealing with square matrices, I would suggest to have the person enter in the number of columns, then the number of rows. Then set up an array "matrix[Col][Row] (or would it be matrix[Row][Col]??, I'm used to deal with matrices in MATLAB where it is A = [1,2,3;4,5,6;7,8,9] which yields a matrix of
    Code:
    A = 1 2 3
        4 5 6
        7 8 9

  11. #11
    Registered User
    Join Date
    Feb 2011
    Posts
    39
    I have some code but I still have a problem with it
    this is my code
    Code:
    
    for(i = 0; i <10; i++){        //10 is the maximum number of rows, since rows are not specified I'm using 10
            for(j = 0; j <cols; j++){       //cols is the number of columns entered
              scanf("%d", &matrix[i][j]);
             }
         }
    
     if(i%cols == 0)          /**********************************/
         rows = (i/cols);      *this section is to calculate the number*
       else                        *after getting columns and the number of*
          rows = (i/cols)+1; * items                                              *
                                    /***********************************/
    
        for(i=0; i<rows; i++){        //this section is just to display the matrix
          for(j = 0; j<cols; j++){
             printf("%3d", matrix[i][j]);
           }
        printf("\n");
        }

    my problem is that even though I terminate the loop to enter the elements in the matrix, the loop keeps counting up to 10. Therefore the number of rows is wrong. This gives me an output of 2 extra rows filled with zeros like this.
    Code:
     
      1  2  3
      4  5  6
      0  0  0
    How can I stop the loop from going all the way to 10 if I don't want it to go all the way to 10?

    i tried putting a break but that didn't make it. This is what I used
    Code:
    for(i = 0; i <10; i++){
            for(j = 0; j <cols; j++){
              scanf("%d", &matrix[i][j]);
              if(scanf("%d", &matrix[i][j]) == x)  'x' is character variable I declared to use as a break
                break;
                  }
            }

  12. #12
    Registered User
    Join Date
    Feb 2011
    Posts
    39
    I have some code but I still have a problem with it
    this is the part of my code for entering the numbers and calculating the rows
    Code:
    
    for(i = 0; i <10; i++){        //10 is the maximum number of rows, since rows are not specified I'm using 10
            for(j = 0; j <cols; j++){       //cols is the number of columns entered
              scanf("%d", &matrix[i][j]);
             }
         }
    
     if(i%cols == 0)          /**********************************/
         rows = (i/cols);      *this section is to calculate the number*
       else                        *after getting columns and the number of*
          rows = (i/cols)+1; * items                                              *
                                    /***********************************/
    
        for(i=0; i<rows; i++){        //this section is just to display the matrix
          for(j = 0; j<cols; j++){
             printf("%3d", matrix[i][j]);
           }
        printf("\n");
        }

    my problem is that even though I terminate the loop to enter the elements in the matrix, the loop keeps counting up to 10. Therefore the number of rows is wrong. This gives me an output of 2 extra rows filled with zeros. For example I enter 3 columns, then I enter 6 elements it gives me an output like this
    Code:
     
      1  2  3
      4  5  6
      0  0  0
    How can I stop the loop from going all the way to 10 if I don't want it to go all the way to 10?

    i tried putting a break but that didn't make it. This is what I used
    Code:
    for(i = 0; i <10; i++){
            for(j = 0; j <cols; j++){
              scanf("%d", &matrix[i][j]);
              if(scanf("%d", &matrix[i][j]) == x)  'x' is character variable I declared to use as a break
                break;
                  }
            }
    I put an printf statement for my i and j in the loop, and i goes from 0 to 9, which shouldn't be if I terminate the loop. Or maybe I'm just doing it wrong

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to read columns and rows
    By university in forum C Programming
    Replies: 1
    Last Post: 12-05-2010, 11:09 AM
  2. sum rows & columns in array
    By ronenk in forum C Programming
    Replies: 7
    Last Post: 06-20-2004, 04:16 AM
  3. calculation from rows and columns
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 07-08-2002, 07:44 PM
  4. Adding Columns and Rows..Help!
    By ProgrammingDlux in forum C++ Programming
    Replies: 3
    Last Post: 04-25-2002, 03:52 PM
  5. sizeof rows (not columns)
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 09-18-2001, 04:45 AM

Tags for this Thread


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