"The best way to cheer yourself up is to try to cheer somebody else up." Mark Twain
Showing posts with label functions(User Defined). Show all posts
Showing posts with label functions(User Defined). Show all posts

Thursday, February 25, 2010

Write a C++ program that uses a function smallo() (that is passed two int argument by value) to receive reference of the smaller value. Then using this reference the smaller value is set to 0. Write a main() function also to exercise this function.

/* This is a example of return by reference */ 


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

int &smallo(int a, int b)
{
if(a>b)
  return a;
else
  return b;
}


void main()
{
int a, b;
cout<< "\n Enter two numbers :";
cin >> a >> b;

cout<< "\n The original values :" << a << "  and " << b;

smallo(a, b) = 0;

cout<< "\n The changed values :" << a << " and " << b;



getch();
}

Sunday, February 21, 2010

Write a C++ program to use following functions:

(i) sqlarge() that is passed two int arguments by reference and then sets the larger of the two numbers to its square.



(ii) sum() that is passed an int argument by value  and that returns the sum of the individual digits of the passed number.



(iii) main() that exercises above two functions by getting two integers from the user and by printing the sum of the individual digits of the square of the larger number.



#include<iostream.h>
#include<conio.h>
void sqlarge(int &a, int &b)
{
if(a >b)
   a = a*a;
else
   b = b*b;
}


int sum(int x);
{
int r, s=0;
while(x > 0)
{
r = x % 10;
s = s+r;
x = x / 10;
}


return s;
}




void main()
{
int num1, num2;


cout<<" \ n Enter a number :";


int tot = sum(num1);


cout<< " \n THE SUM OF DIGITS = " << tot;


cout << "\n Enter another number : ";
cin>> num2;


cout<< "\n The two numbers originally are " << num1 << " and " << num2 ;


sqlarge(num1, num2);


cout<< "\n The two numbers after change are " << num1 << " and " << num2 ;


getch();


}

Do the following :


Write a complete C++ program that invokes a function satis() to find whether four integers a,b,c,d sent to satis() satisfy the equation a3+b3+c3 = d3 or not. The function satis() returns 0 if the above equation is satisfied with the given four numbers otherwise it returns -1



#include<iostream.h>
#include<conio.h>
int satis(int a, int b, int c, int d)
{
if( a*a*a + b*b*b + c*c*c == d*d*d)
return 0;
else
return -1;
}
void main()
{
cout<< "\n Enter 4 integers : ";
int x, y, z, w;
int s = satis( x,y,z,w);
if(s == 0)
cout<< "\n The equation is satisfied";
else
cout<< "\n The equation is NOT satisfied";
getch();
}

Write a program that uses a function power() to raise a number m to power n. The function takes int values for m and n and returns the result correctly. Use a default value of 2 for n to make the function calculate square of m when the value for n is missing while calling


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


double power(int m, int n = 2)
{
double res = pow(m,n);
return res;
}


void main()
{
int x;

cout<< "\n Enter a number to calculate its square :";
double d = power(x);
cout<< "\n The square of " << x <<"is " << d;


cout<< "\n Enter a number to calculate its cube :";

 d = power(x,3);
cout<< "\n The cube of " << x <<"is " << d;



int y;
cout<< "\n Enter a number  and its power to calculate the result :";
cin>>x>>y;

d = power(x,y);
cout<< "\n The " << x << " to the power " << y <<" is " << d;
getch();
}

Saturday, February 20, 2010

Write a C++ program to use the following function :

(i) display() to display a matrix of a size m x n.
(ii) times2() to double each number of the matrix of size mxn.
(iii) main() to read a matrix of size mxn and then to display original matrix and then to display the new matrix formed by doubling its elements.


#include<iostream.h>
#include<conio.h>
void display(int a[][], int m, int n)
{
for(int i = 0; i< m ; i++)
 {
  for( int j = 0; j< n ; j++)
        cout << a[i][j] << "  ";
   cout<< endl;
 }
}

void times2(int a[][], int m, int n)
{
for(int i = 0; i< m ; i++)

  for( int j = 0; j< n ; j++)
     a[i][j] = a[i][j] * 2;


}


void main()
{
int mat[10][10], row, col, i, j;

cout<< "\n Enter total rows :";
cin>> row;

cout<<" \n Enter total columns :";
cin>> col;

cout<< "\n Enter the elements for Matrix:";

for(i = 0; i< row; i++)

  for(j = 0; j< col; j++)
     cin>>mat[i][j];


cout<<"\n The Original Matrix :";
display(mat, row, col);


times2(mat, row, col);


cout<<"\n The New Matrix :";


display(mat, row, col);


getch();
}

Write a complete C++ program that reads a float array having 15 elements. The Program uses a function reverse() to reverse this array. Make suitable assumption.

/* This example is also demonstrating function with call by reference  */
/* Whenever you pass an array through function, it will be done only by reference */


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

void rev(float a[ ], int n)
{
float t;
for(int i =0, k = n-1; i< n/2 ; i++, k--)
{
t = a[i];
a[i] = a[k];
a[k] = t;
}
}


void main()
{
float arr[15];

cout<< "\n Enter 15 real numbers :";
for(int i = 0; i< 15 ; i++)
cin>> arr[i];

clrscr();
cout<<"\n The original array : \n";
for( i = 0; i< 15; i++)
cout<< arr[i] << "  ";

cout<<"\n";

rev(arr, 15);

cout<<"\n The reversed array : \n";

for( i = 0; i< 15; i++)
 cout<< arr[i] << " ";

getch();
}

Write a complete C++ program that uses a function called carea() to calculate area of a circle. The function carea() receives radius of float type and returns area of double type. The function main() gets a radius value from the user, calls carea(), and displays the result. The function carea() is local to main.


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


void main()
{
double carea(float r)
{
double ar = (double) ( 3.14 * r * r );
return ar;
}


float rad;


cout<<  " \n Enter Radius :";
cin >>rad;


double area = carea(rad);


cout<<"\n The area of the circle of radius " << rad << " unit is " << area << "sq. unit ";


getch();


}

Write a function that checks whether or not two arrays (of characters ) are identical, that is, whether they have same characters and all characters in corrosponding positions are equal.


#include<iostream.h>
#include<conio.h>
#include <stdio.h>
#include <string.h>


void main()
{
char sen1[50], sen2[50];
void chk_eq(char str1[], char str2[]);
cout<< "\n Enter String1 :";
gets(sen1);


cout<<"\n Enter String 2:";
gets(sen2);


chk_eq(sen1, sen2);


}


void chk_eq(char str1[], char str2[])
{
int l1,l2;


l1 = strlen(str1);
l2 = strlen(str2);


if(l1 == l2)
{
cout<<" \ n Two String are NOT equal ";
getch();
exit(1);
}


for( int i = 0; i<= l1 - 1 ; i++)
{
  if(str1[i] ! = str2[i])
   {


     cout<<" \ n Two String are NOT equal ";
     getch();
     exit(1);
   }
}


cout<< "\n Two Strings are EQUAL lengthwise , characterwise and casewise ";
getch();
}

Wednesday, February 10, 2010

Write a program with a function to receive an integer array, its size and a character '+' or '-'. By default, the character should be '+'. For the character '+', the function returns the sum of positive numbers stored in the array and for the character '-', the function returns the sum of the negative numbers in the array.


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

int funct(int a[], int n, char s = '+')
{
int sum = 0;
for(int i = 0; i< n ; i++ )
{
if(s == '+')
 {
if(a[i] > 0 )
sum + = a[i];
}
else
{
if(a[i]<0)
sum+=a[i];
}
}
return sum;
}

void main()
{
int arr[20], dn;
cout<<"\n Enter dimension :";
cin>> dn;

for(int i = 0; i< dn ; i++)
{
cout<<"\n Enter any interger (positive / negetive )";
cin>> arr[i];
}

/* ... Sum of all positive integers in the array ... */
int s = funct( arr, dn);

/* .... Sum of all negative integers ..*/
int s2 = funct(arr, dn, '-');

cout<< "\n The sum of the positive integers : " << s;
cout<< "\n The sum of all negative integers : " << s2;

getch();
}
C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off.
Now Playing: Ballade Pour Adeline

About Me

My photo
I m an IT lecturer of a college. I love social-work. I want to do something beneficial for society before dying , that can promote our society, to some extent.