We are asked to read the top right triangle of a 2d array-I dont know if I can illustrate it but imagine the 1s and 0s to constitude the cells in the 2d array. The question asks to sum the contents of the cells that are located in 1s.
01111
00111
00011
00001
00000
I came up with the following code but it seems inconvinent to use several if statments when I could use a short cut but the thing is I dont know the short cut!
no its one past exam question(different parts of the same question which asks to write a program with various functions)-I didnt want to post the whole code (for the one question) but rather solve it bit by bit sorry if it was confusing. the 2d aray is 5*5- how can it vary? Im using the same array and since array size is a constant I didnt think that would be subject to change or am I mistaken? What we are asked to do is all based on the same array. The user enters the numbers into the array (all cells-that's what I presumed anyway)
Right. Here are some hints given under the assumption that final2D is a 5x5 array and noofrows defines the number of entries to sum (i.e., noofrows is 4 in your first example):
(1) Notice how, in row 0, you sum the last noofrows entries.
(2) Notice how, for each row you advance, you decrement the number of entries in the row to add to the sum. This means that row number + number of entries to add for that row is constant and = noofrows.
(3) Notice how you stop adding when current row number is = noofrows
Try to use this and two for-loops to write the code...