Starting from:

$30

CSCI1300 Homework 4 Solved



Objectives  
●     Understand loops  

●     Understand C++ loops: while loops, for loops, do-while loops


Plan for this week  
Questions 1 - 3 are considered as recitation questions. You should be able to finish at least 3 questions during the recitation time. If you’re able to finish them earlier, then continue working on the other questions and complete the homework before the deadline.   

Questions  
Question 1: Sum even positive numbers  
Write a program that asks for the end value (as ​integer​), and computes the sum of the even numbers from 0 to the value entered, inclusive.
 

Expected output 1 (​bold​ is user input)  

Enter a positive number: 

10  
Sum: 30 
 

The file should be named as ​sumEven.cpp​. Don’t forget to head over to the code runner on Moodle and paste your solution in the answer box!

Question 2: Print ​Collatz sequence  
The ​Collatz conjecture​ defines a sequence as follows: if ​n​ is an even number, then the next value is ​n/2​. If n is odd, then the next value is ​3n+1​. The sequence is conjectured to always reach 1 regardless of the starting number.  


Write a program that takes a starting number n and prints the entire sequence, starting with n and ending with 1. Each number should be printed on a new line.


Expected output example (​bold is user input)​   

Enter a positive number: 

10 10 5 

16 








 

The file should be named as ​printCollatz.cpp​. Don’t forget to head over to the code runner on Moodle and paste your solution in the answer box!  

Question 3: Zootopia 
  

The Zootopia Police Department is recruiting new officers and has come up with an innovative equation to hire. They define hireScore​ as a weighted sum of agility​ ,​ strength​ ,​ and speed.​  

                hireScore = 1.8 * ​  agility​  + 2.16 * ​ strength ​  +​ 3.24 * speed​ 

The candidates for this hiring season are foxes, bunnies, and sloths. Chief Bogo has requested you to write a program that takes in these attributes and processes a hireScore​ for the candidate.  

The program should provide a menu to choose the anthropomorphic animal. The menu should have the following options:

1.    Fox  

2.    Bunny

3.    Sloth

4.    Quit

Once an animal is selected, the program should ask the two of the characteristics based on the animal

1.    For fox, take input for agility​             and ​     strength​          

2.    For bunny, take input for agility​      and ​     speed​   

3.    For Sloth, take input for strength​     and ​     speed​   

Note: You must prompt the user for agility, strength, and speed in​ that order,​ while also skipping ​the attribute prompt that does not apply ​ ​to the particular animal.


Write a program to compute the hireScore​ ​ based on the inputs using the weighted sum formula. When you compute the hireScore​ ,​ one of the three characteristics would be 0. The computed hireScore​ should​ be displayed on the screen. The menu will run in a loop, continually offering Bogo four options until he chooses to quit. If the choice is not between 1 - 4, then it prints “Invalid option​ ”. When Bogo select option 4, it prints “​ Good bye!​ ”​


Expected output (​bold​ is user input)  

Select a numerical option: 

=== menu === 

1.  Fox 

2.  Bunny 

3.  Sloth 

4.  Quit 



Enter agility: 

10.5 

Enter strength: 20.1 

Hire Score: 62.316 

Select a numerical option: 

=== menu === 

1.  Fox 

2.  Bunny 

3.  Sloth 

4.  Quit 



Enter agility: 

10.1 

Enter speed: 

30.0 

Hire Score: 115.38 

Select a numerical option: 

=== menu === 

1.  Fox 

2.  Bunny 

3.  Sloth 

4.  Quit 

10 

Invalid option 

Select a numerical option: 

=== menu === 

1.  Fox 

2.  Bunny 

3.  Sloth 

4.  Quit 



Good bye! 
 

The file should be named as ​zootopia.cpp​. Don’t forget to head over to the code runner on Moodle and paste your solution in the answer box!

Question 4: Dream house
You have graduated from CU Boulder and have a great job! You moved to Seattle and decided to save money to buy a house. Write a program to determine how many months it will take you to save enough money to make the down payments given the following assumptions:  

●     The portion of the cost needed for a down payment is 25% (0.25)  

●     Since you just graduated, the current saving is 0

●     Assume you invest your current savings wisely. At the end of each month, you receive additional funding, ​currentSaving​ * r / 12 where r = 0.04. (r is an annual rate, so we divided it by 12)  

●     At the end of each month, your savings will increase by the return of the investment and the portion of the salary.   

Your program asks:  

1.    The starting annual salary (in ​double​)

2.    The portion of the salary you save each month for a down payment (in ​double​) 3. The cost of your dream house (in ​double​)


Expected output 1 (​bold​ is user input)  

Enter your annual salary:  

120000 

Enter the percent of your salary to save, as a decimal:  

.10 
Enter the cost of your dream home:  

1000000 
Number of months: 183 
 

The file should be named as ​dreamHouse.cpp​. Don’t forget to head over to the code runner on Moodle and paste your solution in the answer box!  


Question 5: Count matches
A ​substring​ refers to a string that is a continuous segment of a larger string. The list of all substrings of the string, “​apple​”, would be:  

●     "​apple​",  

●     "​appl​", "​pple​",  

●     "​app​", "​ppl​", "​ple​",  

●     "​ap​", "​pp​", "​pl​", "​le​",  

●     "​a​", "​p​", “​p​”, "​l​", "​e​"

●     ""

 

Write a program that asks for two strings: a string where the substring is searched and substring whose occurrences is to be found. Then, it displays the number of matches.

Expected output 1 (bold​  is user input)​   

Enter the search string:  

mississippi 

Enter the substring to be searched: 

si 
Number of occurrences: 2 
 

Expected output 2 (bold​  is user input)​   

Enter the search string:  

mississippi 

Enter the substring to be searched: 

ipp 
Number of occurrences: 1 
 

The file should be named as countMatches.cpp​  . Don’t forget to head over to the code​               runner on Moodle and paste your solution in the answer box!  

 

Question 6(15pt): Print an alphabetical triangle  
Write a program that takes the height of the triangle, then it prints the triangle like below.  

 

Expected output 1 (bold​  is user input)​   

Enter the height: 

4 abcd efg hi j 
 

Expected output 2 (bold​  is user input)​   

Enter the height: 

10 abcdefghij klmnopqrs tuvwxyza bcdefgh ijklmn opqrs tuvw xyz ab c 
 

The file should be named as printTriangle.cpp​               . Don’t forget to head over to the code​               runner on Moodle and paste your solution in the answer box!  

 

Extra credit Question (10pt): Print diamond  
Write a program that takes the side length of the diamond, then it prints the diamond like below.  

 

Expected output 1 (bold​  is user input)​   

Enter the length: 



   * 

  *** 

 ***** 

******* 

 ***** 

  *** 

   * 
 

Expected output 2 (bold​  is user input)​   

Enter the length: 



 * 

*** 

 * 
 

More products