Sie sind auf Seite 1von 11

UNIVERSITY PUTRA MALAYSIA

FACULTY OF ENGINEERING
__________________________________________
ECC3101
COMPUTER PROGRAMING
(GROUP 2)
LAB ASSIGNMENT (3)
If STRUCTURES
NAME: SAMUEL CHOW TZE RERN
MATRIX NO: 157277
LAB DEMONSTRTOR: YONG CHAI TIK
DATE: 5/8/2010

157277

5/08/2010

QUESTION 1
SOURCE CODE:
#include <iostream>
#include <iomanip>
using namespace std;
int main (void)
{
double a,b,c; //declaring the variables
cout<<"Please insert the 1st number\n1st number is "; //asking user to insert 1st
number
cin>>a ; //user inserts 1st number
cout<<"Please insert the 2nd number\n2nd number is "; //asking user to insert
2nd number
cin>>b ; //user inserts 2nd number
cout<<"Please insert the 3rd number\n3rd number is "; //asking user to insert
3rd number
cin>>c ; //user inserts 3rd number
cout<<"The sum of the three numbers is " <<a+b+c <<"\n"; //summing the 3
numbers sum=a+b+c
cout<<"The average value of the three numbers is " ;
cout<<setprecision(2)<<fixed<<(a+b+c)/3 <<"\n" ; //setprecision(2) is to set
the decimal place shown to two
cout<<setprecision(0)<<fixed ; //settingback to no decimal place
cout<<"The procuct of the three numbers is "<<(a*b*c) <<"\n" ; //products are
a*b*c
cout<<"The smallest number is ";
if (a<=b&&c)
cout<<a ; //if "a" smaller than b & c print "a"
else if (b<=a&&c)
cout<<b ; //if "b" smaller than a & c print "b"
else if (c<=a&&b)
cout<<c ; //if "c" smaller than a & b print "c"
cout<<endl;
cout<<"The largest number is ";
if (a>=b&&c)
cout<<a ; //if "a" larger than b & c print "a"
else if (b>=a&&c)
cout<<b ; //if "b" larger than a & c print "b"
else if (c>=a&&b)
cout<<c ; //if "c" larger than a & b print "c"
cout<<endl;

157277

5/08/2010

system ("pause");
return 0;
}

157277

5/08/2010

157277

5/08/2010

QUESTION 2
SOURCE CODE:
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main(void)
{
double a,b,c,x1,x2,p,x ;
cout<<"insert constant a \na=" ;
cin>>a;
cout<<"insert constant b \nb=" ;
cin>>b;
cout<<"insert constant c \nc=" ;
cin>>c;
if (a==0&&b==0)
cout<<"There is no solution \n";
else if (a==0)
{
x=-c/b;
cout<<"The solution is "<<setprecision(3)<<x;
}
else if (p<0)
{
p=((b*b)-(4*a*c));
cout<<"there are no real roots";
}
else
{
p=((b*b)-(4*a*c));
x1=((-b+sqrt(p))/(2*a));
x2=((-b-sqrt(p))/(2*a));
cout<<"x1 = "<<setprecision(3)<<x1<<" and x2= "<<setprecision(3)<<x2;
cout<<"\n";
}
cout<<endl;
system ("pause");
return 0;
}

157277

5/08/2010

157277

5/08/2010

QUESTION 3

157277

5/08/2010

SOURCE CODE:
#include <iostream>
using namespace std;
int main ()
{
float ph;
cout<<"Please insert pH value \npH=" ;
cin>>ph;
if (ph>7)
{
if (ph<12)
cout<<"ALKALINE" ;
else
cout<<"VERY ALKALINE";
}
else
{
if (ph==7)
cout<<"NEUTRAL";
else
{
if (ph>2)
cout<<"ACIDIC";
else
cout<<"VERY ACIDIC" ;
}
}
cout<<endl;
system ("pause");
return 0;
}

157277

5/08/2010

157277

5/08/2010

10

157277

5/08/2010

11

Das könnte Ihnen auch gefallen