Thread: help about 2D ARRAY...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    3

    Question help about 2D ARRAY...

    i want to ask..
    how to create 2D array by asking the user the value....


    TQVM..

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int *getrow(int length) {
    	int i, ii, ch, *row=malloc(length*sizeof(int));
    	char buffer[16];
    	
    	printf("Enter %d numbers seperated by commas\n"
    		"(max 4 digits): ", length);
    	for (i=0; i<length; i++) {
    		ii=0;
    		while ((ch=fgetc(stdin))!=',') {
    			if (ch=='\n') break;
    			buffer[ii]=ch; 	
    			ii++;
    		}
    		buffer[ii]='\0';
    		row[i]=atoi(buffer);
    	}
    	return row;
    }	
    
    void showmatrix (int *matrix[], int rows, int cols) {
    	int i, ii;
    	for (i=0; i<rows; i++) {
    		for (ii=0; ii<cols; ii++) printf("%5d", matrix[i][ii]);
    		printf("\n");
    	}
    }
    	
    
    int main(int argc, char *argv[]) {
    	int rows, cols, i, **matrix;
    	if (argc<3) {
    		printf("Usage \"%s [NO OF ROWS][NO OF COLS]\", eg %s 3 3\n",argv[0],argv[0]);
    		return 0;
    	}
    	
    	rows=atoi(argv[1]); cols=atoi(argv[2]);
    	matrix=malloc(rows*sizeof(int*));
    	for (i=0; i<rows; i++) {
    /* LEAK:	matrix[i]=malloc(cols*sizeof(int));   */
    		matrix[i]=getrow(cols);
    	}
    	showmatrix(matrix,rows,cols);
    	for (i=0; i<rows; i++) free(matrix[i]);
            free(matrix);
    	return 0;
    }
    Output:
    Code:
    [/root/C] ./a.out 2 4
    Enter 4 numbers seperated by commas
    (max 4 digits): 999,-128, 9,  666
    Enter 4 numbers seperated by commas
    (max 4 digits): 1024, 42, 0, 11
      999 -128    9  666
     1024   42    0   11
    Last edited by MK27; 02-27-2009 at 08:48 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    1
    Quote Originally Posted by MK27 View Post
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int *getrow(int length) {
    	int i, ii, ch, *row=malloc(length*sizeof(int));
    
    }
    	
    
    	for (i=0; i<rows; i++) {
    		matrix[i]=malloc(cols*sizeof(int));	
    	                matrix[i]=getrow(cols);
    	}
    matrix[i]=malloc(cols*sizeof(int));
    line is not required as memory has been allocated twice (second time in getrow). Which will result in memory leak

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by ashu0204 View Post
    matrix[i]=malloc(cols*sizeof(int));
    line is not required as memory has been allocated twice (second time in getrow). Which will result in memory leak
    Good catch -- I'll remove that from the post, for what it's worth. I also forgot to free(matrix) after freeing the rows...
    Last edited by MK27; 02-27-2009 at 08:45 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    137
    A C array is very simple, you can ask the user:
    Code:
    int value;
    int array2d[10][5];
    for(int k = 0; k < 10; k++){
      for(int i = 0; i < 5; i++){
        cout << "What is value for index i " << i << " inside the array k " << k << "?" << endl;
        cin << value;
        array2d[k][i] = value;
      }
    }
    Easy isn't it?
    ★ Inferno provides Programming Tutorials in a variety of languages. Join our Programming Forums. ★

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    execute's example is good if your 2D array is of a fixed size, but you would need to convert the example from using the C++ version of the standard input stream to the C version.

    On the other hand, it seems that previous responses interpreted "how to create 2D array by asking the user the value" as "how to create 2D array after getting the dimensions from the user".
    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. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  2. Help with mallocing a 2d array please?
    By Gatt9 in forum C Programming
    Replies: 5
    Last Post: 10-10-2008, 03:45 AM
  3. 2D array pointer?
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 04-23-2006, 08:16 AM
  4. Read file in 2D array
    By Chook in forum C Programming
    Replies: 1
    Last Post: 05-08-2005, 12:39 PM
  5. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM