Showing posts with label array. Show all posts
Showing posts with label array. Show all posts

Wednesday, August 7, 2013

Program to delete an element from an array

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int s,n,i,p,a[50];
clrscr();
cout<<"Enter number of students : ";
cin>>n;
cout<<"Enter the marks : ";
for(i=0;i<n;i++)
cin>>a[i];
cout<<"Enter the marks to be deleted : ";
cin>>s;
for(i=0;i<n;i++)
if(a[i]==s)
    {
     p=i;
     break;
    }
for(i=p;i<n-1;i++)
a[i]=a[i+1];
cout<<"The new mark list is : ";
for(i=0;i<n-1;i++)
cout<<a[i];
getch();
}

Friday, July 26, 2013

Program to insert new element in an array

#include<iostream.h>
#include<conio.h>
void main()
{
int s,n,i,a[50],p;
clrscr();
cout<<"Enter the number of elements of array : ";
cin>>n;
cout<<"Enter the elements : ";
for(i=0;i<n;i++)
cin>>a[i];
cout<<"Enter the new element and its position : ";
cin>>s>>p;
for(i=n;i>=p;i--)
a[i]=a[i-1];
a[p]=s;
cout<<"The array after inserting new element is : ";
for(i=0;i<n+1;i++)
cout<<a[i]<<"  ";
getch();
}

To know about array click here

Thursday, July 25, 2013

Program to accept 20 numbers and display it in ascending order and descending order - Bubble sort


#include<iostream.h>
#include<conio.h>
void main()
{
int a[20],i,j,t;
clrscr();
cout<<"Enter 20 numbers : ";
for(i=0;i<20;i++)
cin>>a[i];
for(i=0;i<20;i++)
{
for(j=i+1;j<20;j++)
{
 if(a[i]>a[j])
{
    t=a[i];
    a[i]=a[j];
    a[j]=t;
}
}
}
cout<<"Ascending order : "<<"\n";
for(i=0;i<20;i++)
cout<<a[i]<<"\n";
cout<<"Descending order : "<<"\n";
for(i=19;i>=0;i--)
cout<<a[i]<<"\n";
getch();
}

Program to access the elements in two dimensional array and display it


#include<iostream.h>
#include<conio.h>
void main()
{
 clrscr();
 int a[20][20],m,n,i,j;
 cout<<"Enter the size of matrix : "
 cin>>m>>n;
 cout<<"Enter the elements : ";
 for(i=0;i<m;i++)
 for(j=0;j<n;j++)
 cin>>a[i][j];
 cout<<"The matix is : "<<"\n";
 for(i=0;i<m;i++)
 {
 for(j=0;j<n;j++)
 {
 cout<<a[i][j]<<"\t";
 }
 }
 getch();
}

Popular Posts