Border and Interior elements of a matrix

Finding the sum of the border items and the sum of the interior

Page 1 of 1

1 Replies - 14885 Views - Last Post: 27 October 2008 - 02:48 PM Rate Topic: -----

#1 wakemalibu   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 24
  • Joined: 30-September 08

Border and Interior elements of a matrix

Posted 27 October 2008 - 02:21 PM

So far this is what Ive got, The first part finds the sum of the elements in the matrix then the 2nd part finds the sum of the border elements. Now rule of thumb, if you have 2 of 3 variables you can find the third so I take the sum of the elements and subtract the border from the sum to get the interior sum. Atm my numbers are extremely off and I dont know why. The last part of each if-else subtracts the elements that the loops go over twice or at least they should. Also can you look ay my if and if else statements. They say if the the matrix is size 2*2 or less than do this and the the if else is if the matrix is greater than 2*2 do this. What am I missing in my syntax?

void elementSum (double x[][10], int m, int n)

{

   double sum_All, border_Sum, interior_Sum;
   sum_All	   = 0;
   border_Sum	= 0;
   interior_Sum  = 0;
   
   for (int i=0; i <= n-1; i++)

   {

	   for (int j=0; j <= n-1; j++)

	   {
		
		  sum_All = sum_All + x[i][j];
	
	   }

   }

   if( m <= 2 || n <= 2)

   {
		 
	  for (int j=0; j <= n-1; j++)
	 
	  {

		 border_Sum = border_Sum + x[0][j];
  
	  }
 
	  for (int j=0; j <= n-1; j++)
 
	  {
	  
		 border_Sum = border_Sum + x[n-1][j];

	  }

	  for (int i=0; i <= n-1; i++)
   
	  {
	 
		 border_Sum = border_Sum + x[i][0];
 
	  }

	  for (int i=0; i <= n-1; i++)

	  {

		 border_Sum = border_Sum + x[i][n-1];

	  }

	  border_Sum = border_Sum - x[0][n-1] - x[n-1][0] - x[0][0] - x[n-1][n-1];

   }

   else if ( m > 2 || n > 2)

   {
		 
	  for (int j=0; j <= n-1; j++)
	 
	  {

		 border_Sum = border_Sum + x[0][j];
  
	  }
 
	  for (int j=0; j <= n-1; j++)
 
	  {
	   
		 border_Sum = border_Sum + x[n-1][j];

	  }

	  for (int i=0; i <= n-1; i++)
   
	  {
	 
		 border_Sum = border_Sum + x[i][0];
 
	  }

	  for (int i=0; i <= n-1; i++)

	  {

		 border_Sum = border_Sum + x[i][n-1];

	  }

	  
	  border_Sum = border_Sum - x[0][n-1] - x[n-1][0] - x[0][0] - x[n-1][n-1];
	   
	  interior_Sum = sum_All - border_Sum;

   }

   

   
cout << "Sum   of all	  elements: " << fixed << right << sum_All	  << endl
	 << "Sum   of border   elements: " << fixed << right << border_Sum   << endl
	 << "Sum   of interior elements: " << fixed << right << interior_Sum << endl << endl;

}




Is This A Good Question/Topic? 0
  • +

Replies To: Border and Interior elements of a matrix

#2 Gloin   User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: Border and Interior elements of a matrix

Posted 27 October 2008 - 02:48 PM

You're doing calculations on an m*n matrix but when you calculate the border, in your for-loops you treat the matrix as an n*n matrix. You simply never use m as a variable. That's likely to cause errors.

Remember to change it for both cases. greater or less than 2.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1