Q) Program To Implement Addition/deletion/modification/display of Records Using An Array of Structures
Q) Program To Implement Addition/deletion/modification/display of Records Using An Array of Structures
Q) Program to implement
addition/deletion/modification/display of
an array of structures
records using
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
struct book
{
int id ;
char name[20] ;
float price ;
} ;
void
void
void
void
void
insert(book a[],int n) ;
display(book a[],int n) ;
deleet(book a[],int &n,int del);
position(book a[],int &n) ;
modify(book a[],int n) ;
void main()
{
clrscr();
book a[5] ;
int n,del,pos,choice ;
cout<<"Enter the number of entries : " ;
cin>>n ;
insert(a,n) ;
cout<<"Choose one of the following : \n" ;
cout<<"1.delete \n2.Add an element \n3.Modify \n4.display"
cin>>choice ;
cout<<endl ;
clrscr();
display(a,n);
switch(choice)
{
case 1: cout<<"Enter the id of book to be deleted : " ;
cin>>del ;
deleet(a,n,del) ;
cout<<endl ;
cout<<"The number has been deleted \n" ;
cout<<"Array is : \n" ;
display(a,n) ;
break;
case 2:
position(a,n) ;
cout<<"The element is added. Array is : \n"
display(a,n) ;
break ;
case 3:
case 4:
modify(a,n) ;
cout<<"The array has been modified : \n" ;
display(a,n) ;
break ;
display(a,n) ;
}
getch();
}
void insert(book a[],int n)
{
for(int i=0; i<n; i++)
{
cout<<"Enter the info for book "<<i+1<<endl
cout<<"Id
: " ;
cin>>a[i].id
;
cout<<"Name : " ;
cin>>a[i].name
;
cout<<"price : " ;
cin>>a[i].price
;
cout<<endl
;
}
}
void display(book a[],int n)
{
for(int i=0; i<n; i++)
{
cout<<"The info for book "<<i+1<<endl
cout<<"Id
: " ;
cout<<a[i].id<<endl
;
cout<<"Name : " ;
cout<<a[i].name<<endl
;
cout<<"price : " ;
cout<<a[i].price<<endl
;
cout<<endl
;
}
}
void deleet(book a[],int &n,int del)
{
int pos ;
for(int i=0; i<n; i++)
{ if(a[i].id==del)
{
pos=i ;
break ;
}
else
{ pos=-1 ; }
}
if(pos!=-1)
{
for(int i=pos; i<n; i++)
{
a[i]=a[i+1] ;
}
n=n-1 ;
}
}
void position(book a[],int &n)
{
book b ;
int pos ;
cout<<"Enter
cout<<"Id
cin>>b.id
cout<<"Name
cin>>b.name
cout<<"price
cin>>b.price
cout<<endl
else
for(int i=n-1; a[i].id > b.id && i>=0; i--)
{
a[i+1] = a[i] ;
pos=i ;
}
n=n+1 ;
}
a[pos]= b ;
}
void modify(book a[],int n)
{ int check ;
cout<<"Enter the ID of whoose information
cin>>check ;
for(int i=0; i<n; i++)
}
}
if(a[i].id==check)
{
cout<<"Enter the new information "<<endl ;
cout<<"Id
: " ;
cin>>a[i].id
;
cout<<"Name : " ;
cin>>a[i].name
;
cout<<"price : " ;
cin>>a[i].price
;
cout<<endl
;
break ;
}