A 2D array stores values in rows and columns.
The following array
stores prices of meals for 5 days.
It has 3 rows and 5 columns.
double[][] prices
={
{4.00 , 4.50 , 5.95 , 0 , 1.95} ,
{ 0 , 12.50, 3.50 , 0
, 0
},
{5.00 , 19.95,29.50, 0 ,
10.00}
};
Apparently this person was not hungry on Thursday.
Use nested
loops to count the number of prices that equal
zero.
int count = 0;
for(int r = 0; r < 3; r =
r+1)
{
for(int c = 0;
c < 5; c =
c+1)
{
if(prices[r][c] ==
0)
{ count = count + 1;
}
}
}
output("Number of 0 prices = " + count );