Tag: Array
Finding the address of an Element in an Array
by Binod on Mar.04, 2011, under C & C++, Technology, Tutorial
This program shows how to find out the address of a particular element of an array.
Here what is done is some variables are stored in the array ‘a[]‘ and user is asked to enter an element of an array, then the element is checked in the array to find its index and then the memory address is shown.
#include<iostream.h>
#include<conio.h>
#define SIZE 10
main(){
clrscr();
int a[SIZE]={1,2,3,4,5,6,7,8,9,0} ;
int x;
cout<<”Enter Element: “;
cin>>x;
int count=0;
for(int i=0;i<=SIZE;i++){
if(a[i]==x)
break;
else
count++;
}
int *temp=&a[count];
cout<<”Address of “<<x<<” = “<<temp;
getch();
return 0;
}
Insert Element in Array
by Binod on Dec.15, 2010, under C & C++, DSA in C, Tutorial
//WAP to INSERT element in an array using C and algorithm
#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
int a[8]={1,2,3,4,5,6,7,0},pos=0,val;
printf(“befor INSERT: “);
for(int i=0;i<8;i++){
printf(“%d”,a[i]);
}
printf(“\n Enter the value and position of the element to INSERT in array: “);
scanf(“%d %d”,&val,&pos);
printf(“after INSERT: “);
pos=pos-1;
for(i=6;i>=pos;i–)
a[i+1]=a[i] ;
a[pos]=val;
for(i=0;i<8;i++){
printf(“%d”,a[i]);
}
getch();
return 0;
}
Array Update
by Binod on Dec.14, 2010, under C & C++, DSA in C, Tutorial
//WAP to UPDATE element in an array using C and algorithm
#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
int a[8]={1,2,3,4,5,6,7, },pos=0,val;
printf(“befor UPDATE: “);
for(int i=0;i<8;i++){
printf(“%d”,a[i]);
}
printf(“\n Enter the value and position of the element to UPDATE in array: “);
scanf(“%d %d”,&val,&pos);
printf(“after UPDATE: “);
pos=pos-1;
for(i=8;i<=pos;i–){
a[i+1]=a[i] ;
}
a[pos]=val;
for(i=0;i<8;i++){
printf(“%d”,a[i]);
}
getch();
return 0;
}
Delete Element in Array
by Binod on Dec.14, 2010, under C & C++, DSA in C, Tutorial
//WAP to DELETE element in an array using C and algorithm
#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
int a[8]={1,2,3,4,5,6,7, },pos=0,val;
printf(“befor DELETE: “);
for(int i=0;i<8;i++){
printf(“%d”,a[i]);
}
printf(“\n Enter the position of the element to DELETE in array: “);
scanf(“%d”,&pos);
printf(“after DELETE: “);
pos=pos-1;
for(i=pos;i<8;i++){
a[i]=a[i+1] ;
}
for(i=0;i<8;i++){
printf(“%d”,a[i]);
}
getch();
return 0;
}