4.0k views
4 votes
Suppose that sales is a two-dimensional array of 10 rows and 7 columns wherein each component is of the type int , and sum and j are int variables.

Which of the following correctly finds the sum of the elements of the fifth row of sales?1.sum = 0;for(j = 0; j < 10; j++)sum = sum + sales[5][j];

2.sum = 0;for(j = 0; j < 7; j++)sum = sum + sales[4][j];

3.sum = 0;for(j = 0; j < 10; j++)sum = sum + sales[4][j];

4. sum = 0;for(j = 0; j < 7; j++)sum = sum + sales[5][j];

1 Answer

4 votes

Answer:

sum = 0;for(j = 0; j < 7; j++)sum = sum + sales[5][j];

Step-by-step explanation:

here we evaluate condition in parenthesis

sum = 0 here variable sum is initialised with 0 value

now for loop executes the condition in parenthesis which is (j = 0; j < 7; j++)

  • j =0 is 1st condition which will be executed one time , so j = 0 means index value for loop started is with 0 as you know arrays have index
  • j<7 2nd condition for executing the code block which defines loop will go on till the value of j is less than 7 ,as we know the columns in question is 7(this condition must be true)
  • j++ 3rd condition means after each iteration value of j will increase +1
  • here this condition is true (j = 0; j < 7; j++) till 7th column
  • sum = sum + sales[5][j] (sales [row][column] it indicates every 5th row element of each column)

User Myke Black
by
5.8k points