Q1.
Highest temperature in 2D array
class TempHighest
{
public static void main(String args[])
{
int TEMP[][] = {
{30, 32, 31},
{28, 29, 35},
{33, 34, 36}
};
int MAXTEMP = TEMP[0][0]; // assume first value is highest
for(int i = 0; i < [Link]; i++)
{
for(int j = 0; j < TEMP[i].length; j++)
{
if(TEMP[i][j] > MAXTEMP)
{
MAXTEMP = TEMP[i][j];
}
}
}
[Link]("Highest temperature = " + MAXTEMP);
}
}
Q2
a) Why 2D array is suitable
A 2D array is suitable because it stores data in rows and columns.
Each row can represent a student, and each column can represent a term, allowing organised
storage and easy access to each student’s fee data.
b) Total fees paid by each student
class FeesTotal
{
public static void main(String args[])
{
int FEES[][] = {
{5000, 5000, 5000},
{6000, 6000, 6000},
{5500, 5500, 5500}
};
for(int i = 0; i < [Link]; i++)
{
int total = 0;
for(int j = 0; j < FEES[i].length; j++)
{
total = total + FEES[i][j];
}
[Link]("Total fees of student " + (i+1) + " = " + total);
}
}
}
Q3
a) One advantage of using 2D array
A 2D array allows seats to be represented in a row and column format, similar to a real
cinema layout, making it easy to track and access seat availability.
b) Count available seats
class SeatCount
{
public static void main(String args[])
{
int SEATS[][] = {
{1,0,0,1},
{0,1,0,0},
{1,0,1,0}
};
int count = 0;
for(int i = 0; i < [Link]; i++)
{
for(int j = 0; j < SEATS[i].length; j++)
{
if(SEATS[i][j] == 0)
{
count++;
}
}
}
[Link]("Available seats = " + count);
}
}