Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
117 views

Function Lab

The document contains examples of C++ code demonstrating the use of functions, arrays, and multi-dimensional arrays. It defines functions for mathematical operations like addition, subtraction etc. and calls them in a menu-driven program. It also shows passing arguments by value and reference. Examples of one and two-dimensional arrays are provided to store and display sales data by station and month.

Uploaded by

etayhailu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
117 views

Function Lab

The document contains examples of C++ code demonstrating the use of functions, arrays, and multi-dimensional arrays. It defines functions for mathematical operations like addition, subtraction etc. and calls them in a menu-driven program. It also shows passing arguments by value and reference. Examples of one and two-dimensional arrays are provided to store and display sales data by station and month.

Uploaded by

etayhailu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Practice 1

#include<iostream>
#include<iomanip>
#include<math.h>
using namespace std;

double Add(double a, double b);


double Sub(double a, double b);
double Mult(double a, double b);
double Div(double a, double b);
double Power(double a, double b);
int Factorial (unsigned int n);
inline int abs (int n)
{
return n > 0 ? n : -n;
}

double Add(double a, double b)


{
double answer = a + b;
return answer;
}

double Sub(double a, double b)


{
double answer = a - b;
return answer;
}

double Mult(double a, double b)


{
double answer = a * b;
return answer;
}

double Div(double a, double b)


{
double answer = a / b;
return answer;
}

double Power(double a, double b)


{

double answer = pow(a,b);


return answer;
}
int Factorial (unsigned int n)
{
return n == 0 ? 1 : n * Factorial(n-1);
}

int main ()
{
double num1,num2;
int num3,num4;
int choice;

cout << "Select the operation you would like to use" << endl;

cout << "1. Addition\n";


cout << "2. Subtraction\n";
cout << "3. Multiplication\n";
cout << "4. Division\n";
cout << "5. Power\n";
cout << "6. Absolute value\n";
cout << "7. Factorial\n";
cin >> choice;
if(choice<6)
{
cout << "Please enter the first number\n";
cin >> num1;
cout << "Please enter the second number\n";
cin >> num2;
}

switch(choice)
{
case 1:

cout<<"the sum of "<<num1<<" and "<< num2<<" is: "<<Add(num1,num2);


break;

case 2:
cout<<"the Difference of "<<num1<<" and "<< num2<<" is: "<<Sub(num1,num2);
break;

case 3:
cout<<"the multiplication of "<<num1<<" and "<< num2<<" is: "<< Mult(num1,num2);
break;

case 4:
cout<<"the Difference of "<<num1<<" and "<< num2<<" is: "<< Div(num1,num2);
break;

case 5:
cout<<"the power of "<<num1<<" and "<< num2<<" is: "<< Power(num1,num2);
break;

case 6:
{
cout<<"enter a number to do absolute value";
cin>>num3;
cout<<"the absolute value of "<<num3 <<" is: "<< abs(num3);
break;
}
case 7:
{
cout<<"enter the number for Factorial";
cin>>num4;
cout<<"the Factorial of "<<num4 <<" is: "<< Factorial(num4);
break;
}
default:
cout << "Please select a valid operation" << endl;
}
}

Practice 2

#include <iostream.h>
void Foo (int num);

int main (void)


{
int x = 10;
Foo(x);
cout << "x = " << x << '\n';
return 0;
}
void Foo (int num)
{
num = 0;
cout << "num = " << num << '\n';
}
Practice 3
#include <iostream.h>
void Foo (int& num);

int main (void)


{
int x = 10;
Foo(x);
cout << "x = " << x << '\n';
return 0;
}

void Foo (int& num)


{
num = 0;
cout << "num = " << num << '\n';
}

Practice 4
# include<iostream.h>
int devide (int a, int b=2);
int devide(int z, int r, int y);
float devide (float a, float b);

int main()
{
int x=20, y=2;
float n=5.0, m=2.0;
cout<<devide(x,y);
cout<<endl;
cout<<devide(n,m);
cout<<endl;
cout<<devide(n,m,m);
cout<<endl;
return 0;
}
int devide (int a, int b)
{
return a/b;
}
int devide (int a, int b, int c)
{
int w=a/b
return w/c;
}
float devide (float x, float y)
{
return x/y;
Practice 5
#include <iostream>
using namespace std;
int main()
{
// Declare the members of the array
int numbers[] = {8, 25, 36, 44, 52, 60, 75,
89,10,12,42,96,50,76,23,91,18,49,78,58,57,53,71,84,70,40,31,81,77,55,88};
int find;
int i, m = 31;
cout << "Enter a number to search: ";
cin >> find;
for (i = 0; (i < m) && (numbers[i] != find); ++i)
continue;
// Find whether the number typed is a member of the array
if (i == m)
cout << find << " is not in the list" << endl;
else
cout << find << " is the " << i + 1
<< "th element in the list" << endl;
return 0;
}
Practice 6
#include<iostream>
#include<conio.h>
using namespace std;
int main ( )
{
float sales[5][3];
for(int s=0; s<5; s++)
for (int m=0; m<3; m++)
{
cout <<"Enter sales for station"<<s+1;
cout<<", month "<<m+1<<": ";
cin>>sales[s][m];
}
cout<<"\n\n";
for (int j=0; j<=4; j++)
for (int k=0; k<=2; k++)
{
cout<<"Sales"<<j+1 <<", month"<<k+1;
cout<<" "<<sales[j][k] << " ";
if (k==2)
cout<<"\n";
}
getch( );
return 0;
}

You might also like