Thread: A problem with output of a two-dimensional table

  1. #1
    Registered User
    Join Date
    Aug 2014
    Posts
    1

    A problem with output of a two-dimensional table

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    int main(void)
    {
    	int rows , cols;
    	int a[rows][cols];
    	int i , j;
    	while(scanf("%d%d" , &rows , &cols) == 1)
    	{
    		for(i = 0 ; i < rows ; i++)
    			for(j = 0 ; j < cols ; j++)
    				a[i][j] = 2;
    		for(i = 0 ; i < rows ; i++)
    			for(j = 0 ; j < cols ; j++)
    				printf("%d " , a[i][j]);
    	}
     
    	return 0;
    }
    The above codes are expected to show a two-dimensional table with every element being '2' . I used llcwin which claims to support C99 to run. THe codes can be compiled successfully but execution comes to a failure. I don't know why. Please give me help. Thanks.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    This is wrong:
    Code:
    int rows , cols;
    int a[rows][cols];
    rows and cols were left without an initial value, yet you used them to create a variable length array.

    You should create the variable length array within the while loop instead.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Times Table with Two-Dimensional Array
    By jmb272 in forum C Programming
    Replies: 10
    Last Post: 10-02-2007, 02:22 AM
  2. Hash Table Problem.
    By penance in forum C Programming
    Replies: 9
    Last Post: 07-24-2005, 01:03 PM
  3. problem with db table
    By WaterNut in forum Windows Programming
    Replies: 5
    Last Post: 06-16-2004, 02:52 PM
  4. A Table Problem
    By kasun in forum C++ Programming
    Replies: 2
    Last Post: 01-13-2004, 11:53 AM
  5. 2D table problem
    By GaPe in forum C Programming
    Replies: 1
    Last Post: 12-10-2001, 03:52 PM