Vous êtes sur la page 1sur 10

UNIVERSITI PUTRA MALAYSIA

FAKULTI KEJURUTERAAN
Faculty of Engineering

FACULTY OF ENGINEERING

DEPARTMENT OF CIVIL ENGINEERING

ECC3002 COMPUTER PROGRAMMING

SEMESTER 1 2018/2019

ASSIGNMENT TITLE :
1. Assignment 5 (FUNCTION)

NO MATRIC.NO NAME
1 193233 NURJANNAH BINTI AMIR

NAME OF LECTURER: DR AIDI HIZAMI BIN ALES@ALIAS

DATE OF SUBMISSION: 16TH NOVEMBER 2018


QUESTION1

#include <iostream>
using namespace std;
void input (double &ft, double &inch);
void conversion (double &ft, double &inch, double &m, double &cm );
void output (double &m, double &cm);
int main()
{
double ft,inch,m,cm;
input (ft,inch);
conversion (ft,inch,m,cm);
output (m,cm);
}
void input (double &ft, double &inch)
{
cout<<"\nEnter length in feet : ";
cin>> ft;
cout<<"Enter length in inches : ";
cin>> inch;
}
void conversion (double &ft, double &inch, double &m, double &cm )
{
m = ft*0.3048;
cm=((0.3048/12)*100)*inch;
}
void output (double &m, double &cm)
{
cout<<"\nLength equal to ";
cout<<m<<" metres and "<<cm<<" cm"<<endl;
}
QUESTION2
#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;
int parameter(int x , int n);
int main()
{
int x,n,a,b;
char rerun;
do
{
parameter(x,n);
cout<<"\nEnter value of x : ";
cin>>a;
cout<<"Enter value of n : ";
cin>>b;
cout<<"\nThe answer given by the formula : "<<parameter(a,b)<<endl;
cout << endl << "\n\n***Enter Z to run again, any other key to exit : ";
cin >> rerun;
}
while(toupper (rerun) == 'Z');
return 0;
}
int parameter(int x , int n)
{
int sum;
if (x>=0)
{
sum= x +(pow(x,2)/n);
}
if (x<=0)
{
sum= (-1)*( pow(x, n-1)/(n-1)) + (x-1)/(n+1);
}
return sum;
}
QUESTION3
#include <iostream>
using namespace std;
int qualitypoints(int &score);
int main()
{
int score;
char rerun;
do
{
qualitypoints(score);
cout<<"\nEnter student's score : ";
cin>>score;
cout<<"The student's quality point is "<<qualitypoints(score);
cout << endl << "\n\n***Enter Y to run again, any other key to exit : ";
cin >> rerun;
}
while(toupper (rerun) == 'Y');
return 0;
}
int qualitypoints (int &score)
{
if ((score>= 90) && (score<= 100))
{
return 4;
}
else if ((score>= 80) && (score<= 89))
{
return 3;
}
else if ((score>= 70) && (score<= 79))
{
return 2;
}
else if ((score>= 60) && (score<= 69))
{
return 1;
}
return 0;
}
QUESTION4
#include <iostream>
using namespace std;
void input(int &hrs, int &min, char &AMPM);
void conversion(int &hrs, int &min, char & AMPM);
void output(int &hrs, int &min, char &AMPM);
int main()
{
int hrs, min;
char AMPM, rerun;
do
{
input(hrs, min, AMPM);
conversion(hrs, min, AMPM);
output(hrs, min, AMPM);
cout << endl << "\n\n***Enter K to run again, any other key to exit : ";
cin >> rerun;
}
while(toupper (rerun) == 'K');
return 0;
}
void input(int &hrs, int &min, char &AMPM)
{
do
{
cout << "\nEnter hours : ";
cin >> hrs;
if(hrs > 23) cout << "\nPlease enter a value between 0 and 23" << endl;
}
while(hrs > 23);
do
{
cout << "Enter minutes : ";
cin >> min;
if(min > 59) cout << "\nEnter minutes time between 0 to 59 minutes " << endl;
}
while(min > 59);
}
QUESTION5
#include <iostream>
using namespace std;
double hmean(int x,int y);
int main()
{
int x, y;
cout<<"Enter two integers: ";
while (!(cin>>x)||!(cin>>y)||x==0||y==0)
{
cin.clear();
while(cin.get()!='\n')
continue;
cout<<"Enter two integers: ";
}
cout<<"The harmonic mean of "<<x<<" and "<<y<<" = "<<hmean(x,y);
return 0;
}
double hmean(int x, int y)
{
double hm=0.0;
hm=2.0*x*y/(x+y);
return hm;
}
QUESTION6
#include <iostream>
using namespace std;
int gcd(int x, int y)
{
if(y == 0)
{
return x;
}
else
{
return gcd(y, x % y);
}
}
int main()
{
int x,y;
cout << "\nEnter first number : ";
cin >> x;
cout << "Enter second number : ";
cin >> y;
cout << "\nGreatest common divisor (GCD) is " << gcd(x,y) << endl;
return 0;
}

Vous aimerez peut-être aussi