Download lab 5

yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
CSIT CSC111Fall 2021/2022
CSC111: Fundamentals of Programming
Lab 5
1. What is the objective of the code below? In other words, what does this code do?
#include <iostream>
int main()
// get a integer from the user
int number;
std::cout << "Enter a number: ";
std::cin >> number;
int new_number = 0;
while (number != 0) {
new_number = new_number * 10;
new_number = new_number + number % 10;
number = number / 10;
// print the new number
std::cout << "The new number is: " << new_number << std::endl;
2. The Collatz Conjecture or 3x + 1 problem can be summarized as follows: start
with any positive integer n. Then each term is obtained from the previous term as
follows: if the previous term is even, the next term is one half of the previous
term. If the previous term is odd, the next term is 3 times the previous term plus
1. Repeat the process indefinitely. The conjecture states that no matter which
number you start with, you will always reach 1 eventually. Given a number n,
return the number of steps required to reach 1. Let the starting number be
entered from the user.
Starting with n = 12, the steps would be as follows:
1. 12
2. 6
3. 3
4. 10
5. 5
6. 16
CSIT CSC111Fall 2021/2022
7. 8
8. 4
9. 2
10. 1
Resulting in 9 steps. So for input n = 12, the return value would
be 9.
3. A mail order house sells five different products whose retail prices are product 1 — $2.98,
product 2—$4.50, product 3—$9.98, product 4—$4.49 and product 5—$6.87. Write a
program that reads a series of pairs of numbers as follows:
a) Product number
b) Quantity sold for one day
Your program should use a switch statement to help determine the retail price for each
product. Your program should calculate and display the total retail value of all products sold
last week.
4. One interesting application of computers is drawing graphs and bar charts (sometimes
called “histograms”). Write a program that reads five numbers (each between 1 and 30). For
each number read, your program should print a line containing that number of adjacent
asterisks. For example, if your program reads the number seven, it should print *******.
5. The factorial function is used frequently in probability problems. The factorial of a positive
integer n (written n! and pronounced “n factorial”) is equal to the product of the positive
integers from 1 to n. Write a program that evaluates the factorials of the integers from 1 to
5. Print the results in tabular format. What difficulty might prevent you from calculating the
factorial of 20?