Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

Program No.

-
Question
Write a JAVA program to create a two-dimensional array called ‘tank’ of size r×c,
where r is row and c is column value. Array tank holds water after walls are built in it.
Array tank can only have vertical walls.
Output of the program:
1. Print the tank.
2. How many units of water the tank holds?
Process of input in tank array:
Ask the user how many blocks of wall each column will have, insert those blocks from
the bottom of each column.
EXAMPLE 1:
A tank array of size 5x7 is shown below: tank-1

0 0 0 0 0 0 0
1 0 0 0 0 0 0
1 x x x x 1 0
1 x 1 1 x 1 0
1 x 1 1 x 1 0

This tank array has four vertical walls at column 1, 3, 4 and 6. ‘X’ is a unit of water
stored in the tank. It holds 8 units of water.
Algorithm:
Step 1: Begin
Step 2:[Creating objects and initializing variables]
Creating Scanner class object to accept data.
Integer variables r and c are initialized . A character array a is also initialized. An
integer array brick is initialized
r user input
c user input
a[ ][ ]r,c
brickc

Step 3: [Storing the elements of the array]

For(i0 to c)

{
nbsc.nextInt();

brick[i] nb;

For(jr-1 to r-nb)

a[j][i] '1'; //Storing 1 in places of bricks

jj-1;

}ii+1;

Step 4:[Storing the position of water as X]

For (i0 to c)

If(brick[i]>0)

For(ji+1 to c)

If(brick[j]>0)

If(brick[i]<brick[j])

For (ki+1 to <j)

For(lr-1 to r-brick[i])

If(a[l][k] ! '1')

{
a[l][k] 'X';

}ll+1;

}kk+1

Else If(brick[i]>=brick[j])

For(ki+1 to <j)

For(lr-1 to r-brick[j])

If(a[l][k]! '1')

a[l][k] 'X';

}ll+1;

}kk+1;

} } } jj+1; } }

ii+1;}

Step 5:[Storing the positions without water or bricks as 0]


For(i0 to r )
{
For(j0 to <c )
{
If((a[i][j]! '1')&&(a[i][j]! 'X'))
Then a[i][j]='0';
jj+1; }
ii+1; }
Step 6:[Printing the tank]

For(i0 to <r)

For(j0 to c)

Print: (a[i][j]+" ")

jj+1;

}Print : “ ”

ii+I;

}
Step 7: End

Source Code:
import java.util.*;

class Tank

public static void main()

int i,j,k,l,r,c,nb;

Scanner sc=new Scanner(System.in);

System.out.println("Enter the number of rows");

r=sc.nextInt();

System.out.println("Enter the number of column");

c=sc.nextInt();

char a[][]=new char[r][c];

int brick[]=new int[c];

for(i=0;i<c;i++)
{

System.out.println("Enter the number of bricks in


column"+ (i+1));

nb=sc.nextInt(); //Taking input of the number


of bricks

brick[i]=nb;

for(j=r-1;j>=r-nb;j--)

a[j][i]='1'; //Storing 1 in places of bricks

for(i=0;i<c;i++)

if(brick[i]>0)

for(j=i+1;j<c;j++)

if(brick[j]>0)

if(brick[i]<brick[j])

for(k=i+1;k<j;k++)

for(l=r-1;l>=r-brick[i];l--)

if(a[l][k]!='1')

{
a[l][k]='X'; //Storing the
water as X

else if(brick[i]>=brick[j])

for(k=i+1;k<j;k++)

for(l=r-1;l>=r-brick[j];l--)

if(a[l][k]!='1')

a[l][k]='X'; //Storing the


water as X

for(i=0;i<r;i++)

for(j=0;j<c;j++)

{
if((a[i][j]!='1')&&(a[i][j]!='X'))

a[i][j]='0';

for(i=0;i<r;i++)

for(j=0;j<c;j++)

System.out.print(a[i][j]+" "); //Printing the


tank

System.out.println();

Variable description
Variable Data Type Purpose

i integer Iteration

j integer Iteration

k integer Iteration

l integer Iteration

r integer Stores the number


of rows
c integer Stores the number
of columns

nb integer Stores the


number of
bricks

Method Description
Methods Description
main() Comprises the program, to input the
number of rows, columns and bricks
and print a tank
nextInt() Accepts integer from the user
Scanner(...) Parameterised Constructor

Class Description
Class Description
Tank User Defined Class to , to print a tank

Scanner To create an object of Scanner class


Output:

You might also like