Problems

  • April 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Problems as PDF for free.

More details

  • Words: 2,134
  • Pages: 14
Chapter 2

Monthly Payments

Payment =

Rate * (1 + Rate) N *L N ((1 + Rate) − 1)

L: Loan Amount Rate: Monthly Interest Rate N: Number of Payments Sample Run Loan Amount: $ 10000 Monthly Interest: 1% Number of Payments: 36 Monthly Payment: $ 332.14 Total Amount Paid: $ 11957.15 Total Interest Paid: $ 1957.15 Quadratic Roots

Roots =

− b ± b 2 − 4ac 2a

a, b, and c are coefficients in quadratic equation of the form:

ax 2 + bx + c = 0 Sample Runs a: 4 b: 0

a: 2 b: 7.5

c: -36 Root 1: 3 Root 2: -3

c: 6.25 Root 1: -1.25 Root 2: -2.5





Temperature Conversion Write a program that converts an input value in degrees Fahrenheit to the corresponding value in degrees Centigrade – Use formula: centigrade = (Fahrenheit - 32) * 5/9



Make use of constant variables Now, try writing a conversion program to convert from Centigrade to Fahrenheit Celsius

Fahrenheit

0

?

?

40

16

?

?

75

Chapter 3

     

Painting Problem Functional Design

Get wall area from user Get cost of paint (per liter) from user Get area of paint coverage (sq. meters per liter) from user Calculate amount of paint needed Calculate cost of paint Display information

Function Prototypes

float Get_wall_area(); float Get_paint_cost(); float Get_paint_coverage();

int Calculate_paint(float area, float coverage); float Calculate_cost(int liters_paint, float unit_cost); void Display_results(float area, float coverage, float unit_cost, int liters_paint, float total_cost);

main () program

void main() { float area, coverage, unit_cost, total_cost; int liters_paint; area = Get_wall_area(); unit_cost = Get_paint_cost(); coverage = Get_paint_coverage(); liters_paint = Calculate_paint(area, coverage); total_cost = Calculate_cost(liters_paint, unit_cost); Display_results(area, coverage, unit_cost, liters_paint, total_cost);

}

Sample Run: Please enter the wall height and width in meters, then enter return: 3 10 Please enter the paint cost per liter:4 Please enter the number of square meters that can be covered with a single liter of paint: 1.5 For a wall with area 30 square meters, we require 20 liters of paint costing 4.00 dollars per liter, equals to a total cost of $80.00

Extra Problem

Write a function that takes three parameters, called first, second and third, and returns the product of the three parameters. In fact, write several different functions, one of which returns the product of three int values, another returning the product of three float values etc. Is it possible to give all these functions the same name, or does the compiler complain? Try it!

Chapter 4

Logical Expressions

Parenthesize the following logical expressions to show the order of evaluation of the operators. If a = 5, b = 10, c = 15 and d = 0 what are the truth values of the expressions? c == a+b || c == d a != 7 && c >= 6 || a+c <= 20 !(b <= 12) && a % 2 == 0 !(a >5) || c < a+b

Areas

Write a C++ program which will compute the area of a square ( side * side ) or a triangle ( (base * height) / 2 ) after prompting the user to type the first character of the figure name (t or s).

Simple calculator Write a program to implement a simple calculator. Make use of the switch statement. User enters of two integers separated by an operator. Your program should apply that operator on the operands (2 values) and display the result. Valid Operators: +, -, *, /, %, and ^ (power of) For example: If the user enters: 100 % 30, the result is: 10

Leap Year A year is a leap year if it is divisible by 4 and not by 100. If a year is divisible by 400 then the year is a leap year. Otherwise the year is not a leap year. Write a function to check whether a given year is a leap year. For example: 2000 and 1996 are leap years but 1900 is not a leap year

ValiDate! Write a program to validate a given date. Assume that year needs to be in the range: 1900 <= year <= 2100 Sample Runs: Enter a date (ex: 12 23 1969): 12 31 1999 Input date is validated. Enter a date (ex: 12 23 1969): 2 29 1946 Invalid date entered!

Increment Date Add functionality, to the date validate program, to add a single day to the date. For example: Enter a date (ex: 12 23 1969): 12 31 1999 Input date is validated. Now adding a day to the above date... New date is: 1 1 2000

Chapter 5

pow and isPrime

Write the following functions: • Given a float number x and a nonnegative integer n, use a loop to calculate: x raised to power n • Given an integer n, return true if n is prime and false otherwise. A prime number is an integer n > 1 whose only divisors are 1 and n • Write a main program for the above functions

String Reverse

Write a function that reverses and returns the given string. Hints: • string.at (i) returns the char at location i • First char in string is at location 0 • string.length () returns the length of string

Guessing Game

Write program for a game that allows a user to guess a number between 1 and 100. Your program should provide “clues” to indicate whether the user’s guess is “high” or “low” when compared to correct answer. Use rand () to generate a random number. For example: rand()%100+1 generates a number between 1 and 100 (inclusive). Steps: 1. Generate and store a random number 2. Prompt the user to enter a guess between 0 & 100 3. Validate the user entry (0<=entry<=100) 4. Test the user entry 5. If incorrect guess, Prompt user with “high/low” clues & “guess again” 6. Repeat steps 3-5 until user guess is correct 7. If correct guess, Display success and exit

Count Digits •

Write a function that counts and returns number of digits in a given a integer. Example: if 123456 is the integer, the function should return 6



Write a program, using the above and pow functions to find all n digit numbers that are equal to sum of the nth powers of their digits. Example: 153 = 13 + 53 + 33

Chapter 6 Swap Write a function to swap the values of two parameters that are passed to it. Assume any of the available data types for the parameters.

String Reversal Write a function that uses a reference parameter for inputting, a string, and returning the reversed string. Is it possible to code the above function without using a temporary string variable? Try it! Quadratic Roots Write a function to solve the following quadratic equation, given a, b and c.

ax 2 +bx + c =0 If the roots are real then the roots are returned in two parameters root1 and root2 and the function returns true, if they are complex then the function returns false. Use this formula to calculate roots:

Roots =

−b ± b 2 − 4ac 2a

Chapter 7

String-to-Number Write a function to convert a string number to a integer value. Ex: “634738” is converted in to 634738 Also, write a driver for the above function. This program should get the input from the user, store it in a string variable, clean the string to remove any non-digital characters and pass this to the above function to convert to a number. Can you modify the above program so as to handle negative and/or decimal numbers?

Password Validation Write a program that validates a password when it is created by the user. A password should meet the following criteria: • It should be at least seven characters long • It should contain at least one uppercase and at least one lowercase character. • It should contain at least one digit Can you think of any other criteria that can be used in creating “nonguessable” passwords? Modify your program to add these additional rules.

Chapter 8

Basic File I/O • •

Write a program to convert the contents of a text file to uppercase and write to a new file. Write a program to “encrypt” the contents of a text file and write to a new file. Use simple reversal of contents as the encryption technique.

Display Contacts Write a program to process contacts info data from a file to display contacts one per screen. The contacts in file are one contact per line with fields separated by commas. File Input: Gandham,Ravi,[email protected],08-26,425-847-3984 Output: Name: Ravi Gandham Email: [email protected] DOB: 12-31 Phone: 425-847-3984

Temperature Chart Write a program to display a bar chart to show the hourly temperatures of a day. Use a text file containing 24 temperature data values (assume +ve integers) separated by spaces and all on a single line. Sample output: Hour 1: ************************************ Hour 2: ******************************** Hour 3: … … … Hour 24: ***************

Chapter 9

Min, Max, Avg & 1.15Avg Write a program that calculates the minimum, maximum and average values of a given list of 10000 values. Assume that the values are stored in an array. In addition, the above program should compute the number of values that are more than 15% of the average value (find values > 1.15*Average).

Dice Rolls Write a program to roll a six faced dice 12000 times and count how many times each face is rolled. Display these frequencies. Sample output: 1s: 2044 2s: 1996 3s: 2082 4s: 2016 5s: 1946 6s: 1916

Sieve of Eratosthenes For finding all the small primes, say all those less than 10,000,000,000; one of the most efficient ways is by using the Sieve of Eratosthenes. Make a list of all the integers up to n (n > 1) and strike out the multiples of all primes less than or equal to the square root of n, then the numbers that are left are the primes. Write a program using an array to implement the above algorithm.

Load Employee Data Write a function to load “employee” array from a data file containing info about employees. Assume that are there are 80 employees. Employee info is tab separated and is as follows: 943 Walker Aaron M Aerobics Instructor 384 Merwin Adda F Greeter

Process Employees Write a program to process employees. Use the load array function from previous exercise. This program should have the following features: • • •

Search for an employee and Display info Sort employees by their id Save sorted employee info back to file

Sample Data File: Employee.txt

Chapter 10

Laser Printer Class

Create a class called Laser Printer. This class should contain following data members: • Id: Id number • Power: ON or OFF • Pages: # of pages in the printer • toner: Amount of toner in the printer (1% consumed per page printed) It should also contain following member functions in addition to constructor, accessor and mututator methods: • Print (num): prints (num) pages • Describe: Provides a description of the printer Write a driver program to test your laser printer class.

Date Class Create a class called Date. This class should contain following data members: • Day, Month, Year It should also contain following member functions in addition to constructor, accessor and mututator methods: • Display: Print the date to screen • IncDate: Increments the date • AddDays (num): Add (num) of days to the date Write a driver program to test your date class.

Chap 11

Student Scores

Given the following two-dimensional array containing stuScores, where each row of the array represents a student and each column represents a grade on one of the four exams: int stuScores[3][4] = {{63,80,74,78}, {81,99,87,78}, {76,82,83,91}}; Write C++ functions do the following operations on the above array: • maxGrade determines the highest grade among all grades. • minGrade determines the lowest grade among all grades. • avgGrade determines the average grade for a given student.

Date Class: Overloaded Operators Implement the following overloaded operators for the date class developed in previous chapter. ++: Increments the date by a day + : Add (num) of days to the date Write a driver program to test the overloaded operators.

Rational Number Class Define a class that represents rational number (any number that can be written as x/y, where x and y are integers and y != 0.) The above class should provide following member functions in addition to constructor, accessor and mututator methods: • displayF: Print the rational number to screen in fractional form • displayD: Print the rational number to screen in decimal form • add: adds two rational numbers • operator +: adds two rational numbers • reduce: simplifies the fraction to the lowest terms

Write a driver program to test the rational class.

Related Documents

Problems
October 2019 49
Problems
November 2019 46
Problems
July 2020 22
Problems
April 2020 30
Problems
May 2020 25
Problems
August 2019 37