### Counting border elements that are zero in a matrix

Hi everyone. I have two matrices that are input by the user. My question is how would I count the number of border elements that are zero in the FIRST matrix only? Any ideas? I know I will need some for loops, etc.

Given
 ```0 0 0 0 0 0 x x x 0 0 x x x 0 0 x x x 0 0 0 0 0 0 ```

How many zeros are there? How did you count them?

Do the same thing in code. Remember to be careful not to count the same zero twice.

Make a function to do it, and use that function on only one matrix.

Hope this helps.
Okay, I've come up with an idea to do this. I'm going to add up the first and last rows and columns, then subtract the corners. I going to use an if statement making the size at least 3x3 so the matrix will have some interior. Here's the function I have so far. The first part is correct (rows), but the second part (columns) is what I'm having trouble with. How do I get a for loop to just go down instead of across? Any ideas?

 ``1234567891011121314151617181920212223242526`` ``````void borderElements (double a[][20], int s, int t) { int i, j; double firstRowSum = 0.0; double lastRowSum = 0.0; double firstColSum = 0.0; double lastColSum = 0.0; if (s>=3 && t>=3) { for (i=0; i<=t-1; i++) { firstRowSum = firstRowSum + a[0][i]; lastRowSum = lastRowSum + a[s-1][i]; } cout << firstRowSum << endl; cout << lastRowSum << endl; for (j=0; j<=s-1; j++) { firstColSum = firstColSum + a[0][j]; lastColSum = lastColSum + a[t-1][j]; } cout << firstColSum << endl; cout << lastColSum << endl; } }``````
Loop through all the rows. Each time inside the loop, add the first and last column to your total.
I'm not exactly sure what you mean. I'm not sure how to find the sum of the first and last columns. I'm sure there is some error in my second for loop, but I'm not exactly sure what it is.
For the first and last row, you need to examine every element.

 ```0 0 0 0 0 <-- all 0 x x x 0 <-- first and last 0 x x x 0 <-- first and last 0 x x x 0 <-- first and last 0 0 0 0 0 <-- all```

For all the rows in-between, you only need to examine the first and last columns.

Hope this helps.
I tried doing what you're suggesting, and came up with this. I'm sure I am really close to getting it, but can't figure it out exactly.

 ``12345678`` `````` for (i=0; i<=s-1; i++) { for (j=0; j<=t-1; j++) { firstColSum = firstColSum + a[0][s-1]; lastColSum = lastColSum + a[t-1][i]; } }``````
Last edited on
This is what I have now. I'm not sure if I'm supposed to put two elements inside brackets, but it seems logical. Still wrong output though. I could really use some help with this.

 ``123456789101112`` ``````firstColSum = 0.0; lastColSum = 0.0; for (i=0; i<=s-1; i++) { for(j=0; j<=t-1; j++) { firstColSum = firstColSum + a[0, 0][s-1, 0]; lastColSum = lastColSum + a[0, t-1][s-1, t-1]; } } cout << firstColSum << endl; cout << lastColSum << endl;``````
 ``12345678910`` ``````PSEUDOCODE sum = 0 for first row and last row: for each column: sum += is element equal to zero? for all rows except first and last: sum += is first column equal to zero? sum += is last column equal to zero?``````

What's with the commas in your array indices?

Remember: a[row][column].

Hope this helps.
How do I make a for loop for columns, and for all rows except first and last?
Don't take this too hard, but at some point you are going to have to use your brain on this. I'm not just going to give you code. (Though I've all but done that already.)

So if you know how many items there are, how do you loop through all but the first and last?
I know that, and I'm really trying. I wouldn't post anything on here unless I truly needed help because I've tried everything I know. What I was asking is, can you do a for loop with two specifications as you indicated above. I've never done that or seen, thus I don't know how. That is why I asked the question.
 ``12345678910`` `````` sum = 0.0; for(i=0; i<=t-1; i++) { for(j=0; j<=s-1; j++) { sum = sum + a[1][t-2]; } } cout << sum << endl;``````