Question

Using C++ : A Pascals triangle row is constructed by looking at the previous row and...

Using C++ :

A Pascals triangle row is constructed by looking at the previous row and adding the numbers to its left and right to arrive at the new value. If either the number to its left/right is not present, substitute a zero in it's place.

Input:

Your program should read lines from standard input. Each line contains a positive integer which indicates the depth of the triangle (1 based).

Output:

Print out the resulting pascal triangle up to the requested depth in row major form.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

This problem is all about creating pascal triangle program using c++

pascal row can be created as:

     for example:

                               1 2 1 will be the previous row,the next row will be calculated as:

                               1 (1+2) (2+1) 1

the executable code for the pascal triangle is :

  

using namespace std;
#include <iostream>
void print_pascal_triangle(int n);//function declaration
int main()
{
  
    int depth;
    cout<<"enter the depth of the pascal triangle:";
    cin>>depth; /*taking input from the user.How much depth he wants to print.*/
    print_pascal_triangle(depth); /*calling printing_pascal_triangle with argument depth(nothing but no of rows)*/
    return 0;
}
void print_pascal_triangle(int n)
{
    int p_triangle[n][n];/* creating a two dimensional array with size n*n */
    /*In pascal triangle first row has one element.2nd row has 2 elements ..nth row has n elements*/
    for (int row = 0; row < n; row++)
    {
        for (int col = 0; col <= row; col++)
        {
        if (row == col || col == 0) /* every row first and last element will be 1 */
            p_triangle[row][col] = 1;
        else /* For the remaining elements we need to add the element above the current element and element left to that above element*/
            p_triangle[row][col] = p_triangle[row - 1][col - 1] + p_triangle[row - 1][col];
            cout << p_triangle[row][col] << " ";//printing in the same interation
        }
        cout << "\n";
    }
}

sample output for the given code is:

     

        thank you...

Add a comment
Know the answer?
Add Answer to:
Using C++ : A Pascals triangle row is constructed by looking at the previous row and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Things are tense on the battlefield and our army is on the verge of defeat. The only chance is to...

    Programming language is Java 8 Thank you Things are tense on the battlefield and our army is on the verge of defeat. The only chance is to retreat for now, regroup, and then initiate another attack. To do this however, the king must be rescued first. A brave soldier named S wants to rescue the king. S wants to get to the king as fast as possible while avoiding the obstacles on the field such as pits, trees, bodies, etc....

  • Write a C program to compute average grades for a course. The course records are in...

    Write a C program to compute average grades for a course. The course records are in a single file and are organized according to the following format: Each line contains a student’s first name, then one space, then the student’s last name, then one space, then some number of quiz scores that, if they exist, are separated by one space. Each student will have zero to ten scores, and each score is an integer not greater than 100. Your program...

  • PLEASE COMPLETE IN C++ LANGUAGE Location row : int = -1 col : int = -1...

    PLEASE COMPLETE IN C++ LANGUAGE Location row : int = -1 col : int = -1 setLocation(row : int, col : int) getRow(): int getColl): int isEmpty(): bool Details Write all the code necessary to implement the Location class as shown in the UML Class Diagram to the right. Do this in a project named snake (we'll continue adding files to this project as the term progresses). Your class definition and implementation should be in separate files. When complete, you...

  • The goal is to create a code for implementing a Columns game using pygame Your program...

    The goal is to create a code for implementing a Columns game using pygame Your program will read its input via the Python shell (i.e., using the built-in input() function), printing no prompts to a user with no extraneous output other than precisely what is specified below. The intent here is not to write a user-friendly user interface; what you're actually doing is building a tool for testing your game mechanics, which we'll then be using to automatically test them....

  • Make a program using Java that asks the user to input an integer "size". That integer...

    Make a program using Java that asks the user to input an integer "size". That integer makes and prints out an evenly spaced, size by size 2D array (ex: 7 should make an index of 0-6 for col and rows). The array must be filled with random positive integers less than 100. Then, using recursion, find a "peak" and print out its number and location. (A peak is basically a number that is bigger than all of its "neighbors" (above,...

  • Python 2.7.14 Programming Assignment Shape Drawing With Notepad++(PLEASE READ AND FOLLOW THESE INSTRUCTIONS THOROUGLY AS THIS...

    Python 2.7.14 Programming Assignment Shape Drawing With Notepad++(PLEASE READ AND FOLLOW THESE INSTRUCTIONS THOROUGLY AS THIS ASSIGNMENT IS FOR PYTHON 2.7.14. ONLY AND I REALLY NEED THIS TO WORK!!! ALSO PLEASE HAVE THE CODE PROPERLY INDENTED, WITH WHATEVER VARIABLES DEFINED, WHATEVER IT TAKES TO WORK FOR PYTHON 2.7.14. I feel like nothing I do is working and I'm trying everything I can think of. ): In this assignment, the student will create a Python script that implements a series of...

  • PROGRAM DESCRIPTION Using the given class definitions for either C++, create a minimum heap that stores...

    PROGRAM DESCRIPTION Using the given class definitions for either C++, create a minimum heap that stores integers and and implements a minimum priority queue. (Your program can be "hard coded" for integers - it does not need to use templates, generics, or polymorphism.) Your data structure must always store its internal data as a heap. Your toString function should return a string with the heap values as a comma separated list, also including the size of the heap as well....

  • C language huffman This exercise will familiarize you with linked lists, which you will need for...

    C language huffman This exercise will familiarize you with linked lists, which you will need for a subsequent programming Getting Started assignment Overview Requirements Getting Started Submit Start by getting the files. Type 264get hw13 and then cd hw13 from bash. Pre-tester You will get the following files: Q&A Updates 1. huffman.h: An empty header file, you have to define your own functions in this homework. 2. huffman.c: An empty c file, you have to define your own functions in...

  • Using C programming

    Using C, create a data file with the first number being an integer. The value of that integer will be the number of further integers which follow it in the file. Write the code to read the first number into the integer variable how_many.Please help me with the file :((This comes from this question:Write the code to dynamically allocate ONE integer variable using calloc (contiguous allocation) or malloc (memory allocation) and have it pointed to by a pointer (of type int...

  • Hello Guys. I need help with this its in java In this project you will implement...

    Hello Guys. I need help with this its in java In this project you will implement a Java program that will print several shapes and patterns according to uses input. This program will allow the use to select the type (say, rectangle, triangle, or diamond), the size and the fill character for a shape. All operations will be performed based on the user input which will respond to a dynamic menu that will be presented. Specifically, the menu will guide...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT