0% found this document useful (0 votes)
509 views45 pages

PSP

Here is a C++ program that calculates New Jersey state income tax based on the rules provided: #include <iostream> using namespace std; int main() { double income, tax; cout << "Enter your taxable income: "; cin >> income; if(income <= 20000) { tax = income * 0.02; } else { tax = 400 + (income - 20000) * 0.025; } cout << "Your NJ state income tax is: " << tax << endl; return 0; } This program first prompts the user to enter their taxable income. It then uses an if/else statement to check if the

Uploaded by

adeem Mir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
509 views45 pages

PSP

Here is a C++ program that calculates New Jersey state income tax based on the rules provided: #include <iostream> using namespace std; int main() { double income, tax; cout << "Enter your taxable income: "; cin >> income; if(income <= 20000) { tax = income * 0.02; } else { tax = 400 + (income - 20000) * 0.025; } cout << "Your NJ state income tax is: " << tax << endl; return 0; } This program first prompts the user to enter their taxable income. It then uses an if/else statement to check if the

Uploaded by

adeem Mir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 45

Level Precedence group Operator Description Grouping

1 Scope :: scope qualifier Left-to-right


++ -- postfix increment / decrement
() functional forms
2 Postfix (unary) Left-to-right
[] subscript
. -> member access
++ -- prefix increment / decrement
~ ! bitwise NOT / logical NOT
+ - unary prefix
3 Prefix (unary) & * reference / dereference Right-to-left
new delete allocation / deallocation
sizeof parameter pack
(type) C-style type-casting
4 Pointer-to-member .* ->* access pointer Left-to-right
5 Arithmetic: scaling * / % multiply, divide, modulo Left-to-right
6 Arithmetic: addition + - addition, subtraction Left-to-right
7 Bitwise shift << >> shift left, shift right Left-to-right
8 Relational < > <= >= comparison operators Left-to-right
9 Equality == != equality / inequality Left-to-right
10 And & bitwise AND Left-to-right
11 Exclusive or ^ bitwise XOR Left-to-right
12 Inclusive or | bitwise OR Left-to-right
13 Conjunction && logical AND Left-to-right
14 Disjunction || logical OR Left-to-right
= *= /= %= += -=
Assignment-level assignment / compound assignment
15 >>= <<= &= ^= |= Right-to-left
expressions
?: conditional operator
16 Sequencing , comma separator Left-to-right
Software Development Method
1. Specify the problem requirements.
2. Analyze the problem.
– Input, output and other requirements
3. Design the algorithm to solve the problem.
4. Implement the algorithm.
5. Test and verify the completed program.
6. Maintain and update the program.
CASE STUDY Converting Miles to
Kilometers
• PROBLEM
– Your summer surveying job requires you to study
some maps that give distances in kilometers and some
that use miles. You and your coworkers prefer to deal
in metric measurements. Write a program that
performs the necessary conversion
• ANALYSIS
• Problem Input
– miles /* the distance in miles*/
• Problem Output
– kms /* the distance in kilometers */
• Relevant Formula
– 1 mile = 1.609 kilometers
• DESIGN
– Get the distance in miles.
– Convert the distance to kilometers.
– Display the distance in kilometers
• ALGORITHM WITH REFINEMENTS
– Get the distance in miles.
– Convert the distance to kilometers.
• The distance in kilometers is 1.609 times the distance in
miles.
– Display the distance in kilometers.
• IMPLEMENTATION
• TESTING
Problem
• Write a program that will compute the tax and
tip on a restaurant bill meal charge. The tax
should be 6.75% of the meal cost. The tip
should be 8.5% of the total after adding the
tax. Display the meal cost, tax amount, tip
amount and total bill.
• One acre of a land is equivalent to 43,560
square feet. Write a program that calculates
and displays the number of acre in a tract of
land with 389,767 square feet.
Problem
A C++ program is required that calculates and displays the weekly
gross pay and net pay of two employees. The first employee is
paid an hourly rate of $16.43, and the second is paid an hourly
rate of $12.67. Both employees have 20% of their gross pay
withheld for income tax, and both pay 2% of their gross pay,
before taxes, for medical benefits.
a. For this programming problem, how many outputs are
required?
b. How many inputs does this problem have?
c. Write an algorithm for converting the input items into output
items.
d. Test the algorithm using the following sample data:
The first employee works 40 hours during the week, and the
second employee works 35 hours.
Problem
• Write a program that takes the length and width of
a rectangular yard and the length and width of a
rectangular house situated in the yard. Your
program should compute the time required to cut
the grass at the rate of two square feet a second.
• Suppose that the cost of sending an international
fax is calculated as follows: The service charge is
$3.00, $.20 per page. Design an algorithm that asks
the user to enter the number of pages to be faxed.
The algorithm then uses the number of pages to
be faxed to calculate the amount due.
Problem
• Jason typically uses the Internet to buy various
items. the total cost of the items ordered, at one
time, is $200 or more, the shipping and handling is
$10 per item. Design an algorithm that prompts
Jason to enter the number of items ordered and
the price of each item. The algorithm then outputs
the total billing amount.
• Given the length and width of a rectangle, write
C++ program that computes and outputs the
perimeter and area of the rectangle
Problem
• Write a C++ program that prompts the user to input
the elapsed time for an event in hours, minutes, and
seconds. The program then outputs the elapsed time
in seconds.
• To make a profit, a local store marks up the prices of
its items by a certain percentage. Write a C++
program that reads the original price of the item
sold, the percentage of the marked-up price, and the
sales tax rate. The program then outputs the original
price of the item, the percentage of the mark-up, the
store’s selling price of the item, the sales tax rate, the
sales tax, and the final price of the
Problem
A milk carton can hold 3.78 liters of milk. Each morning, a
dairy farm ships cartons of milk to a local grocery store.
The cost of producing one liter of milk is $0.38, and the
profit of each carton of milk is $0.27. Write a program
that does the following:
a) a. Prompts the user to enter the total amount of milk
produced in the morning.
b) b. Outputs the number of milk cartons needed to hold
milk.
c) c. Outputs the cost of producing milk.
d) d. Outputs the profit for producing milk.
Escape Character
combination of a backslash and these characters is called an escape sequence
Formatted Output
The setiosflags() Manipulator

• cout<<setw(10)<<setiosflags(ios::left)<<142;
Math Library Functions Revisited
Me tho d De sc rip tio n Exa m p le
ceil( x ) rounds x to the smallest integer ceil( 9.2 ) is 10.0
not less than x ceil( -9.8 ) is -9.0
cos( x ) trigonometric cosine of x cos( 0.0 ) is 1.0
(x in radians)
exp( x ) exponential function ex exp( 1.0 ) is 2.71828
exp( 2.0 ) is 7.38906
fabs( x ) absolute value of x fabs( 5.1 ) is 5.1
fabs( 0.0 ) is 0.0
fabs( -8.76 ) is 8.76
floor( x ) rounds x to the largest integer floor( 9.2 ) is 9.0
not greater than x floor( -9.8 ) is -10.0
fmod( x, y ) remainder of x/y as a floating- fmod( 13.657, 2.333 ) is 1.992
point number
log( x ) natural logarithm of x (base e) log( 2.718282 ) is 1.0
log( 7.389056 ) is 2.0
log10( x ) logarithm of x (base 10) log10( 10.0 ) is 1.0
log10( 100.0 ) is 2.0
pow( x, y ) x raised to power y (xy) pow( 2, 7 ) is 128
pow( 9, .5 ) is 3
sin( x ) trigonometric sine of x sin( 0.0 ) is 0
(x in radians)
sqrt( x ) square root of x sqrt( 900.0 ) is 30.0
sqrt( 9.0 ) is 3.0
tan( x ) trigonometric tangent of x tan( 0.0 ) is 0
(x in radians)
Fig . 3.2 M a th lib ra ry func tio ns.
Mathematical Library Functions
Casts
• dataType(expression)
• int (a*b)
Run time cast
staticCast<dataType>(expression)
staticCast<int>(a*b) is same as compile time
int (a*b)
QUESTION
• the New Jersey state income tax is assessed at
2% of taxable income for incomes less than or
equal to $20,000. For taxable incomes greater
than $20,000, state taxes are 2.5% of the
income exceeding $20,000 plus a fixed
amount of $400.
The expression to be tested is whether taxable
income is less than or equal to $20,000,
• It is only used to solve the problems that require
two or more unrelated decisions or in which all
the decisions must be processed.

• Example1: Find the amount to charge people of


varying ages for a concert ticket. When the
person is under 16, the charge is Rs. 700/-,
when the person is 60 or over, the charge is Rs.
500/-, all others are charged Rs. 1000/-.

• Example2: Change the value of x to 0 when it


becomes greater than 100 , and change the
value of y to 0 when it becomes less than 250.
TEMPERATURE CONVERSION
PROGRAM
• Use of character data type
Nested if Statements

if(hours<9)
if (distance>500)
cout<< “snap”;
else
cout<< “pop”;
if-else chain
Input Code Marital Status
M Married
S Single
D Divorced
W Widowed
if else chain
Monthly Sales Income
• Greater than or equal to $50,000
– $375 plus 16% of sales
• Less than $50,000 but greater than or equal to $40,000
– $350 plus 14% of sales
• Less than $40,000 but greater than or equal to $30,000
– $325 plus 12% of sales
• Less than $30,000 but greater than or equal to $20,000
– $300 plus 9% of sales
• Less than $20,000 but greater than or equal to $10,000
– $250 plus 5% of sales
• Less than $10,000
– $200 plus 3% of sales
The switch Statement

You might also like