Part 2: Write a Program Whose Input Is Two Integers and Whose Output Is the Two Integers Swapped.

Functions in C++

A function is a code module that performs a single task. Some examples such as sorting, search for a given item, and invert a square matrix. Once a function is created it is tested extensively. After this, it becomes a part of the library of functions. A user can use such a library function as many times as needed. This idea improves software robustness and also reduced code development time. Functions are classified into two categories: system defined, and user defined. Examples for a system defined math library functions are: sqrt, exp, and log. In this course, we only focus our attention to user defined functions

User defined functions

As the name suggests these functions are defined and created by users. Functions in C++ agree with the notion of functions in mathematics. For example, they allow functional composition such as f(f(p),q,f(f)). However, C++ functions either return one value or no value. Consider the following example. This is a user defined function that computes the value of factorial of a small integer.

            // This the factorial function int fact(int n){  //This function maps input integer n to another  int ans =1;	// ans is the local variable of the function for(int i= 2; i<= n; i++)	// i also a local scratch pad variable ans *=i; return ans;	   // function returns the answer to the caller }        

The integer variable n is the input to the function and it is also called the parameter of the function.

If a function is defined after the main() function then its prototype must be specified int fact (int);

at the top in order to avoid forward reference error being flagged by the compiler. A prototype definition specifies the name of the function, number and type of each parameter and the data type of the output produced.

All pieces of the code such as a function prototype, a calling main program and the complete function definition are shown in the following

#include <iostream> using namespace std; int fact(int n); // function prototype  int main(){ int nv,ans; 	cout<<" Input n:"; 	cin>> nv; 	ans = fact(nv);// Function is called 	cout<<"Answer is:"<<ans<<endl; } 	 int fact(int n){  // This function maps input int to another int 	int ans =1; // ans is the local variable of the function 	for(int i= 2; i<= n; i++)	// i also a local sractch pad variable 	     ans *=i; 	return ans; // function returns the answer to the caller }        

Typical outputs that result from the execution of this code are shown below:

.

Function examples
Example 1

Write a function in order to determine the maximum of two integers x and y Answer:

//this function finds the bigger number of the two input variables int max2(int x, int y){ int result; 		if ( x > y )//if "x" is bigger than "y" 			result =x;//output x 		else 			result = y;//if not bigger than "y" output "y" 		return result; }        

This function accepts two integers x and y as its input. It chooses x as the result if x is larger than y; otherwise y is chosen for the result.

#include <iostream> using namespace std; int max2(int n,int m); //function prototype int main() {     int x,y,ans; 	cout<<" Input x, y:"; 	cin>> x >> y; 	ans = max2(x, y); // Function is called 	cout<<"Answer is:"<<ans<<endl;     return 0; } int max2(int x, int y){     int result;     if ( x > y ) 		result =x; 	else 		result = y; 	return result; }        
Example 2:

Write a function to find the maximum of the three integers x, y and z. Answer:

int max3(int x, int y, int z){ int result=x; 	if(result < y) //if x is smaller than y 		result = y; //output y 	if (result < z) //if x is smaller than z 		result= z; //output z     else 	return result; }        

To start with x is assumed to be the result. Then the result is compared against y and z. if y or z is larger than the result y or z is chosen for the result respectively.

#include <iostream> using namespace std;  int max3(int n,int m,int o); //function prototype  int main() {     int x,y,z,ans; 	cout<<" Input x, y, z:"; 	cin>> x >> y >> z; 	ans = max3(x, y, z); // Function is called 	cout<<"Answer is: "<<ans<<endl;      return 0; }  int max3(int x, int y, int z){ int result=x; 	if(result < y) //if x is smaller then y 		result = y; //output y 	if (result < z) //if x is smaller then z 		result= z; //output z     else  	return result; }        

The same function can composed around max2 function and this is shown below:

int max3(int x, int y, int z){ int result; result=max2(x,max2(y,z)); //this shows the max2 function  						 //inside the max 3 function 	return result; }        

This calculation shows the use of functional composition that is max(x, y, z) = max(x, max(y,z))

#include  <iostream> using namespace std;  int max2(int x,int y); int max3(int n,int m,int o); //function prototype  int main() {     int x,y,z,ans; 	cout < <" Input x, y, z:"; 	cin>> x >> y >> z; 	ans = max3(x, y, z); // Function is called 	cout < <"Answer is: " < <ans < <endl;      return 0; } int max2(int x, int y){ int result; 		if ( x > y ) //if "x" is bigger than "y" 			result =x; //output x 		else 			result = y; //if not bigger than "y" output "y" 		return result; }  int max3(int x, int y, int z){ int result; 	result=max2(x,max2(y,z)); 	return result; }        
Example 3

Write a function that will return how many three digit natural numbers are divisible by either 4 or 5 but not both.

int div45(){ int count, i;//initialize the count and the pointer     for(i=100,count =0; i < 1000; i++) //this goes through all the #'s   //that are 3 digits 		if ((i % 4 == 0 && i % 5 !=0 ) || 		   (i % 4 != 0 && i % 5 == 0)) //to prevent double counting 							   //i%5 == 0 and i%4 == 0 finds 							   //the numbers divisible by 4 							   //or 5 		   count++; 	return count; }        

Answer: this function first goes through all the possibilities for 3 digit numbers first, then it checks if that current "i" value is divisible by 4 OR 5 while at the same time checking that it is not double counting the other number.

Example 4

Write a function that will return true if the given number n is a prime, otherwise it must return false.

bool isPrime(int n){ int i; 	for(i=2; i < n/2; i++) 		if ( n% i == 0) 			return false; 	return true; }        

Answer: This function checks for all divisors from 2 up to and including n/2 -1. It will exit beforehand if it finds a divisor before completion of the loop. If the code completes the loop means the number does not have a divisor and therefore it is a prime number.

Example 5

Write a function that will return true if the given number is a palindrome otherwise it must return false. A number is said to be a palindrome if it reads the same whether it is scanned from left to right or vice-versa. For example, 506605 is a palindrome while 5123 is not.

bool isPalind(int n){ int nr=0; int org=n; 	while ( n != 0){ 		nr = 10 * nr + n % 10; 		n /= 10; 	} 	return ( org == nr); }        

Answer: The logic behind a palindrome is to reverse the given number n. It is done by the while loop. The given number n is a palindrome if and only if the original n and its reversal are identical.

.

Part 2: Write a Program Whose Input Is Two Integers and Whose Output Is the Two Integers Swapped.

Source: https://www.cpp.edu/~elab/ECE114/Functions.html

0 Response to "Part 2: Write a Program Whose Input Is Two Integers and Whose Output Is the Two Integers Swapped."

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel