Saturday, November 29, 2014

C++ Using array pro



Program to enter three integers and output the smallest integer using IF

#include
#include
int main()
{
clrscr();
int x,y,z,smallest;
cout << "Enter 3 integers : ";
cin>>x>>y>>z;
smallest=x;
if(y
smallest=y;
if(z
smallest=z;
cout << "The smallest integer out of the 3 integers you typed ";
cout << x << ", " << y << " & " << z << " is : " << "\n" << smallest << "\n";
getch();
return 0;
}

Output
 
The smallest integer out of the 3 integers you typed 82, 78 & 86 is :
78

Program to find the sum of either of the diagonals of a 4 x 4 matrix
 
#include 
#include 
void main()
{
clrscr();
int x;
int a[4][4],sum=0; 
cout << "Enter the elements of the matrix : " << endl;
for(int y=0;y<4 span="" y="">
for (int x=0;x<4 span="" x="">
{
cout << "Element " << x+1 << ", " << y+1 << " : ";
cin>>a[x][y];
}
for(x=0;x<4 span="" x="">
for(y=0;y<4 span="" y="">
if(x==y)
sum+=a[x][y];
else if(y==4-(1+1));
sum+=a[x][y];
cout << "Sum of either of the diagonal elements is : " << sum;
getch();
}
 
Output
            
          Sum of either of the diagonal elements is : 34
 
Enter 10 integers in a single dimension array and then print out the array in ascending order
 
#include 
#include 
void main()
{
clrscr();
int array[10],t;
for(int x=0;x<10 span="" x="">
{
cout << "Enter Integer No. " << x+1 << " : " << endl;
cin>>array[x];
}
for (x=0;x<10 span="" x="">
{
for(int y=0;y<9 span="" y="">
{
if(array[y]>array[y+1])
{
t=array[y];
array[y]=array[y+1];
array[y+1]=t;
}
}
}
cout << "Array in ascending order is : ";
for (x=0;x<10 span="" x="">
cout << endl << array[x];
getch();
}
 
Output
 
Array in ascending order is :
6
8
21
43
48
53
67
78
92
95

No comments:

Post a Comment