Thread: scan recusivly in to 2D array

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    8

    Smile scan recusivly in to 2D array

    hello!
    i'm trying to write a code which get variable as int and put them at 2D array...
    this a code which i wrote, it is the first time i'm trying to do thing like that.
    very importent it will be Recursion and without any loops.
    thanks!

    Code:
     void scanMatrix(int matrix[][18],int indexRow, int indexCol, int sizeRow, int sizeCol){
    
    	int num;
    
    	if(indexCol == sizeCol && indexRow == sizeRow) return;
    
    	matrix[indexRow][indexCol]=scanf("%d",&num);
    
    	if(indexCol != sizeCol)
    		return scanMatrix(matrix,indexRow,indexCol+1,sizeRow,sizeCol);
    
    	if(indexCol == sizeCol)
    		return scanMatrix(matrix,indexRow+1,indexCol,sizeRow,sizeCol);
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > matrix[indexRow][indexCol]=scanf("%d",&num);
    What exactly are you expecting to happen here?

    scanf is going to return -1 (aka EOF), 0 or 1.
    The successful input in num is just thrown away.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-05-2012, 10:35 AM
  2. scan 2 d array for match using strcmp
    By elipse in forum C++ Programming
    Replies: 3
    Last Post: 09-19-2011, 01:38 AM
  3. Replies: 3
    Last Post: 11-01-2010, 08:22 PM
  4. how to scan an int dinamic 2d array..
    By transgalactic2 in forum C Programming
    Replies: 21
    Last Post: 07-01-2009, 04:04 PM
  5. scan multiple items in multidimensional array
    By requiem in forum C Programming
    Replies: 1
    Last Post: 04-17-2003, 03:02 PM

Tags for this Thread