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

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