You are on page 1of 7

Assignment 1: C++

1. Outline the major differences between a structure and a class (2)

2. The traffic section are keeping manual records of motor speeding offenders. The

information kept on each offence include driver's license number, name, address,

travelling speed and fine amount. The fine amount is calculated based on the

decision table given below:

Exceeded speed limit by /km/h Fine ($)


1-10 10.00
11-15 20.00
16-20 30.00
21-50 50.00
51 and above 0

If the driver exceed speed limit by 51 km/h or more he/she is liable to appear in court
and will not pay any fine. An officer is then responsible for calculating the total
amount collected from all the offenses. They have approached you a C++ expert to
design a program which caters for the above scenario.

Required

a.) Design a suitable structure which caters for the above scenario (14)

#include <iostream>

using namespace std;

struct Traffic{

string d_l,name, add;

double speed, limit, fine;

void setValues(){

cout<<"Enter drivers licence number"<<endl;

cin>>d_l;
cout<<"Enter drivers name"<<endl;

cin>>name;

cout<<"Enter address"<<endl;

cin>>add;

cout<<"Enter travelling speed"<<endl;

cin>>speed;

cout<<"Enter speed limit"<<endl;

cin>>limit;

double calc(){

double diff= speed-limit;

cout<<diff;

if( diff>=0){

if(diff>=1 && diff<=10){

fine= 10.00;

else if(diff>=11 && diff<=15){

fine= 20.00;

else if(diff>=16 && diff<=20){

fine= 30.00;

else if(diff>=21 && diff<=50){

fine= 50.00;

else{

fine= 0;
}

} else{

fine= 0;

return fine;

void display(){

cout<<d_l<<"\t" <<name<<"\t"<<add<<"\t"<<speed<<"\t"<<calc()<<endl;

};

b.) Create your instances within the main where the user will specify the total number
of offenders before displaying the output (5)

int main()

double total=0 ;

int n;

cout<<"Enter number of offenders"<<endl;

cin>>n;

Traffic t[n];

for(int i= 0; i<n; i++){

t[i].setValues();

total+= t[i].calc();

cout<<"Drvers"<<"\t"
<<"Name"<<"\t"<<"Address"<<"\t"<<"Speed"<<"\t"<<"Fine"<<endl;
for(int i= 0; i<n; i++){

t[i].display();

cout << "Total\t\t\t\\tt" <<total<< endl;

return 0;

3. Kampala Municipality are keeping manual records of parking violators. If a


motorist does not pay for a parking disc the car must be clamped and they are
supposed to pay a fine of $35.00. Should they exceed one hour without paying an
extra $10 dollars is paid per hour. Information captured in their records include
vehicle registration number, responsible person , clamping fee, extra charge(cost
per hour x number of hours ) , total payable(extra charge+ clamping fee). There is
also need to compute the total amounts collected. They have approached you a C++
expert to design a program which caters for the above scenario.

Required

a.) Design a suitable structure which caters for the above scenario (14)

Ans:

#include <iostream>

using namespace std;

struct Kampala{

/*

vehicle registration number, responsible person , clamping fee, extra charge(cost


per hour x number of hours ) ,

total payable(extra charge+ clamping fee).

*/

string reg, person;

double fee,costph, hrs, extra;

char over;

double calcExtra(){
if(over=='Y' || over=='y'){

cout<<"Enter Number of Hours: "<<endl;

cin>>hrs;

cout<<"Enter Cost per hour"<<endl;

cin>>costph;

extra= hrs* costph;

} else{

extra= 0;

return extra;

void setValues(){

fee= 35.00;

cout<<"Enter Reg Number"<<endl;

cin>>reg;

cout<<"Enter Person"<<endl;

cin>>person;

cout<<"Has the car exceed"<<endl;

cin>>over;

calcExtra();

void display(){

cout<<reg<<"\t"<<person<<"\t"<<fee<<"\t"<<extra<<extra + fee<<endl;
}

};

b.) Create your instances within the main where the user will specify the total number
of violators before displaying the output (5)

Ans:

int main()

double total=0 ;

int n;

cout<<"Enter number of Violators"<<endl;

cin>>n;

Kampala k[n];

for(int i= 0; i<n; i++){

k[i].setValues();

total+= k[i].extra+ k[i].fee;

cout<<"Reg"<<"\t" <<"Name"<<"\t"<<"Fee"<<"\t"<<"Extra"<<"\t"<<"Total"<<endl;

for(int i= 0; i<n; i++){

k[i].display();

cout << "Total\t\t\t\t" <<total<< endl;

return 0;

You might also like