Professional Documents
Culture Documents
cout<<number ; prints 10
cout<<&number ; 4000
2. Reference Variables:
Ref. vars. are also called as alias variables which are used to give another name to
existing variable.
int n=5;
int &m=n;
here n & m both points to same location hence if n changed m also get changed or vice
versa.
3. Pointers:
Pointers are variables which points to address of a variable. For declaration * is used.
#include<iostream.h>
#include<conio.h>
void main()
{
int i=10, *p;
clrscr(); Output
p = &i;
cout<<”\nValue of i: “<<i; 10
cout<<”\nValue of i: “<<*p; 10
cout<<”\nValue of i: “<<*(&i); 10
#include<iostream.h>
void main()
{
int i=5, *p, **p;
p=&i;
pp=&p;
Output
cout<<” Value of i “ << i; 10
cout<<”\n Value of i “ << *p; 10
cout<<”\n Value of i “ << *(&i); 10
cout<<”\n Value of i “ << **p; 10
double function()
{
double a, b;
a=10.5;
b=20.5;
a=a + b;
return(a);
}
struct item a;
a.ItemName=”Soap” ;
a.Price=10;
a.mnfDate.day=3;
a.mnfDate.month=10;
a.mnfDate.year=2007;
cout<<”\n Name :”<<a.ItemName;
cout<<”\n Price :”<<a.Price;
cout<<”\n Date :”<<a.mnfDate.day<<”/”;
cout<<a.mnfDate.month<<”/”;
cout<<a.mnfDate.year;
}
//Program on structure local to function
#include<iostream.h>
void main()
{ void function();
function();
getch();
}
void function()
{ struct date
{ int day;
int month;
int year;
};
struct date today={2,10,2007};
cout<<”Todays date is “<<today.day<<today.month<<today.year;
}
3. Union:
Union & structure are both same in nature & in use. Memory requirement of union is less
as compared with structure. The memory used for struct is addition of all data type used
in structure where as memory of union is the highest memory data type in union. Since
all variables use same memory once we use next variable first is removed from memory
& hence if we print its value gives wrong result.
struct st union un
{ {
int j; int j;
float x; float x;
char p; char p;
}; };
Here memory requirement of structure is 2 + 4 + 1 =7 Bytes.
Where as memory requirement of union is 4 Bytes i.e. highest memory data type x.
struct node
{
int info;
struct node *next;
};
struct node *list=0;
struct node * getnode(void)
{
/*function to allocate memory for new node*/
return((struct node*)new(struct node));
}/*end getnode*/
void insertend(int x)
{
/*insert new element at the end of linked list*/
struct node *q,*temp;
q=getnode();
q->info=x;
q->next=NULL;
temp=list;
if(temp==NULL)
list=q;
else
{
while(temp->next!=NULL)
temp=temp->next;
temp->next=q;
}
display();
}/*void insertend*/
void insafter(int p,int x)
{
/*insert new element after a specific node*/
struct node *q,*t;
int i;
if(list==NULL)
cout<<"\nInvalid insertion";
else
{
t=list;
for(i=0;i<p-1;i++)
t=t->next;
q=getnode();
q->info=x;
q->next=t->next;
t->next=q;
display();
}
}/*end insafter*/
void delafter(int p)
{
/*delete new element after a specific node*/
int x,i;
struct node *t,*q;
clrscr();
cout<<"\n\tDynamic Linked List";
cout<<"\n1.Insert after a node";
cout<<"\n2.Delete after a node";
cout<<"\n3.Insert at begining ";
cout<<"\n4.Delete at begining ";
cout<<"\n5.Insert at end ";
cout<<"\n6.Delete at end ";
cout<<"\n7.Insert at location";
cout<<"\n8.Delete at location";
cout<<"\n9.Search an element";
back:cout<<"\nEnter ur choice : ";
cin>>ch;
switch(ch)
{
case 1: cout<<"\nEnter the node no : ";
cin>>p;
cout<<"\nEnter the info : ";
cin>>x;
insafter(p,x);
break;
case 2: cout<<"\nEnter the node no : ";
cin>>p;
delafter(p);
break;
case 3: cout<<"\nEnter the info : ";
cin>>x;
insertbeg(x);
break;
case 4: deletebeg();
break;
case 5: cout<<"\nEnter the info : ";
cin>>x;
insertend(x);
break;
//Program on structures
#include<iostream.h>
#include<conio.h>
#include<string.h>
struct student
{
char name[20],DOB[8];
int Marks,SeatNo;
} s[5];
main()
{
int i,j,temp;
char temp1[20];
for(i=0;i<5;i++)
{
cout<<"\n Enter the name of student : " <<i;
cin>>s[i].name;
cout<<"\n Enter the SeatNo of student : "<<i;
cin>>s[i].SeatNo;
cout<<"\n Enter the DOB of student : "<<i;
cin>>s[i].DOB;
cout<<"\n Enter the Marks of student : "<<i;
cin>>s[i].Marks;
}
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
if(s[i].SeatNo>s[j].SeatNo)
temp=s[j].Marks;
s[j]. Marks =s[i]. Marks;
s[i]. Marks =temp;
strcpy(temp1,s[j].DOB);
strcpy(s[j].DOB,s[i].DOB);
strcpy(s[i].DOB,temp1);
strcpy(temp1,s[j].name);
strcpy(s[j].name,s[i].name);
strcpy(s[i].name,temp1);
}
}
for(i=0;i<5;i++)
{ cout<<"\n Name : "<< s[i].name<<"\t SeatNo :
"<<s[i].SeatNo<<"\tDOB : "
<<s[i].DOB <<"\t Marks : "<<s[i].Marks;
}
}
return(0);
}
//Program on structures
#include <iostream.h>
#include <conio.h>
struct COMPUTER
{
char CPUtype[15],CDROM_type[10],mouse_type[15];
char key_board_type[15],SVGA_monitor_make[15];
int SDRAM_size,HDD_size;
float clock_speed,CDROM_speed;
} comp[50];
void main()
{
int i;
clrscr();
for(i=0;i<50;i++)
{
cout<<"\nEnter CPU tpye, CDROM_type, mouse_type";
cin>>comp[i].CPUtype>>comp[i].CDROM_type>>comp[i].mouse_type;
cout<<"Enter key_board_type, SVGA_monitor_make";
cin>>comp[i].key_board_type>>comp[i].SVGA_monitor_make;
cout<<"Enter SDRAM_size, HDD_size";
cin>>comp[i].SDRAM_size>>comp[i].HDD_size;
cout<<"Enter clock_speed, CDROM_speed";
cin>>comp[i].clock_speed>>comp[i].CDROM_speed;
}
for(i=0;i<50;i++)
{ if(comp[i].HDD_size> 8)
{
cout<<"CPU type is "<<comp[i].CPUtype;
}
}
}