# Border and Interior elements of a matrix

Page 1 of 1

## 1 Replies - 15091 Views - Last Post: 27 October 2008 - 02:48 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=69285&amp;s=bf578efd27dd47eab9d65d546a7263a5&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 wakemalibu

Reputation: 1
• 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

• Expert Schmexpert...

Reputation: 235
• 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.