Assignment of functions.docx1541910831, Exams of Advanced Computational Complexity

Function help understanding functions

Typology: Exams

2015/2016

Uploaded on 03/23/2016

Ahsan.Abdullah
Ahsan.Abdullah 🇵🇰

1 document

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Important Insructions:
Assignment will be done individually.
Each student will submit this assignment to the relevant lab instructor.
Label the folder with the name of student , reg # and section.
Any kind of cheating will awarded negative 5 marks.
Assignment should be submitted till Friday. after Friday any assignment will not be accepted
………………………………………………………
Lab Manual 03
1. Write a program by defining function overloading. The name of the overloaded function is “square”. The
prototypes of overloaded function are:
void square (void); used to print a solid square of * with side length of 4
void square (char); used to print a solid square of specified character with side length of 8
void square (char, int); used to print a solid square of specified character with specified side length
2. Write a program to find the factorial of a number using reference parameters.
3. Write a program by defining a function “swap” to exchange two values by passing arguments by reference
to the function.
4. Write a program to convert the centigrade temperature into Fahrenheit temperature using Inline function.
5. Write a program that uses a function template called min to determine the smaller of two numbers. Test the
program using integers, characters and real values as arguments.
6. Using Alias of Variable (What does the following piece of code do?)
int x = 5;
int &a = x;
a = a + 100;
void square(int &a)
{
cout<<"a:\t"<<a<<endl;
cout<<"&a\t"<<&a<<endl;
}
void main()
{
clrscr();
int x=5;
cout<<"x\t"<<x<<endl;
cout<<"&x\t"<<&x<<endl;
pf3
pf4

Partial preview of the text

Download Assignment of functions.docx1541910831 and more Exams Advanced Computational Complexity in PDF only on Docsity!

Important Insructions:

• Assignment will be done individually.

• Each student will submit this assignment to the relevant lab instructor.

• Label the folder with the name of student , reg # and section.

• Any kind of cheating will awarded negative 5 marks.

• Assignment should be submitted till Friday. after Friday any assignment will not be accepted

Lab Manual 03

1. Write a program by defining function overloading. The name of the overloaded function is “square”. The

prototypes of overloaded function are:

• void square (void); used to print a solid square of * with side length of 4

• void square (char); used to print a solid square of specified character with side length of 8

• void square (char, int); used to print a solid square of specified character with specified side length

2. Write a program to find the factorial of a number using reference parameters.

3. Write a program by defining a function “swap” to exchange two values by passing arguments by reference

to the function.

4. Write a program to convert the centigrade temperature into Fahrenheit temperature using Inline function.

5. Write a program that uses a function template called min to determine the smaller of two numbers. Test the

program using integers, characters and real values as arguments.

6. Using Alias of Variable (What does the following piece of code do?)

int x = 5; int &a = x; a = a + 100;

void square(int &a) { cout<<"a:\t"<<a<<endl; cout<<"&a\t"<<&a<<endl; } void main() { clrscr(); int x=5; cout<<"x\t"<<x<<endl; cout<<"&x\t"<<&x<<endl;

square(x); getch(); }

7. What does the following piece of code do?

#include <iostream.h>

int a = 10;

int main() { int a = 15;

cout << "main() " << endl; cout << "a = " << a << endl; cout << "::a = " << ::a << endl;

int a = 25; cout << "------------Inner Block------------" << endl; cout << "a = " << a << endl; cout << "::a = " << ::a << endl; }

cout << "-----------Back to main()-----------" << endl; cout << "a = " << a << endl; cout << "::a = " << ::a << endl; }

  1. Default Arguments: Specifying values for the arguments at the time of declaration is called default arguments. By using default arguments, you can call a function without giving the required parameters. If the parameter values are omitted in a function call, then default values are atomically passed to the function definition.

void time(int h=12, int m=56, int s=35); void main () { int a, b; clrscr(); time(); time(11); time(10,33); time(10,45,58); } void time(int h, int m, int s) { Cout<<h<<”:”<<m<<”:”<<s<<endl; }

1. Write a program that accept a number from the user as an input and calculates the square of a given number

using function.

void decToBin(int num, int base)

if(num > 0)

decToBin(num / base, base);

cout << num % base;

void printStars()

cout<<"\n\n***************************\n";

cout<<"***************************\n\n";

void main()

clrscr();

int decimalNum;

int base;

base = 2;

cout<<"\n\n Enter number in Decimal\t";

cin>>decimalNum;

cout<<endl;

printStars();

cout<<"\n\n Decimal " <<" = " << " Binary \n\n" ;

cout<<" " << decimalNum <<"\t ";

decToBin ( decimalNum, base);

printStars();

getch();

7. The expression of computing C (n,r), the number of combinations of n items taken r at a time is

C(n,r) = n! / r! (n-r)!.. Write a recursive function to calculate the C. moreover

C (n,0) = C(n,n) = 1. It is also known that C(n,r) = C(n – 1 , r - 1) + C(n-1 , r)

8. Write recursive GCD function, that finds greatest common divisor of two integers. Mathematical

Rule is given below.

gcd (x,y) = x if y=

gcd(y, x%y) if y≠ 0