THE OPEN UNIVERSITY OF SRI LANKA DIPLOMA IN TECHNOLOGY - LEVEL 3 ACADEMIC YEAR 2007/2008 MEK3170 C PROGRAMMING ASSIGNMENT

#1-MODEL ANSWERS

Question 1- Model Answers (10 marks)
Model Answer (a)-Using a do-while loop #include<iostream.h> #include<conio.h> void main() { clrscr(); int x=10; //Using a do-while loop do{ //Value 50 will not be printed if(x==50){ x=x+10; } else{ //Printing the other values from 10-100 cout<<x; x=x+10; cout<<"\n"; } } while(x<=100); getch(); } Model Answer (b) – Using a for loop #include<iostream.h> #include<conio.h> void main() { clrscr(); for(int x=10;x<=100;x=x+10){ //The value 50 will not be printed if(x==50); //printing the other values else cout<<x<<endl; } getch(); }

Page 1 of 7

Question 2- Model Answer (25 marks)
#include<iostream.h> #include<conio.h> void main() { clrscr(); cout<<"\t----OUSL STUDENT ENROLMENTS----\n\n"; cout<<"\tYear\tNo.of Students\n\n"; //Representing students enrolled in 1990 cout<<"\t1990\t"; for(int a=1;a<=2;a++){ for(int b=15;b>0;b--){ cout<<"*"; } cout<<"\n\t\t"; } //Representing students enrolled in 1995 cout<<"\n"; cout<<"\t1995\t"; for(int c=1;c<=2;c++){ for(int d=19;d>0;d--){ cout<<"*"; } cout<<"\n\t\t"; } //Representing students enrolled in 2000 cout<<"\n"; cout<<"\t2000\t"; for(int e=1;e<=2;e++){ for(int f=19;f>0;f--){ cout<<"*"; } cout<<"\n\t\t"; } //Representing students enrolled in 2005 cout<<"\n"; Page 2 of 7

cout<<"\t2005\t"; for(int g=1;g<=2;g++){ for(int h=25;h>0;h--){ cout<<"*"; } cout<<"\n\t\t"; } getch(); }

Question 03- Model Answer (25 marks)
#include<iostream.h> #include<conio.h> #include<stdlib.h> #include<iomanip.h> void main() { clrscr(); int option; start: //Displaying the menu cout<<"\t\t---MENU---\n\n"; cout<<"1.Calculating the current in an electrical circuit"<<endl; cout<<"2.Calculating the acceleration of a car"<<endl; cout<<"3.Calculating the intercept of a straight line graph"<<endl; cout<<"4.Quit\n\n"; //Taking the user input cout<<"Enter an option:"; cin>>option; //Setting precision to two decimal points using <iomanip.h> cout<<setiosflags(ios::fixed|ios::showpoint); cout<<setprecision(2); switch(option){ case 1: //Taking user input float V,R,current; cout<<"Enter voltage:";

Page 3 of 7

cin>>V; cout<<"Enter resistance:"; cin>>R; //Calculating the current and displaying the answer current=V/R; cout<<"The current in the electrical signal is "<<current<<"\n\n"; getch(); clrscr(); //A label to display the main menu again goto start; case 2: //Taking the user input float s,u,t,acc; cout<<"Enter Distance:"; cin>>s; cout<<"Enter velocity:"; cin>>u; cout<<"Enter Time:"; cin>>t; //Calculating the acceleration and displaying the answer if(t>0){ acc=(2*(s-(u*t)))/(t*t); cout<<"Acceleration of the car is "<<acc<<"\n\n"; } else { cout<<"Time cannot be a 0 or a negative value!"; } getch(); clrscr(); //A label to display the main menu again goto start; case 3: const int m=4; int x,y,intercept; //Taking the user input cout<<"Enter a value for x coordinates:"; cin>>x; cout<<"Enter a value for y coordinates:"; cin>>y; //Calculating the intercept and displaying the answer intercept=y-(m*x);

Page 4 of 7

cout<<"Intercept of the grapg is "<<intercept<<"\n\n"; getch(); clrscr(); //A label to display the main menu again goto start; case 4: exit(0); break; default: cout<<"Invalid option!\n\n"; getch(); clrscr(); goto start; } getch(); }

Question 4-Model Answer (40 marks)
#include<iostream.h> #include<conio.h> #include<stdlib.h> #include<iomanip.h> void main(){ clrscr(); //Declaring the rates per unit as constants const float B1=3,B2=4.5,B3=7.80,B4=15,B5=20.30; //Declaring the fixed charge and fuel surcharge percentage as constants const float fix=240.00,fuel=0.2; //Declaring other variables for calculations float fuel_charge,tot_charge,consession,add_charges,sub_total,final_total; int consum; char type; //Taking user input-consumption in units and customer type cout<<"Enter your electricity consumption in units:"; cin>>consum; cout<<"Enter customer type:"; cin>>type;

Page 5 of 7

//-------Calculating the charges for the units-----//When the consumption units are 30 or less than 30 if (consum<=30) tot_charge=consum*B1; //When the consumption units are more than 30 but less than 60 else if (consum>30 && consum<=60) tot_charge=(30*B1)+(consum-30)*B2; //When the consumption units are more than 60 but less than 90 else if (consum>60 && consum<=90) tot_charge=(30*B1)+(30*B2)+(consum-60)*B3; //When the consumption units are more than 90 but less than 180 else if (consum>90 && consum<=180) tot_charge=(30*B1)+(30*B2)+(30*B3)+(consum-90)*B4; //When the consumption units are more than 180 else tot_charge=(30*B1)+(30*B2)+(30*B3)+(90*B4)+(consum-180)*B5; //Calculating the Fuel surcharge if (consum>90) fuel_charge=tot_charge*fuel; else fuel_charge=0; //Calculating the total amount(Unit charges+Fixed charge+Fuel charge) sub_total=tot_charge+fix+fuel_charge; //Calculations depending on the customer type switch(type) { //For regular customers case 'r': case 'R': consession=0; add_charges=0; final_total=sub_total; break; //For consession customers case 'c': case 'C':

Page 6 of 7

consession=sub_total*0.1; add_charges=0; final_total=sub_total-consession; break; //For business customers case 'b': case 'B': consession=0; add_charges=sub_total*0.1; final_total=sub_total+add_charges; break; //When an illegal customer type is entered programme display a message & terminates default: cout<<"Wrong customor type"; getch(); exit(0); } //Setting the precision into two decimal points using <iomanip.h> cout<<setiosflags(ios::fixed|ios::showpoint); cout<<setprecision(2); //Dislaying the electricity bill cout<<"\n\n"; cout<<"\tCustomer Type:"<<type<<endl; cout<<"\t\t\t\t\tAmount(Rs)\n\n"; cout<<"\tCharges for "<<consum<<" units\t\t"<<setw(10)<<setfill(' ')<<tot_charge<<endl; cout<<"\tMonthly fixed charge\t\t"<<setw(10)<<setfill(' ')<<fix<<endl; cout<<"\t20% Fuel Surcharge\t\t"<<setw(10)<<setfill(' ')<<fuel_charge<<endl; cout<<"\tTotal amount\t\t\t"<<setw(10)<<setfill(' ')<<sub_total<<endl; cout<<"\tConsession\t\t\t"<<setw(10)<<setfill(' ')<<consession<<endl; cout<<"\tAdditional Charges\t\t"<<setw(10)<<setfill(' ')<<add_charges<<endl; cout<<"\tTotal Charge\t\t\t"<<setw(10)<<setfill(' ')<<final_total; cout<<"\n\n\tYour total Electricity Charge is Rs."<<final_total; getch(); }

Page 7 of 7

Sign up to vote on this title
UsefulNot useful

Master Your Semester with Scribd & The New York Times

Special offer for students: Only $4.99/month.

Master Your Semester with a Special Offer from Scribd & The New York Times

Cancel anytime.