You are on page 1of 7

JOMO KENYATTA UNIVERSITY OF AGRICULTURE AND TECHNOLOGY

BACHELOR OF SCIENCE IN INFORMATION TECHNOLOGY


ICS 2105 DATA STRUCTURES AND ALGORITHMS
LAB I: REVIEW OF PROGRAMMING
Instructions:
This course is designed to teach you how to program efficiently. It assumes that you know the basics of
programming in C/C++,
 Can write, debug and run simple programs in C/C++, and
 Have some simple understanding of object-oriented design.

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

What does correct mean?


We need to have some formal notion of the meaning of correct: thus we define it to mean
"run in accordance with the specifications".

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();
}

4) An electricity power distribution company charges its domestic customers as follow:


Consumption units Rate of charges
0-200 0.50 per unit
201-400 Ksh 100 plus Ksh. 0.65 per unit excess of 200
401-600 Ksh 230 plus Ksh. 0.80 per unit excess of 400
601 and above Ksh 390 plus Ksh. 1.00 per unit excess of 600
2
Design and write a program that reads the customer number and power consumed and prints the amount
to be paid by the customer.

#include<stdio.h>
#include<conio.h>

void main()
{
int cno,units;
float bill;
clrscr();

printf("Enter Customer Number,Units : ");


scanf("%d %d",&cno,&units);

if(units <= 200)


bill = 0.50 * units;
else if(units >= 201 && units <= 400)
bill = 100 + 0.65 * (units - 200);
else if(units >= 401 && units <= 600)
bill = 230 + 0.85 * (units - 400);
else
bill = 390 + 1.00 * (units - 600);

printf("\n Customer Number = %d",cno);


printf("\n Units consumed = %d",units);
printf("\n Bill = %f",bill);
getch();
}

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:

Gross Salary Tax Rate


Gross <= 2000 No tax deduction
2000<Gross<=4000 3%
4000<Gross<=5000 5%
Gross>5000 8%
Design and write a program that will read an executive’s job number, level number, and basic pay and
then compute the net salary after withholdings income tax.
Gross salary = basic pay + house rent allowance + perks
Net salary = gross salary – income tax

#include<stdio.h>
#include<conio.h>

3
void main()
{
int level,ca,ea,perk,basic;
float hra,gs,tax,netsal;

clrscr();

printf("\n\n\tEnter the level::");


scanf("%d",&level);

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;

printf("\n\n\tEnter basic salary::");


scanf("%d",&basic);

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)

Write the C/C++ program and test with Fib (8)

c) Iterative Vs Recursive Functions


Fib(n) = if (n = 0) then 1
if (n = 1) then 1
else Fib (n-1) + Fib (n-2)

Rewrite the Fibonacci function using iteration.


Which one is better? Why?

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

You might also like