Research Area

Ashutosh Dubey

Tuesday, May 11, 2010

Array IInd Tutorial

6. WAP to implement BUBBLE SORT algorithm.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
cout<<" \t\t\t\tLaxman"<<endl;
int a[100],i,j,temp,n;
cout<<"Enter the no. of elements in the array"<<endl;
cin>>n;
cout<<"Enter the elements"<<endl;
for(i=0;i<n;i++)
{
cin>>a[i];
}
for(i=0;i<n;i++)
{
for(j=0;j<n-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
cout<<"Sorted list is\n";
for(i=0;i<n;i++)
cout<<a[i]<<"\t";
getch();
}

7. WAP to implement SELECTION SORT algorithm.

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
cout<<" \t\t\t\tLaxman"<<endl;
int a[100],i,j,temp,n;
cout<<"Enter the no. of elements in the array"<<endl;
cin>>n;
cout<<"Enter the elements"<<endl;
for(i=0;i<n;i++)
{
cin>>a[i];
}
for(i=0;i<n;i++)
{
for(j=i+1;j<=n;j++)
{

if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}}}
cout<<"Sorted list is\n";
for(i=0;i<n;i++)
cout<<a[i]<<"\t";
getch();
}
8. WAP to accept a string and print the character separately line by line.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
char x[10],i,j,t;
cout<<"\t\t\t\tLaxman"<<endl;
cout<<"Enter the string :\n";
cin>>x;
cout<<"verticl string:\n";
for(i=0;x[i]!='\0';i++)
cout<<x[i]<<endl;
getch();
}
9.WAP   accept a string and convert uppercase to lowercase and lowercase to uppercase.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
char x[10],i,j,t;
cout<<"\t\t\t\tLaxman"<<endl;
cout<<"Enter the string "<<endl;
cin>>x;
cout<<"After changing:"<<endl;
for(i=0;x[i]!='\0';i++)
{
if(x[i]>=65&&x[i]<=90)
x[i]+=32;
else if(x[i]>=97&&x[i]<=122)
x[i]-=32;
}
for(i=0;x[i]!='\0';i++)
cout<<x[i];
getch();
}

10. WAP to declare a 2D array of 3*4.accept the value from the user and print them on the screen.

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int x[3][4],i,j;
cout<<"\t\t\t\tLaxman"<<endl;
cout<<"Enter the elements"<<endl;
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
cin>>x[i][j];
}
cout<<"the matrix is"<<endl;
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
cout<<x[i][j]<<" ";
cout<<endl;
}
getch();

}

No comments: