Você está na página 1de 7

THE OPEN UNIVERSITY OF SRI LANKA

DIPLOMA IN TECHNOLOGY - LEVEL 3


ACADEMIC YEAR 2007/2008
MEK3170 C PROGRAMMING
ASSIGNMENT #2-MODEL ANSWERS

Question 1- Model Answers (25 marks)

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>

//function prototype declaration


void displayMenu();
float acceleration(float distance,float velocity, float time);
float current (float voltage,float resistance);
float intercept(float x_coord,float y_coord);

void main()
{
clrscr();

int option;
start:
//Display the menu to the user
displayMenu();
//Get 'option’ from the user
cout<<"Enter an option:";
cin>>option;

switch(option){
case 1:
int V,R;
cout<<"Enter voltage:";
cin>>V;
cout<<"Enter resistance:";
cin>>R;
cout<<"The current in the electrical signal is "<<current(V,R)<<"\n\n";
getch();
clrscr();
goto start;

case 2:
float S,vel,t;

Page 1 of 7
cout<<"Enter Distance:";
cin>>S;
cout<<"Enter velocity:";
cin>>vel;
cout<<"Enter Time:";
cin>>t;
cout<<"Acceleration of the car is "<<acceleration(S,vel,t)<<"\n\n";
getch();
clrscr();
goto start;

case 3:
float x,y;
cout<<"Enter a value for x coordinates:";
cin>>x;
cout<<"Enter a value for y coordinates:";
cin>>y;
cout<<"Intercept of the graph is "<<intercept(x,y)<<"\n\n";
getch();
clrscr();
goto start;

case 4:
exit(0);
break;

default:
cout<<"Invalid option!\n\n";
getch();
clrscr();
goto start;
}

getch();
}

//Function to display the menu


void displayMenu(){
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";
}

Page 2 of 7
//Calculating the current in an electrical circuit
float current (float vol,float res){
return vol/res;
}

//Calculating the acceleration of a car


float acceleration(float distance,float velocity, float time){
int result;
if(time>0){
result=(2*(distance-(velocity*time)))/(time*time);
return result;
}
else {
cout<<"Time cannot be a negative value!";
return 0;
}

//Calculating the intercept of a straight line graph


float intercept(float x_coord,float y_coord){
const int m=4;
float c;
c=y_coord-(m*x_coord);
return c;
}

Question 2- Model Answer (25 marks)

#include<iostream.h>
#include <conio.h>

void main (){


clrscr();
int marks[5];
int *point;
int temp;

//Getting user input using a array and a pointer


cout<<"Please enter 5 integer values for marks...\n";
point=marks;
for ( int z=0; z<5; z++) {
cout<<"marks["<<z<<"]:";
cin>>*point;
point++;

Page 3 of 7
}

//Calculating the sum of the values


cout<<"\nThe sum of the values you entered is ";
int result=0;
for (int *p=marks;p<marks+5;p++){
result = result+(*p);
}

// Arranging the marks in descending order


for(int x=0;x<5;x++){
for ( int y=0; y<4; y++) {
if(marks[y+1]>marks[y]){
temp=marks[y];
marks[y]=marks[y+1];
marks[y+1]=temp;
}
}
}

//Displaying the marks in descending order


cout<<result<<"\n\n";
cout<<"The 5 marks you entered in descending order...\n";
for(int n=0;n<5;n++){
cout<<"\n"<<marks[n];
}
getch();
}

Question 03- Model Answer (25 marks)

#include<iostream.h>
#include<string.h>
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>

void main()
{
clrscr();
char f_name[20];
char l_name[40];
char full_name[60];
char u_name[20];
char password[15];
char rentered_password[15];

Page 4 of 7
//Get user details
cout<<"Enter your first name:";
gets(f_name);
cout<<"Enter your last name:";
gets(l_name);

cout<<"\nEnter a user name:";


gets(u_name);
pass:
cout<<"Enter a password(Password should be minimum of 7 characters):";
gets(password);

//test whether the pasword is more than 7 characters


if(strlen(password)>=7){
cout<<"Re-enter Password to confirm:";
gets(rentered_password);
//Check the password with the re-entered pasword
if(!strcmp(rentered_password,password)){
clrscr();
//Displaying user login details
cout<<"********Your Login Details********\n\n";
strcpy(full_name,f_name);
strcat(full_name," ");
strcat(full_name,l_name);
cout<<"Name:\t\t"<<full_name<<endl;
cout<<"User Name:\t"<<u_name<<endl;
cout<<"Password:\t"<<password;
}
/*Exit programme when the passowrd doen't match with the
re-entered password*/
else
cout<<"Passwords doesn't match!";
getch();
exit(0);
}
/*Display an error message when the number of characters of the
password is less than 7*/
else {
cout<<"Your password is less than 7 characters!\n\n";
goto pass;

getch();
}

Page 5 of 7
Question 4-Model Answer (25 marks)

#include <iostream.h>
#include<conio.h>

// Each class object represents a bank account


// The current balance depends on the history of deposits and withdrawals

class Account {
//current account balance
long balance;

public:
void deposit(long amount) {
balance =balance+amount;
}

void withdraw(long amount){


balance =balance-amount;
}

void status () {
cout<<"Bank Balance: Rs."<<balance<<endl;
if(balance<0)
cout<<"You have a debit balance!"<<endl;
else if (balance==0)
cout<<"You have a zero balance"<<endl;
}

/*constructor- used to initialize newly created accunts,


if no initialAmount is specified*/

Account (long initialAmount = 0) {


balance = initialAmount;
}

//Destructor
~Account () {
}
};

void main ()
{
clrscr();

Page 6 of 7
//object creation
Account Nimal, Kamal(100),Saman(-400);

//Nimal deposit Rs.500


Nimal.deposit (500);
//Check the status of Nimal's account
cout<<"---Nimal's account---"<<endl;
Nimal.status();
cout<<endl;

//Kamal deposit Rs.400


Kamal.deposit(400);
//Kamal withdraw Rs.200 from the account
Kamal.withdraw (200);
//Check the status of Nimal's account
cout<<"---Kamal's Account---"<<endl;
Kamal.status ();
cout<<endl;

//Saman deposit Rs.400


Saman.deposit(400);
//Check the status of Saman's account
cout<<"---Saman's Account---"<<endl;
Saman.status();

getch();
}

Page 7 of 7

Você também pode gostar