0% found this document useful (0 votes)
5 views3 pages

Array Riyaan

The document contains Java code examples demonstrating the use of 2D arrays for different applications, including finding the highest temperature, calculating total fees paid by students, and counting available seats in a cinema layout. It explains the suitability of 2D arrays for organized data storage and access. Additionally, it highlights the advantages of using 2D arrays in various contexts.

Uploaded by

riyaanamin12
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views3 pages

Array Riyaan

The document contains Java code examples demonstrating the use of 2D arrays for different applications, including finding the highest temperature, calculating total fees paid by students, and counting available seats in a cinema layout. It explains the suitability of 2D arrays for organized data storage and access. Additionally, it highlights the advantages of using 2D arrays in various contexts.

Uploaded by

riyaanamin12
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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);


}
}

You might also like