Thread: Odd results on user input array

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    24

    Odd results on user input array

    More of less on a whim I decided to write a program to convert matrices into row echelon form. To start off, I wrote a program that took numbers from the user and wrote them into a matrix, then display that matrix.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
    	int c;
    	int i, j;
    	int nr_row, nr_column;
    
    
    	float mat[nr_row][nr_column];
    
    
    	printf("Number of rows: ");
    	scanf(" %i", &nr_row);
    	printf("\nNumber of columns: ");
    	scanf(" %i", &nr_column);
    
    	printf("\nInput matrix elements:");
    	for (i = 0; i < nr_row; i++){
    		for (j = 0; j < nr_column; j++)
    		{
    			printf("\nInput element [%d][%d]: ", i, j);
    			scanf("%f", &mat[i][j]);
    		}
    	}
    
    	printf("===========================================");
    	printf("\nMatrix entered-\n");
    
    	for (i = 0; i < nr_row; i++)
    	{
    		printf("\n");
    		for (j = 0; j < nr_column; j++)
    		{
    			printf("%7.2f", mat[i][j]);
    		}
    	}
    	
    	return 0;
    }
    However, somewhere along the line the values are changed somehow-

    Code:
    Number of rows: 4
    
    Number of columns: 2
    
    Input matrix elements:
    Input element [0][0]: 1
    
    Input element [0][1]: 5
    
    Input element [1][0]: 54
    
    Input element [1][1]: 88
    
    Input element [2][0]: 12
    
    Input element [2][1]: 48
    
    Input element [3][0]: 13
    
    Input element [3][1]: 52
    ===========================================
    Matrix entered-
    
       1.00  54.00
      54.00  12.00
      12.00  13.00
      13.00  52.00
    Another run-
    Code:
    Number of rows: 3
    
    Number of columns: 3
    
    Input matrix elements:
    Input element [0][0]: 1
    
    Input element [0][1]: 8
    
    Input element [0][2]: 4
    
    Input element [1][0]: 56
    
    Input element [1][1]: 12
    
    Input element [1][2]: 3
    
    Input element [2][0]: 45
    
    Input element [2][1]: 78
    
    Input element [2][2]: 9
    ===========================================
    Matrix entered-
    
       1.00  56.00  45.00
      56.00  45.00  78.00
      45.00  78.00   9.00
    Last edited by deadrabbit; 08-15-2011 at 07:35 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    	int nr_row, nr_column;
    
    
    	float mat[nr_row][nr_column];
    You have to know the values of nr_row and nr_column before you declare mat.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
    	int nr_row, nr_column;
    
    
    	float mat[nr_row][nr_column];
    
    
    	printf("Number of rows: ");
    	scanf(" %i", &nr_row);
    	printf("\nNumber of columns: ");
    	scanf(" %i", &nr_column);
    Look closely... that the time you are creating the array, you cannot possibly know the number of rows or columns, in fact nr_row and nr_column are used in a totally uninitialized state... you input their values *after* creating the array.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Oh my, there's that crystal ball working over time again... another double header... Hi Quzah...

  5. #5
    Registered User
    Join Date
    Aug 2011
    Posts
    24
    Thanks! That fixed things up.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array size base on user input
    By icoigo in forum C Programming
    Replies: 2
    Last Post: 10-27-2010, 12:28 PM
  2. help needed with array manipulation through user input
    By priorityversion in forum C++ Programming
    Replies: 5
    Last Post: 02-02-2008, 11:48 PM
  3. User input for array size.
    By coolmoniker in forum C++ Programming
    Replies: 27
    Last Post: 08-24-2006, 11:34 PM
  4. array and user input
    By enlinux in forum C Programming
    Replies: 2
    Last Post: 07-20-2003, 02:08 AM
  5. How to let the user input values for an array...
    By TerranAce007 in forum C++ Programming
    Replies: 2
    Last Post: 08-24-2002, 03:54 AM

Tags for this Thread