Thread: simplify this?

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    6

    simplify this?

    Code:
    #include <stdio.h>
    
    void main()
    {
    	int matrix[100][100], n, k, l, bord, bord1, bord2, bord3, bord4;
    
    	do{
    		printf("Enter order of matrix between 1 and 100 \n");
    		scanf("%d",&n);
    	}while(n<1 || n>100);
    
    	for(k=0;k<n;k++){
    		printf("Enter values for Row %d\n",k);
    		for(l=0;l<n;l++){
    			printf("Enter values for row %d col %d\n",k,l);
    			scanf("%d",&matrix[k][l]);
    		}
    	}
    	bord=0;
    	for(k=0;k<1;k++){
    		for(l=0;l<n;++l){
    			bord=bord+matrix[k][l];
    		}
    	}
    	bord1=0;
    	for(k=n-1;k<n;k++){
    		for(l=0;l<n;++l){
    			bord1=bord1+matrix[k][l];
    		}
    	}
    	bord2=0;
    	for(l=0;l<1;l++){
    		for(k=0;k<n;k++){
    			bord2=bord2+matrix[k][l];
    		}
    	}
    	bord3=0;
    	for(l=n-1;l<n;l++){
    		for(k=0;k<n;k++){
    			bord3=bord3+matrix[k][l];
    		}
    	}
    	bord4=0;
    	bord4=bord+bord1+bord2+bord3;
    	printf("The sum of the border elements are: %d\n",bord4);
    }
    I'm supposed to make a square matrix n x n, and get the sum of all border elements. This program is what I came up with, it works well enough, I was just wondering if there was a way to use fewer iterative statements.
    Last edited by markg; 10-25-2005 at 07:05 PM. Reason: missed bord2

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simplify Fraction Help
    By CaliJoe in forum C++ Programming
    Replies: 1
    Last Post: 05-01-2009, 01:17 PM
  2. Can someone simplify this statement from this thead
    By cdalten in forum C Programming
    Replies: 1
    Last Post: 01-30-2006, 08:04 AM
  3. Noobie wanting to Expand and simplify this prog
    By bandito9111 in forum C++ Programming
    Replies: 4
    Last Post: 05-12-2003, 03:39 AM
  4. sort 2D array
    By astro_not in forum C Programming
    Replies: 2
    Last Post: 04-13-2003, 02:55 AM
  5. reverse order - arrays
    By Lillian in forum C Programming
    Replies: 12
    Last Post: 11-03-2002, 07:38 PM