Question

In: Computer Science

. . . . . . . . . . . . r . . ....

. . . . .
. . . . .
. . r . .
. . . . .   
. . . . .

Grow 1
. . . . .
. . r . .
. r r r .
. . r . .   
. . . . .

Grow 2
. . r . .
. r r r .
r r r r r
. r r r .   
. . r . .

Grow 3
. r r r .
r r r r r
r r r r r
r r r r r   
. r r r .

Grow 4
r r r r r
r r r r r
r r r r r
r r r r r   
r r r r r

Hello, I need help creating a grow function that grows the rose based on the user input. This is in java

Solutions

Expert Solution


CODE:

package rose;

import java.util.Scanner;

public class Rose
{
    //function that grows the rose
    private static void growRose(char[][] arr, int n)
    {
        //initialising the array with dosts
        for(int i=0;i<5;i++)
            for(int j=0;j<5;j++)
                arr[i][j]='.';
        //calculating start and end index, which is basically sucstracting value of (n-1) from 2 and adding 2 for end
        //rose variable stores the number of rose to start with
        int startRowIndex = 2-(n-1),endRowIndex = 2+(n-1),rose = 1;
        //if startIndex after substracting becomes less than 0, then we make it to 0, and change the number of rose
        if(startRowIndex<0)
        {
            rose+=(2*(-startRowIndex)-1);
            startRowIndex = 0;
        }
        //if endIndex becomes greater tahn 4, or goes out of size of array, then assign it to endRowindex
        if(endRowIndex >4)
            endRowIndex = 4;
        //Now we start a for loop from startRowIndex to startColIndex
        for(int i=startRowIndex;i<=endRowIndex;i++)
        {
            //we calculate startColIndex and End column index value by following formula
            int startColIndex = 2-rose/2;
            int endColIndex = 2+rose/2;
            //check if out of bound, then maki it in the bounds
            if(startColIndex<0)
                startColIndex=0;
            if(endColIndex>4)
                endColIndex=4;
            //we run for loop to change the array index to value 'r'
            for(int j=startColIndex;j<=endColIndex;j++)
                arr[i][j]='r';
            //if value of 'i' is less than 2, then we increment the number of rose by 2
            if(i<2)
                rose+=2;
            //else we decrement by 2
            else
                rose-=2;
        }      
    }
    public static void main(String[] args)
    {
        //defining the 2d-array
        char[][] arr = new char[5][5];
        Scanner scan = new Scanner(System.in);
        //taking input from user
        int n = scan.nextInt();
        //calling function to grow the rose according to user input
        growRose(arr,n);
        //prints the 2d array
        for(int i=0;i<5;i++)
        {
            for(int j=0;j<5;j++)
                System.out.print(arr[i][j]);
            System.out.println();
        }
      
    }
  
}


OUTPUT:

3
..r..
.rrr.
rrrrr
.rrr.
..r..

Ask any question if you have in comment section below.

Please rate the answer.


Related Solutions

Suppose f : R → R is measurable and g : R → R is monotone....
Suppose f : R → R is measurable and g : R → R is monotone. Prove that g ◦ f is measurable
Let R and S be rings. Denote the operations in R as +R and ·R and...
Let R and S be rings. Denote the operations in R as +R and ·R and the operations in S as +S and ·S (i) Prove that the cartesian product R × S is a ring, under componentwise addition and multiplication. (ii) Prove that R × S is a ring with identity if and only if R and S are both rings with identity. (iii) Prove that R × S is a commutative ring if and only if R and...
R is included in (R-{0} )x(R-{0} ) R = {(x,y) : xy >0} Show that R...
R is included in (R-{0} )x(R-{0} ) R = {(x,y) : xy >0} Show that R is an equivalent relation and find f its equivalent classes
Consider the following collections of subsets of R: B1 ={(a,∞):a∈R}, B2 ={(−∞,a):a∈R}, B3 ={[a,∞):a∈R}, B4 =...
Consider the following collections of subsets of R: B1 ={(a,∞):a∈R}, B2 ={(−∞,a):a∈R}, B3 ={[a,∞):a∈R}, B4 = {[a, b] : a, b ∈ R}, B5 = {[a, b] : a, b ∈ Q}, B6 ={[a,b]:a∈R,b∈Q}. (i) Show that each of these is a basis for a topology on R. (ii) What can you say about the corresponding topologies T1,...,T6, eg, are any of the topologies the same, are any comparable, are any equal to familiar topologies on R, etc?
GAGE R & R EXERCISE in this example, we do a gage R&R study on two...
GAGE R & R EXERCISE in this example, we do a gage R&R study on two data sets: one in which measurement system variation contributes little to the overall observed variation (GAGEAIAG.MTW), and one in which measurement system variation contributes a lot to the overall observed variation (GAGE2.MTW). For comparison, we analyze the data using both the ANOVA and the Xbar and R method. The GAGEAIAG data was taken from Measurement Systems Analysis Reference Manual, 3rd edition. (Chrysler, Ford, General...
a) Why would exercise influence the R-R interval? Would a change in R-R interval following a...
a) Why would exercise influence the R-R interval? Would a change in R-R interval following a short period of exercise be the same for all people, why/why not? b) Each component of the ECG waveform is associated with a particular activity during the cardiac cycle. Based on your knowledge of the actions of the heart associated with the P, Q, R, S, and T waves in the ECG, and what actions in the heart generate the lub-dub sounds, when would...
a) Why would exercise influence the R-R interval? Would a change in R-R interval following a...
a) Why would exercise influence the R-R interval? Would a change in R-R interval following a short period of exercise be the same for all people, why/why not? b) Each component of the ECG waveform is associated with a particular activity during the cardiac cycle. Based on your knowledge of the actions of the heart associated with the P, Q, R, S, and T waves in the ECG, and what actions in the heart generate the lub-dub sounds, when would...
Prove that {f(x) ∈ F(R, R) : f(0) = 0} is a subspace of F(R, R)....
Prove that {f(x) ∈ F(R, R) : f(0) = 0} is a subspace of F(R, R). Explain why {f(x) : f(0) = 1} is not.
1. Prove or disprove: if f : R → R is injective and g : R...
1. Prove or disprove: if f : R → R is injective and g : R → R is surjective then f ◦ g : R → R is bijective. 2. Suppose n and k are two positive integers. Pick a uniformly random lattice path from (0, 0) to (n, k). What is the probability that the first step is ‘up’?
Coding in R: There are two options of working in R: • install R on your...
Coding in R: There are two options of working in R: • install R on your personal computer from: https://www.r-project.org/ and you can do the assignment at home or wherever your computer is. The Seatbelts data set contains the monthly totals of car drivers in Great Britain killed or seriously injured Jan 1969 to Dec 1984. Compulsory wearing of seat belts was introduced on 31 Jan 1983. The data dictionary for the Seatbelts data set can be found here: https://stat.ethz.ch/R-manual/R-devel/library/datasets/html/UKDriverDeaths....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT