Home Work 2: Program Looping and Decision Statements
1 (a) (Project 4) Write a program that reads 3 integers, arranges them in the ascending order and displays the result. Although one can write the code in many different ways, the programs with less computation complexity and storage requirements will be given the highest credit.
#include <stdio.h>
main()
{
/* a program to sort three integers */
int x, y, z, temp;
scanf("%d %d %d", &x,&y,&z);
if (x > y)
{
temp = y;
y = x ;
x = temp;
}
if (y > z)
{
temp = z;
z = y ;
y = temp;
}
if (x > y)
{
temp = y;
y = x ;
x = temp;
}
printf("The three integers arranged in the ascending order
is, %d, %d, %d", x, y, z);
}
(b) Can you use the same algorithm you have used in your program if you were to sort 100 integers instead of 3?
2. (Project 5) Write a program in C that reads the present salary of each employee (assume a maximum of 3 employees) and computes their future salaries over the next 10-years for a raise of 2%, 4%, 6%, 8% and 10%.
#include <stdio.h>
main()
{
/*program to compute the salries of 3 employees after ten years
given a raise of 2%, 4%, 6%, 8% and 10% */
char emp1, emp2, emp3;
float sal1, sal2, sal3, raise;
int year;
printf("enter the name of the first employee and his salary\n");
scanf("%c %f", &emp1,&sal1);
printf("enter the name of the second employee and his salary\n");
scanf("\n%c %f", &emp2,&sal2);
printf("enter the name of the third employee and his salary\n");
scanf("\n%c %f", &emp3,&sal3);
for (raise = .02; raise <= .1; raise += .02)
{
for (year = 1; year <= 10; year++)
{
sal1 = sal1 + sal1 * raise;
sal2 = sal2 + sal2 * raise;
sal3 = sal3 + sal3 * raise;
}
printf("%c's salary after 10 years with %.2f%% raise
is %.2f\n", emp1,raise*100,sal1);
printf("%c's salary after 10 years with %.2f%% raise
is %.2f\n", emp2,raise*100,sal2);
printf("%c's salary after 10 years with %.2f%% raise
is %.2f\n", emp3,raise*100,sal3);
}
}
3. (Project 6) Write a program that works like a calculator. The program should allow for addition, subtraction, multiplication, division and computing the %.
#include <stdio.h>
main()
{
float x, y, result;
unsigned unsignedx, unsignedy, unsignedresult;
char op;
scanf("%f %c %f", &x,&op,&y);
if (op == '+')
result = x + y;
else if (op == '-')
result = x - y;
else if (op == '*')
result = x * y;
else if (op == '/')
result = x / y;
else if (op == '%')
{unsignedx = x;
unsignedy = y;
unsignedresult = unsignedx % unsignedy;
printf("%u %c %u = %u\n", unsignedx, op,
unsignedy, unsignedresult);
}
if (op != '%')
printf("%f %c %f = %f\n", x, op, y, result);
}
4. (Project 7) Develop a program to read in a value of taxable income and calculate the tax. The following is the tax table from the old 1987 IRS Form 1040 for married tax payers filing jointly, or for heads of households. The newer ones have fewer steps, but because this table has more steps, it makes for a more interesting problem.
| Taxable | Tax | Base |
| Income | Rate | Tax |
| $0 | 11.00% | $0 |
| $3,000 | 15.00% | $330 |
| $28,000 | 28.00% | $4,080 |
| $45,000 | 35.00% | $8,840 |
| $90,000 | 38.50% | $24,590 |
This works as follows. For example, if you have more than $3,000 in taxable income, but less thatn $28,000, pay $330 plus 15% of the amount in excess of $3,000.
Hint: You may use a series of if statements or the switch
statement.
#include <stdio.h>
main()
{
float income, tax, rate;
int bracket;
printf("enter your taxable income:");
scanf ("%f", &income);
if (income < 3000)
bracket = 1;
else if (income < 28000)
bracket = 2;
else if (income < 45000)
bracket = 3;
else if (income < 90000)
bracket = 4;
else
bracket = 5;
/* demonstration of the switch */
switch (bracket)
{
case (1):
{
tax = income * .11;
break;
}
case (2):
{
tax = 330 + ((income - 3000) * .11);
break;
}
case (3):
{
tax = 4080 + ((income - 28000) * .28);
break;
}
case (4):
{
tax = 8840 + ((income - 45000) * .35);
break;
}
case (5):
{
tax = 24590 + ((income - 90000) * .385);
break;
}
}
printf ("tax = %f\n", tax);
}
This document was generated using the LaTeX2HTML translator Version 96.1 (Feb 5, 1996) Copyright © 1993, 1994, 1995, 1996, Nikos Drakos, Computer Based Learning Unit, University of Leeds.
The command line arguments were:
latex2html -split 0 -html_version 3.0 hw2-sol.tex.
The translation was initiated by Vijay & on Thu Mar 5 10:22:07 EST 1998