The document contains 6 programs written in C++ to perform various tasks involving arrays and conditional operators:
1) Three programs to swap two values using a third variable, without a third variable, and by adding and subtracting values.
2) Two programs to find the maximum and minimum of 10 values stored in an array.
3) A program to find the largest of two values using a conditional operator.
4) A program to find the smallest of three values using a conditional operator.
1. 1. Program to swap two values using third variable.
2. Program to swap two values without using third variable.
3. Program to find the maximum of 10 values stored in array.
4. Program to find the smallest of 10 values stored in array.
5. Program to find the largest of two values using conditional
operator.
6.Program to find the smallest of three values using conditional
operator.
2. #include<iostream.h>
#include<conio.h>
void main()
{
int a,b,temp; OUTPUT
clrscr(); Enter value of a: 45
cout<<“Enter value of a:”; Enter value of b: 56
cin>>a; Original value of a is:45
cout<<“Enter value of b:”; Original value of b is:56
cin>>b; After Swapping
cout<<"Original value of a is:"<<a<<endl; New value of a is:56
cout<<"Original value of b is:"<<b<<endl; New value of b is:45
temp=a;
a=b;
b=temp;
cout<<“After Swapping..”<<endl;
cout<<“New value of a is:"<<a<<endl;
cout<<“New value of b is:"<<b<<endl;
getch();
}
3. #include<iostream.h>
#include<conio.h>
class swapping
{
public:
int anew,bnew,temp;
void swap( int anew, int bnew) //function receives arguments via object of swapping class
{
temp=anew;
anew=bnew;
bnew=temp; Output
cout<<“New value of a is:"<<anew<<endl; Value of a is: 10
cout<<“New value of b is:"<<bnew<<endl;
} Value of b is: 15
}; After swapping
void main()
{ New Value of a is: 15
int a=10,b=15; //we have set the values of a and b as 10 ,15, it can be user defined too. New value of b is: 10
clrscr();
swapping s1; //created object of swapping class
cout<<“Value of a is:"<<a<<endl;
cout<<“Value of b is:"<<b<<endl;
cout<<“After Swapping”<<endl;
s1.swap(a,b); //called swap function using object and dot operator
getch();
}
4. #include<iostream.h>
#include<conio.h>
void main()
{
int a, b; OUTPUT
clrscr(); Enter value of a: 45
cout<<“Enter value of a:”; Enter value of b: 56
cin>>a; Original value of a is:45
cout<<“Enter value of b:”; Original value of b is:56
cin>>b; After Swapping
cout<<“Value of a is:"<<a<<endl; New value of a is:56
cout<<“Value of b is:"<<b<<endl; New value of b is:45
a=a+b;;
b=a-b;
a=a-b;
cout<<“After Swapping..”<<endl;
cout<<“New value of a is:"<<a<<endl;
cout<<“New value of b is:"<<b<<endl;
getch();
}
5. 2.Program to swap two values without using third variable.
#include<iostream.h>
#include<conio.h>
class swapping
{
public:
int anew,bnew;
void swap( int anew, int bnew);
};
void swapping :: swap(int anew , int bnew)
{
anew= anew+bnew; OUTPUT
bnew=anew-bnew; Enter value of a: 25
anew=anew-bnew; Enter value of b: 65
cout<<"new value of a is:"<<anew<<endl; Value of a is: 25
cout<<"new value of b is:"<<bnew<<endl;
Value of b is:65
}
void main() After swapping
{ Value of a is: 65
int a,b; Value of b is: 25
clrscr();
swapping s1,s2;
cout<<"Enter value of a:";
cin>>a;
cout<<"Enter value of b:";
cin>>b;
cout<<“Value of a is:"<<a<<endl;
cout<<“Value of b is:"<<b<<endl;
s1.swap(a,b);
getch();
}
6. 3.Program to find maximum of 10 values stored in array.
#include<iostream.h>
#include<conio.h>
void main()
{
int a[10],i,j,m,loc=0;
OUTPUT
clrscr(); Enter 10 elements of array:
cout<<"enter 10 elements of array:"; 5
for(i=0;i<=9;i++) 8
{
cin>>a[i]; 2
} 12
m=a[0]; 65
for(j=1;j<=9;j++)
{
36
if(a[j]>m) 98
{ 45
m=a[j]; 25
loc=j+1;
} 96
}
cout<<"max value is:"<<m; Max value is: 98
cout<<"its loc is:"<<loc;
getch();
Its location is: 7
}
7. 3.Program to find maximum of 10 values stored in array.
#include<iostream.h> OUTPUT
#include<conio.h>
class greatest
{
public:
int a[10],j,max;
int largest() //member func of greatest class that returns a value of integer type
{
cout<<"enter 10 elements of array:";
Enter 10 elements of array:
for(int i=0;i<=9;i++) 5
{
cin>>a[i]; 8
}
max=a[0]; 2
for(j=1;j<=9;j++)
{ 12
if(a[j]>max)
{
65
max=a[j]; 36
}
} 98
return max;
} 45
};
void main() 25
{
int max1;
96
clrscr();
greatest g1;
max1=g1.largest(); Max value is: 98
cout<<"Greatest of ten values is:"<<max1;
getch();
}
8. 4.Program to find smallest of 10 values stored in an array.
#include<iostream.h>
#include<conio.h>
void main()
{
int min,a[10],I; OUTPUT
clrscr(); Enter 10 elements of array:
cout<<“Enter 10 elements of array.”; 5
for(i=0;i<=9;i++) 8
cin>>a[i]; 2
12
for(j=1;j<=9;j++) 65
{ 36
If(a[j]<min) 98
min=a[j]; 45
} 25
cout<<“smallest of 10 values is:”<<min; 96
getch();
Smallest of ten values is: 2
}
9. 4.program to find smallest of 10 values stored in an array.
#include<iostream.h>
#include<conio.h>
class smallest
{
public:
int a[10],j,min; OUTPUT
int calculate_smallest()
{ Enter 10 elements of array:
cout<<"enter 10 elements of array:";
for(int i=0;i<=9;i++) -5
{
cin>>a[i]; 8
}
min=a[0]; //0th element is set as minimum values
2
for(j=1;j<=9;j++) 12
{
if(a[j]<min) 65
{
min=a[j]; 36
}
} 98
}
return min; 45
}; 25
void main()
{ 96
int min1;
clrscr();
smallest s1;
min1=s1.calculate_smallest();
Smallest of ten values is: -5
cout<<“Smallest of ten values is:"<<min1;
getch();
}
10. 5.Program to find largest of two values using conditional operator.
#include<iostream.h>
#include<conio.h>
void main()
{
int a , b, c; OUTPUT
Enter value of a: 56
clrscr(); Enter value of b: 36
cout<<“Enter value of a:”; 56 is greatest.
cin>>a;
cout<<“ Enter value of b:”;
cin>>b;
c=a>b?a:b; //using conditional operator, c stores the
biggest of two values.
cout<<a<<“ is greatest”;
getch();
}
11. 5.Program to find greatest of two values using conditional
operator.
#include<iostream.h>
#include<conio.h>
class comparison
{
public:
int a1,b1,max;
int greatest (int a1,int b1)
{
max=a1>b1?a1:b1; //using conditional(ternary operator) to compare a and b, storing result in max.
return max;
}
};
void main()
{
int a, b;
clrscr(); OUTPUT
cout<<"enter a:";
cin>>a;
cout<<"enter b:";
cin>>b; Enter value of a: 62
comparison c1; Enter value of b: 36
cout<<"Greatest of two values is:"<<c1.greatest(a,b);
getch();
Greatest of two values is:62
}
12. 6.Program to find smallest of three values using ternary operator.
#include<iostream.h>
#include<conio.h>
void main()
{ OUTPUT
int a , b, c, max; Enter value of a: 96
clrscr(); Enter value of b: 125
cout<<"Enter value of a:"; Enter value of c: 36
cin>>a; 36 is greatest
cout<<" Enter value of b:";
cin>>b;
cout<<"Enter value of c:";
cin>>c;
max=a<b?(a<c?a:c):(b<c?b:c); //using conditional
operator, max stores the biggest of three values.
cout<<max<<" is smallest";
getch();
}
Editor's Notes
This is a program to swap two values using the third variable(temp). It does not use the class concept.The next program is same but it is done using class concept.
This is same program as the previous one, but it use class structre.
It is a program to swap two values without using third variable and without using class structure