C101 Laboratory Session 10 Lab Goals 1 Dealing With Random N

C101 Laboratory Session 10lab Goals 1 Dealing With Random Numbers

Dealing with random numbers. More practice with functions. Write a program that has the user pick an integer, either 1, 2, or 3. The computer then picks a random number within this same range. Both numbers are passed into a function which compares them and displays whether they are the same or not. The program should then prompt the user to play again and repeat until the user decides to exit. Make sure to initialize the random seed once with srand(time(NULL)); outside the loop. The program should generate new random numbers each iteration based on user input.

Paper For Above instruction

To develop an effective program that fulfills the specified requirements, one must understand key programming concepts such as random number generation, function usage, user input handling, and loop control. This program demonstrates these concepts through a simple interactive game that compares user-chosen numbers with computer-generated random numbers, and also incorporates loan payment calculations involving mathematical functions and formatted output.

Part 1: Random Number Guessing Game

The first part of the program involves creating an interactive game where the user selects a number between 1 and 3. The program then randomly generates a number within the same range. Both numbers are passed into a dedicated function which compares them and displays whether they match or not. The game continues in a loop, prompting the user after each round whether they want to play again. Key aspects include initializing the random seed once at the start of the program with srand(time(NULL)); to ensure randomness, and generating random numbers with rand() % 3 + 1.

To implement this, the program will include a main loop that keeps prompting the user for input and generates the random number accordingly. The comparison and output will be handled through a function that takes both numbers as parameters, performs a simple equality check, and displays the appropriate message. This structure promotes modular code, making it easy to maintain and expand.

Part 2: Loan Payment Calculator

The second part involves creating an application that computes monthly loan payments based on user inputs. The program must collect the principal amount, annual interest rate (as a percentage), loan duration in years, and number of payments per year. Using these inputs, the program calculates the monthly payment using the formula provided, which involves compound interest calculations using exponential functions.

Key functions include:

  • Read_Loan_Info: Reads and stores user inputs.
  • MonthlyPayment: Calculates the monthly payment based on inputs and the loan formula.
  • Show_Table: Displays a formatted table with the principal, interest rate, years, number of payments, and the resulting monthly payment.

In implementing the calculation, the program must convert the annual interest rate percentage to a decimal, divide by 12 to get monthly interest rate, and compute the total number of payments. The exponential component involves raising (1 + monthly interest rate) to the power of total payments, which can be performed with pow() from . Special care must be taken to ensure floating-point division and to format numerical outputs to two decimal places with iomanip.

Program Mechanics and Best Practices

The program architecture should emphasize modularity and clarity. Use header includes <iomanip> for output formatting, <math.h> for the exponentiation function, and <ctime> for seeding randomness. Place srand(time(NULL)); outside of loops to avoid reseeding issues. Use descriptive variable names and clear prompts for inputs.

The loan payment computation must correctly handle input percentages, converting them into decimal form before calculation. The output should strictly follow the specified formatting: two decimal points for floats, with dollar signs and percentage symbols where applicable, and no scientific notation. Test cases provided include a range of principal amounts, interest rates, and loan durations to validate accuracy and formatting.

Conclusion

This program combines fundamental programming concepts such as user input, random number generation, function calls, mathematical calculations, and formatted output. By modularizing the code through specific functions for input, calculation, and display, it demonstrates good programming practice. The dual functionality showcases both interactive game development and financial computation, making the program versatile and instructive for learners.

References

  • Deitel, P. J., & Deitel, H. M. (2017). C++ How to Program (10th ed.). Pearson.
  • Stroustrup, B. (2013). The C++ Programming Language (4th ed.). Addison-Wesley.
  • Lippman, S. B., Lajoie, J., & Moo, N. S. (2012). C++ Primer (5th Edition). Addison-Wesley.
  • Gamma, E., Helm, R., Johnson, R., & Vlissides, J. (1994). Design Patterns: Elements of Reusable Object-Oriented Software. Addison-Wesley.
  • ISO/IEC. (2020). ISO/IEC 14882:2020 - Programming Languages — C++.
  • MIT OpenCourseWare. (n.d.). C++ Programming Tutorials. https://ocw.mit.edu
  • GeeksforGeeks. (2022). Random Number Generation in C++. https://www.geeksforgeeks.org
  • Cplusplus.com. (n.d.). cmath library documentation. https://www.cplusplus.com/reference/cmath/
  • cppreference.com. (2023). rand, srand, and pow functions. https://en.cppreference.com
  • Programiz. (n.d.). C++ Input Output Formatting. https://www.programiz.com/cpp-programming/formatting-output