Showing posts with label element. Show all posts
Showing posts with label element. Show all posts
Wednesday, February 18, 2015
C Program to delete an element from an Array
#include<iostream.h>
#include<process.h>
#include<conio.h>
void main()
{
clrscr();
int a[50],x,n,i,j,b[50];
cout<<"How many elements of Array you want to create?";
cin>>n;
cout<<"Enter elements of Array
";
for(i=0;i<n;++i)
cin>>a[i];
cout<<"Enter element to delete:";
cin>>x;
for(i=0,j=0;i<n;++i)
{
if(a[i]!=x)
b[j++]=a[i];
}
if(j==n)
{
cout<<"
Soory!!!Element is not in the Array";
exit(0);
}
else
{
cout<<"
New Array is ";
for(i=0;i<j;i++)
cout<<b[i]<<" ";
}
getch();
}
Friday, February 13, 2015
C program to find largest and smallest element from a 1D array

#include<stdio.h>
#include<conio.h>
void main()
{
int a[50],i,n,large,small;
clrscr();
printf("How many elements:");
scanf("%d",&n);
printf("Enter the Array:");
for(i=0;i<n;++i)
scanf("%d",&a[i]);
large=small=a[0];
for(i=1;i<n;++i)
{
if(a[i]>large)
large=a[i];
if(a[i]<small)
small=a[i];
}
printf("The largest element is %d",large);
printf("
The smallest element is %d",small);
getch();
}
Subscribe to:
Posts (Atom)