1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
ICE Princess25 [194]
3 years ago
15

Pascal's Triangle is a triangular array in which every number represents the

Computers and Technology
1 answer:
Viefleur [7K]3 years ago
6 0

Answer:

I am writing a JAVA program.

import java.util.Scanner;  // this class is used here to take input from user

import java.util.ArrayList;    // used to create dynamic arrays  

public class PascalTriangle{   // class to print nth line of pascal's triangle

/* function line() that takes the line index (line_no) as parameter and finds the nth line (find line at given index line_no) in the Pascal's Triangle */

   public static ArrayList<Integer> line(int line_no) {  

//declares ArrayList of integers

       ArrayList<Integer> current_line = new ArrayList<Integer>();  

       current_line.add(1);    // sets the element of every line to  1      

/* if conditions checks if the input n-th line is the first line which means it checks if the line to be returned is the 1st one */

       if (line_no == 0) {

       return current_line;  //returns the first line/row}  

/* the following statement produces the previous line by passing the index position (line_no-1) of that line as a parameter of line() method */    

       ArrayList<Integer> previous = line(line_no - 1);    

/* the loop has i variable which is initialized to 1 and this loop continues to execute until the value of i gets greater than or equal to the size of previous line */

       for (int i = 1; i < previous.size(); i++) {  //start of the loop

/* the following statement computes the current line based on the previous line of the Pascal triangle and this is a recursive part. This will compute the elements of the current line based on previous line and store these elements in present*/

           int present = previous.get(i - 1) + previous.get(i);  

//adds the elements computed by the above line to the current_line. this means it adds each element of present to current_line. For example if the n-th  line is 5 then it the elements at 6th row of pascals triangle are found and returned considering the topmost line the "0th" line */

           current_line.add(present); }  

       current_line.add(1);  

     return current_line; } //returns current line i.e. n-th line of pascal triangle  

   public static void main(String[] args)     {  //start of main() function body

      Scanner scan = new Scanner(System.in);  // create Scanner class object

/* prompts user to enter integer n as input to return the nth line of Pascal's triangle */

       System.out.print("Enter·line·of·Pascal's·Triangle·to·print: ");

       int n = scan.nextInt();    //scans and reads the input integer

// calls line function to return the n-th line of Pascal triangle

       ArrayList<Integer> array = line(n);  

       for (int i = 0; i < array.size(); i++) {  /* loop iterates through the array until the loop variable i gets equal to or exceeds the array size in order to display the elements of the n-th line of pascal triangle where n is integer input */

    System.out.print(array.get(i)+ "."); }    }  } //prints the elements of n-th line

/* get() method gets the elements of nth line and . is used to seperate each element with a dot. */

Explanation:

The program is well explained in the comments mentioned with each statement of the program. The program has a function line() that takes as argument the index line_no which is the nth line of Pascal's triangle whose elements are to be displayed. The program computes the line of the previous line number (index) first using recursion. After this the values of the current line is computed with the help of the previous one. This is iterated using a loop until the nth line. In the main() method user is prompted to enter integer n and then line() method is called by passing the n integer to the function to display the elements of this n-th line in pascal's triangle. loop is used to iterate through the entire array and get() method gets the elements of nth line of array and . is used to separate each element with a dot. The program and output is attached in a screenshot.

You might be interested in
Make a program that (i) asks the user for a temperature in Fahrenheit degrees and reads the number; (ii) computes the correspond
Lerok [7]
<h2>Answer:</h2><h2></h2>

//import the Scanner class to allow for user's input

import java.util.Scanner;

//Create a class declaration with appropriate name

public class FahrenheitToCelsius {

   //Begin the main method where execution starts from

   public static void main(String[] args) {

       //Create an object of the Scanner class to allow reading from standard

       // input

       Scanner input = new Scanner(System.in);

       

       //Prompt the user to enter the temperature

       System.out.println("Enter a temperature in Fahrenheit degrees");

       

       //Read the number and store in a variable of type double

       double fahrenheit = input.nextDouble();

       

       

       //Convert the temperature to Celsius

       double celsius  = (fahrenheit - 32) * (5 / 9.0);

       

       //Print out the result

       System.out.println("The temperature in Celsius is " + celsius);

       

   } //End of main method

   

}   //End of class declaration

==============================================================

<h2>Sample Output 1:</h2><h2 />

>> Enter a temperature in Fahrenheit degrees

32

>> The temperature in Celsius is 0.0

==============================================================

<h2>Sample Output 2:</h2><h2 />

>> Enter a temperature in Fahrenheit degrees

78

>> The temperature in Celsius is 25.555555555555557

===============================================================

<h2>Explanation:</h2>

The code has been written in Java and it contains comments explaining every segment of the code. Please go through the comments.

The actual lines of code are written in bold face to separate them from the comments.

Sample outputs have also been given to show how the result of the program looks like.

5 0
3 years ago
I ate five M&amp;Ms: red, green, green, red and yellow. Only these three colors are possible. I assume that p(yellow)=3p(green)
Rina8888 [55]

Answer:

Below is code written in a free CAS (WxMaxima):

The above code creates the probability of 19 or more brown in the sample of 48 for population sizes from 5*19 to 10000 in steps of 5.

Here’s a plot of that data:

The horizontal blue line is the probability for an infinite population size (or, choosing each of the 48 M&Ms with replacement, which I infer is not what you meant). It is calculated using the binomial cdf:

The red curve approaches the blue line asymptotically as the population gets larger.

At population 10000, the red curve is

.

5 0
2 years ago
If an ARQ algorithm is running over a 40-km point-to-point fiber optic link then:
Dennis_Churaev [7]

Answer and Explanation:

Given data:

Distance (D) = 40 KM

Speed of light in the fiber =Distance/ speed of light in the fiber

a) Delay (P) = Distance/ speed of light in the fiber

= (40,000 Meters/2×108 m/s)

=( 40×103 Meters/2×108 m/s)

Propagation delay (P) = 0.0002 seconds or 200 microseconds

b)

if propagation delay is 0.0002 Seconds roundup trip time (RTT) will be 0.0004 Seconds or 400 micro Seconds

Essentially since transmission times and returning ACKs are insignificant all we really need is a value slightly greater than 0.0004 seconds for our timeout value.

c)

The obvious reasons would be if the data frame was lost, or if the ACK was lost. Other possibilities include extremely slow processing on the receive side (late ACK).

Or Extremely Slow Processing of the ACK after it is received back at the send side.

4 0
3 years ago
Assume that another method has been defined that will compute and return the student's class rank (Freshman, Sophomore, etc). It
ladessa [460]

Answer:

Option (D) i.e., s1.getClassRank( ); is the correct option to the following question.

Explanation:

Here, in the following option, the object "S1" is the object of the class "rank" and "getClassRank( )" is the function of that class "rank". so, in the following code the function "getClassRank( )" is called through the class object which computes and returns the class rank of the students.

So, that's why the following option is the correct way to call the function.

8 0
3 years ago
Print("Weight on Earth?")
Lynna [10]

Answer:

weightEarth = float(input("Enter weight on earth: "))

weightMoon = weightEarth/6

print("Weight on moon:", weightMoon)

Explanation:

You have to convert the string input into a float in order to do calculations with it.

5 0
2 years ago
Other questions:
  • Find every number from 1 to n (inclusive) that is a palindrome which starts with the digit 3. Do not user a helper function.
    10·1 answer
  • Dr. Patterson’s office calls to give patient Sara Martin her test results from her most recent visit. Her husband answers the ph
    8·2 answers
  • Computers store temporary Internet files in the Recycle Bin. These files take up space and slow down a computer. Which tool can
    10·1 answer
  • While surfing online, Patricia checks her email and reads the latest messages. She then browsers a website and logs in a comment
    8·1 answer
  • Word documents contain how many sections by default?
    9·1 answer
  • Advanced Electronics has employees who use a company website to share work. By posting on the website, employees are
    7·1 answer
  • The Last Free brainliest<br><br> This Is The Last Brainliest That Im Doing Forever Sorry Guys
    8·2 answers
  • PLEASE HELP I WILL GIVE BRAINLIEST!!!!
    7·2 answers
  • Which computers accuracy is much higher(i.e.analog &amp; digital )​
    14·1 answer
  • Consider the following code snippet that appears in a subclass: public void deposit(double amount) { transactionCount ++; deposi
    14·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!