Download C++ Code Snippets: Functions and Calculations and more Assignments Mathematics in PDF only on Docsity!
Computer Fundamentals and
Programming
Assignment 4
Submitted By: Riyan Ali Alvi
Department: Electrical Engineering
Program and Session: BS-EE- 20 - 24
Registration Number: BS- 20 - LB- 103921
Submitted To : Dr.Muhammad Tufail
DATED: 01 / 01 / 2021
PAKISTAN INSTITUTE OF ENGINEERING AND APPLIED SCIENCES
NILORE, ISLAMABAD
1. Computing Cube using function: Write a function called ComputeCube( ) that
computes the cube of a number.
Code
#include using namespace std; // Function Decleration int ComputeCube(int x); int main() { int x; cout<<"Cube of an integer\n"; cout<<"Enter a number: "; cin>>x; //Function Call cout<<"Cube of "<<x<<" ="<<ComputeCube(x); return 0; } //Function Definition int ComputeCube(int x) { return xxx; }
Output:
3. Max and Min using function: Write a C++ program to find maximum and
minimum between two numbers using functions.
Code
#include using namespace std; void maxmin(int x,int y); int main() { int x,y; cout<<"Enter Number 1: "; cin>>x; cout<<"Enter Number 2: "; cin>>y; maxmin(x,y); return 0; } void maxmin(int num1,int num2) { if (num1==num2) cout<<num1<<" and "<<num2<<" are equal."; else if(num1>num2) cout<<num1<<" is greater than "<<num2; else cout<<num2<<" is greater than "<<num1;
return; }
Output:
4. Division using function: Write a C++ program to divide two numbers using
function. Your program should be able to display the remainder if one number is
not completely divisible by the other.
Code
#include using namespace std; int i; void divd(int,int); int main() { int x,y; cout<<"Enter Dividend: "; cin>>x; cout<<"Enter Divisor: "; cin>>y; divd(x,y); return 0; } void divd(int dividend,int divisor) {
void table(int num,int mul) { for (i=1;i<=mul;++i) cout<<num<<" × "<<i<<" = "<<num*i<<endl; return; }
Output:
6. Even( ) and Odd( ): Write a C++ program that tells whether a number in even
or odd. Make use of two function even ( ) and odd ( ). Make logic in such a way
that your even function calls odd function if the given number is not even and
vice versa.
Code
#include using namespace std; void even(int); void odd(int); int main() { int x;
while (1) { cout<<"Please, enter number ( 0 to exit ): "; cin>>x; if (x==0) break; odd(x); } cout<<"Program Terminated!"; return 0; } void even(int num) { if (num>0 && num!=0) { if (num%2 == 0) { cout<<num<<" is even.\n\n"; return; } else odd(num); return; } else cout<<"Number is negative.\n\n"; } void odd(int num) { if (num>0 && num!=0) { if (num%2 != 0) { cout<<num<<" is odd.\n\n";
cout<<"Case converter of Letters (Press 0 to exit)\n\n"; while (1) { cout<<"Enter a Letter: "; cin>>c; if (c==48) break; else function(c); } cout<<"Program Terminated!"; return 0; } void function(char letter) { if( letter>=65 && letter<=90) cout<<"In Lower Case: "<<char(letter+32)<<"\n\n"; else if( letter>=97 && letter<=122) cout<<"In Upper Case: "<<char(letter-32)<<"\n\n"; else cout<<"Wrong input!"; return; }
Output
8. Print Series: Write a C++ function printSeries( ) to print the following series
for the integer x upto the n th term
Code
#include #include using namespace std; void printSeries(int,int); int main() { int x,n; cout<<"Enter x: "; cin>>x; cout<<"Enter number of Terms: "; cin>>n; printSeries(x,n); return 0; } void printSeries(int num,int term) { int i=1,sum = 0; cout<<num; while(i<term) { cout<<" + "; ++i; sum =pow(num,i);
static int i=0; ++i; int q=num/7; if (q==0) return i; else if ( q>=7 ) div7(q); else return i; }
Output:
10. Geometry: Write two separate functions using pass by value and pass by
reference to calculate each of the following…..
a. area of a circle
b. circumference of a circle
c. area of a rectangle
d. area of a triangle
e. volume of a sphere (4/3Pi r3 )
Code
#include using namespace std; float Acircle(float);
float acircle(float,float&); float Atri(float,float); float atri(float,float,float&); float Arec(float,float); float arec(float,float,float&); float Circum(float); float circum(float,float&); float Vsphere(float); float vsphere(float,float&); const float pi = 3.14; int main() { float areac,atrian,arect,circ,vsphe,rad,base,alt,len,wid; cout<<"Enter Radius: "; cin>>rad; cout<<"Area of Circle(Pass by Value): "<<Acircle(rad)<<endl; acircle(rad,areac); cout<<"Area of Circle(Pass by Reference): "<<areac<<endl; cout<<"Circumference of Circle(Pass by Value): "<<Circum(rad)<<endl; circum(rad,circ); cout<<"Circumference of Circle(Pass by Reference): "<<circ<<endl; cout<<"Volume of Sphere(Pass by Value): "<<Vsphere(rad)<<endl; vsphere(rad,vsphe); cout<<"Volume of Sphere(Pass by Reference): "<<vsphe<<endl<<endl; cout<<"Enter Length: "; cin>>len; cout<<"Enter Width: "; cin>>wid; cout<<"Area of Rectangle(Pass by Value): "<<Arec(len,wid)<<endl; arec(len,wid,arect); cout<<"Area of Rectangle(Pass by Reference): "<<arect<<endl<<endl;
float Circum(float radius) { return 2piradius; } float circum(float radius,float& circum) { circum = 2piradius; return 2piradius; } float Vsphere(float radius) { return (piradiusradiusradius4)/3; } float vsphere(float radius,float& sphere) { sphere = (piradiusradiusradius4)/3; return (piradiusradiusradius4)/3; }
Output:
11. Area of a garden: Write a program which a builder will use to calculate the
area of garden left on a site after building a house. The house is a rectangle, as is
the site. You must subtract the area of the house from the area of the site.
Make sure that the house fits into the site, check the lengths and sides of each.
Code
#include using namespace std; float Arec(float,float); int main() { float len,lenh,wid,widh; cout<<"Enter Length of Site: "; cin>>len; cout<<"Enter Width of Site: "; cin>>wid; cout<<"Enter Length of House: "; cin>>lenh; cout<<"Enter Width of House: "; cin>>widh; if (len>=lenh && wid>=widh) cout<<"Area of Garden: "<<Arec(len,wid)-Arec(lenh,widh); return 0; } float Arec(float len,float wid) { return len*wid;}
Output:
callf(); } cout<<"I've been called "<<callf()<<" times."; } int callf() { static int count = - 1; return ++count; }
Answer:
In this case either global or local static variable may be used to calculate the calls to the function
since, the only difference between them is their scope. Local static variable starts the count as
soon as the function is called the first time. Hence, in this case either of them may be used.
We can’t use local variable because local variable has automatic storage class and gets destroyed
after the function returns. It gets initialized every time the function is called and cannot be used
to store value of the count in between function calls.
Output:
13. Square and Cube: Write two different versions of function cube and square
in which implement the concept of pass by value and pass by reference.
Code
#include using namespace std; float Cubef(float); float cubef(float,float&); float Squaref(float); float squaref(float,float&);
int main() { float x,cube,square; cout<<"Enter a number: "; cin>>x; cout<<"Cube(Pass by Value): "<<Cubef(x)<<endl; cubef(x,cube); cout<<"Cube(Pass by Reference): "<<cube<<endl; cout<<"Square(Pass by Value): "<<Squaref(x)<<endl; squaref(x,square); cout<<"Square(Pass by Reference): "<<square; return 0; } float Cubef(float num) { return numnumnum; } float cubef(float num,float& cube) { cube = numnumnum; return numnumnum;;} float Squaref(float num) { return numnum;} float squaref(float num,float& square) { square = numnum; return num*num;}
Output: