Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

10:40 AM Wed 3 Mar . 100% CCCS224 Lab 6.pdf Practice Activity with Lab Instructor: (15 Minutes) Problem Statement (Triangle) We have triangle made of

image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
10:40 AM Wed 3 Mar . 100% CCCS224 Lab 6.pdf Practice Activity with Lab Instructor: (15 Minutes) Problem Statement (Triangle) We have triangle made of blocks. The topmost row has 1 block, the next row down has 2 blocks, the next row has 3 blocks, and so on. Compute recursively (no loops or multiplication) the total number of blocks in such a triangle with the given number of rows. For example: triangle(0) 0 triangle(1) 1 triangle(2)3 Defining Recursive Solution: Row 1 If we actually draw this triangle for rows = 4, we see that row 1 has one block, row 2 has 2 blocks, row 3 has 3 blocks and row 4 has 4 blocks and we have to add all these blocks. Row 2 Row 3 So, our recursive method will have the following formula: Row 4 triangle(rows) = rows + triangle(rows - 1) 10:40 AM Wed 3 Mar . 100% CCCS224 Lab 6.pdf 3 of 8 triangle(rows) = rows + triangle(rows - 1) Solution: 1. Create a project with the name Recursion2Project. 2. Create a package with the name "Recursion2" within this project. 3. Create a main class in this package with the name "TriangleRecursion". 4. Add the following lines of code within TriangleRecursion" main class. Lab. 6 Page 3 10:40 AM Wed 3 Mar . 100% CCCS224 Lab 6.pdf 4 of 8 #include using namespace std; int triangle(int rows) if (rows == 0) return 0; else return rows+ triangle(rows-1); int main() intr; cout>r; int SumBlocks = triangle(r): cout

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Sams Teach Yourself Beginning Databases In 24 Hours

Authors: Ryan Stephens, Ron Plew

1st Edition

067232492X, 978-0672324925

More Books

Students also viewed these Databases questions

Question

Explain the function and purpose of the Job Level Table.

Answered: 1 week ago