#Include #Include Using Namespace STD Void Main (Double Sale, Com, Earn
#Include #Include Using Namespace STD Void Main (Double Sale, Com, Earn
#Include #Include Using Namespace STD Void Main (Double Sale, Com, Earn
Good Programs
There are a number of facets to good programs: they must
a) run correctly
b) run efficiently
c) be easy to read and understand
d) be easy to debug and
e) be easy to modify
The first of these is obvious - programs which don't run correctly are clearly of little use.
"Efficiently" is usually understood to mean in the minimum time - but occasionally there will be other
constraints, such as memory use, which will be paramount.
As will be demonstrated later, better running times will generally be obtained from use of the most
appropriate data structures and algorithms, rather than through "hacking", i.e. removing a few statements
by some clever coding - or even worse, programming in assembler!
This course will focus on solving problems efficiently: you will be introduced to a number of fundamental
data structures and algorithms (or procedures) for manipulating them.
Key terms
a) Correct
A correct program runs in accordance with its specifications
b) Algorithm
A precisely specified procedure for solving a problem.
c) Hacking
Producing a computer program rapidly, without thought and without any design methodology.
This lab tutorial reviews C/C++ programming constructs and techniques and your solution should include:
i) Analysis of the problem
ii) Program design: Either pseudocode or flowchart diagrams
iii) The computer program (C/C++)
1) Juja Pharmaceuticals Company pays its salesmen on a commission basis. The salesmen receive Kshs.
200 per week plus 9% of their gross sales for that week. For example, a salesman who sells Kshs. 5000
worth of drugs in a week receives Kshs. 200 plus 9% of Kshs. 5000, which is a total of Kshs. 650.
Design and write a C program that will input each salesman’s gross sales for the previous week and
calculate and display the salesman’s earnings. Process one salesman’s figures at a time, and include 50
salesmen.
#include<iostream>
#include<conio.h>
using namespace std;
void main()
{
double sale, com, earn;
1
while (true)
{
cout<<"Enter Sales in Dollars (-1 to end): ";
cin>>sale;
if(sale== -1)
{
break;
}
com = sale / 100 * 9;
earn = com + 200;
cout<<"Salary is: $"<<earn<<endl;
}
getch();
}
2) The straight line method of computing the yearly depreciation of the value of an item is given by:
Depreciation = (purchase price – salvage price)/year of service
Design and write a program to determine the salvage value of an item when the purchase price, years
of service, and the annual depreciation are given.
3) The total distance traveled by a vehicle in t seconds is given by
Distance = ut + (at2)/2
Where u is the initial velocity, a is the acceleration. Design and write a program to evaluate the distance
traveled at regular intervals of time, given the values of u and a. The program should provide the
flexibility t the user to select his own time intervals and repeat the calculations for different values of u
and a.
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main() e
{
int i, n, sec;
float d, u, a;
clrscr();
printf("Enter the no. of intervals\n");
scanf("%d", &n);
for(i = 1; i <= n; i++)
{
printf("interval: %d \n", i);
printf("Enter the time in seconds \n");
scanf("%d",&sec);
printf("Enter the velocity \n");
scanf("%f", &u);
printf("Enter the acceleration \n");
scanf("%f", &a);
d= d + (u * sec + (a * (pow(sec, 2))) / 2);
}
printf("Total distance travelled is %.2f", d);
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
int cno,units;
float bill;
clrscr();
5) A manufacturing company has classified its executives into four levels for the benefit of certain perks.
The levels and corresponding perks are shown below:
Level perks
Conveyance allowance entertainment allowance
1 1000 500
2 750 200
3 500 100
4 250
An executive’s gross salary includes basic pay, house rent allowance at 25% of
basic pay and other perks. Income tax is withheld from the salary on a
percentage basis as follows:
#include<stdio.h>
#include<conio.h>
3
void main()
{
int level,ca,ea,perk,basic;
float hra,gs,tax,netsal;
clrscr();
if(level==1)
{
ca=1000;
ea=500;
}
if(level==2)
{
ca=750;
ea=200;
}
if(level==3)
{
ca=500;
ea=100;
}
if(level==4)
{
ca=250;
ea=0;
}
perk=ca+ea;
hra=basic*0.10;
gs=basic+hra+perk;
if(gs<2000)
{
tax=0;
}
else if(gs>=2000 && gs<=4000)
{
tax=gs*0.03;
}
else if(gs>=4000 && gs<=5000)
{
tax=gs*0.05;
}
else
{
tax=gs*0.08;
}
netsal=gs-tax;
printf("\n\n\tLevel\tPerk\tBasic\tHRA\tTax\tGS\tNetsal\n\n");
printf("\n\t**************************************************\n");
printf("\n\n\t%d\t%d\t%d\t%.2f\t%.2f\t%.2f\t%.2f",level,perk,basic,hra,tax,gs,netsal);
4
getch();
}
6)
a) Modify the program below to calculate and display the perimeter of the Rectangle
// Rectangle class example with a constructor
#include <iostream>
using namespace std;
class Rectangle {
int width, height;
public:
Rectangle (int,int); //constructor declaration
int area (void) {
return (width*height);
}
};
Rectangle::Rectangle (int w, int h) {
width = w;
height = h;
}
// driver program
int main () {
Rectangle rectA (3,4);
Rectangle rectB(5,6);
cout << "Rectangle A area: " << rectA.area ();
cout<< endl;
cout << "Rectangle B area: " << rectB.area (); cout<<endl;
}
b) Recursive Functions
Consider the following definition for a recursive Fibonacci function:
Fib(n) = if (n = 0) then 1
if (n = 1) then 1
else Fib (n-1) + Fib (n-2)
7)
a) Implementing algorithms
Implement the following algorithm and test using the following test data: 10, 7, 9
5
b) Implementing algorithms
Implement the following algorithm and test using the following test data: 90,60,80,50,35,77,88,45,79, 98
Program: Determine the average grade of a class
Initialize Counter and Sum to 0
Do While there are more data
Get the next Grade
Add the Grade to the Sum
Increment the Counter
Loop
Computer Average = Sum / Counter
Display Average
6
7