Thread: entering numbers to array in a row

  1. #1
    Banned
    Join Date
    Oct 2008
    Posts
    1,535

    entering numbers to array in a row

    i need to enter numbers in a row
    and my program need to take them from right to left
    (the last number in the row will be the first one in the matrix)
    for input: 1 2 3 4 5 6 in 2X3 matrix
    6 5 4
    3 2 1
    i tried to do this but its not workin
    Code:
          for (index=rows;index>=0;index--){
    
               for (kndex=cols;kndex>=0;kndex--){
    
                 scanf("%d",&matrix[index][kndex]);
               }
    
            }

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Well you've provided very little back ground information, but I'd say that the element "rows" and "cols" is out of bounds, ie on the first iteration.

    An example of what I mean
    Code:
    #define ROWS 5
    
    char example[ROWS] = {0};
    
    for (index = ROWS; index >= 0; index--)
    {
       /* on the first iteration index = 5, however example[5] is out of bounds */
    }

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Pull off the newline char:

    Code:
    #include <stdio.h>
    
    int A[2][3];
    
    int main(void)  {
       int row, col;
    
       for(row = 1; row >= 0; row--)   {
          for(col = 2; col >= 0; col--)   {
             scanf("&#37;d", &A[row][col]);
             //getchar();  //pulls off the newline char that scanf() leaves behind
          }
       }
       for(row = 0; row < 2; row++)  {
          for(col = 0; col < 3; col++)
             printf(" %d ", A[row][col]);
          printf("\n");
       }
       getchar();  //needed because of scanf()
       getchar();  
       return 0;
    }
    Last edited by Adak; 12-19-2008 at 06:34 AM.

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Why bother? It's "whitespace".
    Last edited by zacs7; 12-19-2008 at 06:26 AM.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Because it's 4:30 in the morning!

    Somewhere I was taught to add a getchar() or similar, after every scanf(), to pull the newline
    off the keyboard buffer.

    You're right, it's no problem for scanf(), as it loops, but you do need to add another getchar() at the end, to hold the console window open.

    Thanks, Zacs7.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  2. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  3. two dimensional dynamic array?
    By ichijoji in forum C++ Programming
    Replies: 6
    Last Post: 04-14-2003, 04:27 PM
  4. the definition of a mathematical "average" or "mean"
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-03-2002, 11:15 AM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM