C++ Programs for Various Calculations
C++ Programs for Various Calculations
A program determining student grades based on score ranges uses a switch-case structure within certain boundaries. Input scores are matched to specific ranges, leading to the following grading: Below 25 – F, 25 to 45 – E, 45 to 50 – D, 50 to 60 – C, 60 to 80 – B, Above 80 – A. Specific conditions in switch cases efficiently map scores to assigned grades .
Conditional structures in C++ can efficiently guide activity suggestions based on temperature. Using a series of 'if', 'else if', and 'else' statements, the program evaluates the input temperature and advises activities like swimming if the temperature is 80°F or higher, tennis if it's between 60°F and 79°F, golf for 40°F to 59°F, and skiing if below 40°F. This demonstrates the use of branching in decision-making processes .
In C++, the program calculating falling distance applies the physics law d = 0.5 * g * t^2, where g is the acceleration due to gravity (9.8 m/s²). The user inputs time in seconds, and the formula computes and outputs the distance. By using concepts of physics, this program exemplifies real-world application of the laws of motion in computer science .
The program checks if a purchase qualifies for a discount by calculating the cost as unit price times quantity. If the total exceeds 1000 currency units, a 10% discount is applied. Conditional 'if-else' logic is used to decide in real-time whether the final cost will be reduced, ultimately outputting the discounted or full price based on the total cost .
Performance-based salary adjustments in C++ are determined by first obtaining the current annual salary and a performance rating input from the user. The switch statement is used to apply different raise percentages: a 6% raise for excellent performance (rating 1), a 4% raise for good performance (rating 2), and a 1.5% raise for poor performance (rating 3). The resulting new salary is calculated using these percentages and displayed to the user .
To determine the volume of a globe in C++, the formula (4.0/3.0) * π * r^3 is used, where π (pi) is approximately 3.14, and r is the radius of the globe. The program prompts the user to input the radius, calculates the volume using the stated formula, and outputs the result. The 'pow' function from the <cmath> library can be used to compute the cube of the radius .
In the C++ exam program, a student’s pass/fail status is determined by their score percentage, where 50% or more means pass. A switch statement assigns different letter grades based on higher score brackets. For failing scores below 50%, the program advises 'Resit' for scores between 33-49% or 'Redo course' for less than 33%. This program uses conditional logic to guide educational improvements .
Dynamic memory allocation in C++ can be demonstrated by using the 'new' operator to create an array of integers. In the program, memory for an array of integers is allocated dynamically, values are assigned, the array is printed, and finally the memory is deallocated using 'delete[]'. This ensures efficient memory usage, especially for variable-size data .
BMI (Body Mass Index) is calculated programmatically using the formula BMI = weight / height^2, where weight is in kilograms and height is in meters. This computation requires taking inputs of weight and height, squaring the height using the 'pow' function, and dividing the weight by this squared value. The calculated BMI helps in determining the intensity of training required for a person based on their body composition .
Compensation pay adjustments for employees in C++ are computed by first reading an employee's previous annual salary, then calculating a compensation increase at a fixed rate of 11.5%. The formula total_comp_pay = comp_pay / 2.0 is used to determine the final compensation pay over six months. The new annual salary and monthly salaries are adjusted accordingly by adding the total compensation pay to the old salary .