Sei sulla pagina 1di 5

12345678910111213141516171819

20212223242526272829303132333
43536373839404142434445464748
Jamal Hannun-14194
49505152535455565758596061626
GENG106-Assignment3
36465666768697071727374757677
78798081828384858687888990919
29394959697989910010110210310
41051061071081091101111121131
14115116117118119120121122123
12412512612712812913013113213
31341351361371381391401411421
43144145146147148149150151152
15315415515615715815916016116
21631641651661671681691701711
72173174175176177178179180181
18218318418518618718818919019
11/27/2013
201300283

Qatar University
College of Engineering.

Computer Programming Lab (GENG106)

1) [5 points]Write a function called zero_small() that has two integer


arguments being passed by reference and sets the smaller of the two
numbers to 0. Write the main program to access the function.

#include <iostream>
using namespace std;
void zero_small(int & num1, int & num2)
{
if (num1 == num2)
cout<< " Error, numbers are equal\n";
else if (num1<num2)
{
cout<<"\nThe 1st integer number is smaller\n";
num1=0;
}
else
{
cout<<"\nThe 2nd integer number is smaller\n";
num2=0;
}
}
//------------------------------------------------------------void main()
{
int num1,num2;
cout<<"Enter the 1st integer number : ";
cin>>num1;
cout<<"Enter the 2nd integer number : ";
cin>>num2;
zero_small(num1,num2);
cout<<" The 1st integer number : "<<num1<<endl;
cout<<" The 2nd integer number : "<<num2<<endl;
system("pause");
}

Qatar University
College of Engineering.

Computer Programming Lab (GENG106)

2) [15 points] Write a program that lets the user perform arithmetic operations
on two numbers. Your program must be menu driven, allowing the user to
select the operation (+, -, *, or /) and input the numbers. Furthermore, your
program must consist of following functions:
1. Function showChoice: This function shows the options to the user and
explains how to enter data.
2. Function add: This function accepts two number as arguments and returns
sum.
3. Function subtract: This function accepts two number as arguments and
returns their difference.
4. Function multiply: This function accepts two number as arguments and
returns product.
5. Function divide: This function accepts two number as arguments and
returns quotient.

#include <iostream>
#include <cstdlib>
int num1,num2,choice;
using namespace std;

//trace main 1st


//Global

int take_check_Choice()
{
bool flag;
do
{
flag = 0 ;
cout<<" choice : ";
cin>>choice;

//user will enter the choice

if (choice<1 || choice>5 || choice%1 != 0)//checking if not integer or out-rang


{flag = 1; cout<<"Invalid choice, enter an integer (1-4)\n";}
}while(flag);

//will repeat till user enters in-rang integer

return choice;
}
//-------------------------------------------------------------int showChoice()
{
cout<<" Choose one of the following operations to perform\n";
cout<<"1 : addition
\n";
cout<<"2 : subtraction
\n";
cout<<"3 : multiplication
\n";
cout<<"4 : division
\n";
cout<<"5 : Exit
\n";

//(*)

return take_check_Choice();//take_check_Choice() will execute and return an in-range integer


}

Qatar University
College of Engineering.

Computer Programming Lab (GENG106)

//-------------------------------------------------------------void input_numbers()
{
//user input for num1 and num2
cout<<"\nEnter the 1st number : ";
cin>>num1;
cout<<"Enter the 2nd number : ";
cin>>num2;
}

//-------------------------------------------------------------long int addition()


{
input_numbers();
long int
sum = num1 + num2 ;
return sum;
}

//-------------------------------------------------------------long int subtraction()


{
input_numbers();
long int
difference = num1 - num2 ;
return difference;
}

//-------------------------------------------------------------long int multiplication()


{
input_numbers();
long int product = num1 * num2 ;
return product;
}

//-------------------------------------------------------------long double division()


{
do
{
input_numbers();
if (num2 == 0)
cout<<"division by zero is not allowed\n";
}while(num2==0);
long double quotient = num1*1.0 / num2 ;
return quotient;
}

Qatar University
College of Engineering.

Computer Programming Lab (GENG106)

//-------------------------------------------------------------int main()
{
cout<<"Welcome to arithmetic operations calculator\n";
do{
switch (showChoice())
{
case 1 :
cout<<"\n
case 2 :
cout<<"\n
case 3 :
cout<<"\n
case 4 :
cout<<"\n
case 5 :
exit(0);
}
}while(choice != 5);
system("pause");
return 0;
}

// showChoice() will be executed (*)


// and return integer to the switch
The sum is: "

<<addition()

<<"\n\n"; break;

The difference is: "<<subtraction()

<<"\n\n"; break;

The product is: "

<<"\n\n"; break;

<<multiplication()

The quotient is: " <<division()

<<"\n\n"; break;

//menu will repeat till the user exit

Potrebbero piacerti anche