Programming Exercise

Functions Programming Exercise P-9

Conditional Structure Programming Exercise P-6

Q.36 Write a function, and a program to test it, that will place a zero in each of three variables in the calling program.

```#include<iostream.h>
#include<conio.h>
main()
{
void zero(int&, int&, int&);
int a, b, c;
clrscr();
a = 10;
b = 15;
c = 152;
cout<<"Value of a = "<<a<<endl;
cout<<"Value of b = "<<b<<endl;
cout<<"Value of c = "<<c<<endl;

zero(a,b,c);
cout<<"Values of a, b & c after calling function "<<endl;
cout<<"Value of a = "<<a<<endl;
cout<<"Value of b = "<<b<<endl;
cout<<"Value of c = "<<c<<endl;
getch();
}

// definition of zero() function
void zero(int& x, int& y, int& z)
{
x = y = z = 0;
}```

Q.37 Write a program to find out the weighted average of input numbers by the following formula:// Average = i = N i = 1 fi xi i = N i = 1 fi// Find out the average using function and return the calculated average to the calling function. Suppose the value of N is 6, then input 6 values of f and x.

```#include<iostream.h>
#include<conio.h>
main()
{
double weighted_avg(void);
clrscr();
cout<<"weighted average :"<<weighted_avg();
getch();
}

// definition of weighted_avg() function
double weighted_avg(void)
{
int i, N, f, x, sum_fx = 0, sum_f = 0;
double average = 0;
clrscr();
cout<<"Enter value of N : ";
cin>>N;
for(i = 1;i<=N;i++)
{
cout<<"Enter value of f : ";
cin>>f;
cout<<"Enter value of x : ";
cin>>x;
sum_fx = sum_fx + f*x;
sum_f = sum_f + f;
}
average = sum_fx/sum_f;
return average;
}```

Q.38 Write a program that finds out the factorial of a number using default arguments.

```#include<iostream.h>
#include<conio.h>
main()
{

long fact(int = 3);
clrscr();
cout<<"Factorial of 3 is :"<<fact();
getch();
}

// definition of fact() function
long fact (int n)
{
int f = 1;
while(n!=0)
{
f = f*n;
n--;
}
return f;
}```

Q.39 Write a program that converts the centigrade temperature into Fahrenheit temperature using inline function.

```#include<iostream.h>
#include<conio.h>

inline float c_f_temp(float c)
{
return 9.0/5.0*c+32;;
}

main()
{
float c_temp;
clrscr();
cout<<"Enter temperature in centigrade ? ";
cin>>c_temp;
getch();
}```

Q.40 Write a program by defining functions overloading “sum”, to find the sum of different set of numbers.

```#include<iostream.h>
#include<conio.h>

int sum(int x, int y)
{
return x + y;
}

float sum(float x, float y, float z)
{
return x + y + z;
}

main()
{
clrscr();
cout<<" Sum of 2, 5 is : "<<sum(2, 5)<<endl;
cout<<" Sum of 2.2, 3.3 and 5.6 is : "<<sum(2.2, 3.3, 5.6)<<endl;
getch();
}```

Q.41 Write a program using three versions of an overloaded function line(). The first version takes no parameter and displays a line of 15 asterisks. The second version takes an integer ‘n’ parameter and displays a line of ‘n’ asterisks. The third version takes an integer ‘n’ and a character as parameters and displays a line of given character of ‘n’ length.

```#include<iostream.h>
#include<conio.h>

void line(void);
void line(int);
void line(int, char);
main()
{
clrscr();
line();
line(10);
line(25, '#');
getch();
}

void line(void)
{
for(int i = 1; i<=15; i++)
cout<<"*";
cout<<endl;
}

void line(int n)
{
for(int i = 1; i<=n; i++)
cout<<"*";
cout<<endl;
}

void line(int n, char ch)
{
for(int i = 1; i<=n; i++)
cout<<ch;
cout<<endl;
}```

Q.42 Write a program that uses a function template called “sum” to determine the sum of numbers.

```#include<iostream.h>
#include<conio.h>

template < class N >
N sum (N x, N y, N z )
{
return x + y + z;
}

main()
{
int a, b, c;
clrscr();
cout<<"Enter three integer values ? " <<endl;
cin>>a>>b>>c;
cout<<"Sum of three integer values is: "<<sum(a,b,c)<<endl;
double d1, d2, d3;
cout<<"Enter three real values?" <<endl;
cin>>d1>>d2>>d3;
cout<<"Sum of three real values is:" <<sum(d1,d2,d3)<<endl;
getch();
}```

Q.43 Write a program that uses a function template called “min” to determines the smaller of two numbers. Test the program using integers, characters and real values as arguments.

```#include<iostream.h>
#include<conio.h>

template < class N >
N min (N x, N y )
{
if (x< y)
return x;
else
return y;
}

main()
{
int a, b;
clrscr();
cout<<"Enter two integer values ? " <<endl;
cin>>a>>b;
cout<<"Smaller value is : "<<min(a, b)<<endl;

char ch1, ch2;
cout<<"Enter two characters ? " <<endl;
cin>>ch1>>ch2;
cout<<"Smaller value is : "<<min(ch1, ch2)<<endl;

float r1, r2;
cout<<"Enter two real values ? " <<endl;
cin>>r1>>r2;
cout<<"Smaller value is : "<<min(r1, r2)<<endl;
getch();
}```