whats the problem....

This is a discussion on whats the problem.... within the C Programming forums, part of the General Programming Boards category; i have written a program which should print the matrix in the following fashion. 1 2 3 4 5 16 ...

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    46

    whats the problem....

    i have written a program which should print the matrix in the following fashion.

    1 2 3 4 5
    16 17 18 19 6
    15 24 25 20 7
    14 23 22 21 8
    13 12 11 10 9


    code is :
    Code:
    #include <stdio.h>
    
    main()
    {
    	int a[10][10];
    	int i, j;
    	int r, c;
    	int count = 0, loop;
    
    	printf("enter the order of square matrix\n");
    	scanf("&#37;d%d", &r, &c);
    	
    	if(r == c) {
    		for(loop = 0; loop < (r + 1)/ 2; loop++) {		//for circular loop
    			for(i = loop; i < (c - loop); i++) {		//row loop th row
    				if(count < r * r)                               //condition for to come out of loop
    					a[loop][i] = ++count;            //assigning the counter value
    			}
    			for(i = loop + 1; i < (r - loop); i++) { //for n - loop th row
    				if(count < r * r)                            //same as above
    					a[i][r - loop - 1] = ++count;
    			}
    			for(i = (c - loop - 2); i > loop; i--) {    
    				if(count < r * r)
    					a[c - loop - 1][i] = ++count;
    			}
    			for(i = (r - loop - 1); i > loop; i--) {     
    				if(count < r * r)
    					a[i][loop] = ++count;
    			}
    			if(count == r * r)
    				break;
    		}
    		for(i = 0; i < r; i++) {
    			for(j = 0; j < c; j++) {
    				printf("%d\t", a[i][j]);
    			}
    			printf("\n");
    		}
    		
    	}
    	else
    		printf("invalid order for computation\n");
    }


    but with this code every thing is going fine till the matrix order 10 * 10. hence forth it is showing some garbage values in the matrix... some one please tell me what changes i have to make to make this program generic.....
    Last edited by vapanchamukhi; 09-05-2008 at 06:13 AM.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If you declare an array like this:
    Code:
    int a[10][10];
    then you only have 10x 10 element.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Aug 2008
    Posts
    46
    oh ............ im sorry for that....

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Why not just use a 1d array? Its a little more easy to be dynamic if you aren't using 2d arrays.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21