Star Pattern

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n;
cout<<” enter the value”;
cin>>n;
int m=10;
int s=n-1;
for(int i=1;i<=n;i++)
{
for(int j=2*n-3;j>=s;j–)
{
cout<<” “;
}
for(int p=10;p>=m;p=p-1)
{
cout<<“*”;
}
cout<<“\n”;
m=m-2;
s++;
}
int y=n-1;
int v=2*n-2;
for(int q=n;q>=1;q–)
{
for(int g=n-1;g>=y;g–)
{
cout<<” “;
}
for(int h=1;h<v;h=h+1)
{
cout<<“*”;
}
cout<<“\n”;
y–;
v=v-2;
}
getch();
}

Fibonacci Series

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a=0,b=1,c,n;
cout<<“enter the limit”;
cin>>n;
cout<<a<<“\n”<<b;
for(int i=2;i<=n;i++)
{
c=a+b;
cout<<“\n”<<c;
a=b;
b=c;
}
getch();
}

Deletion-Linked List

#include<iostream.h>
#include<stdio.h>
#include<conio.h>
struct stud
{
int roll;
char name[30];
stud *next;
};
void main()
{
clrscr();
int n;
cout<<“enter n”;
cin>>n;
stud *first,*last,*x;
first =new stud;
cout<<“enter the first roll”;
cin>>first->roll;
cout<<“name”;
gets(first->name);
fflush(stdin);
first->next=NULL;

last = first;
for(int a=1;a<n;a++)
{
x=new stud;
cout<<“enter roll”;
cin>>x->roll;
cout<<“name”;
gets(x->name);
fflush(stdin);
x->next=NULL;
last->next=x;
last=x;
}
cout<<“elements are”;
last=first;
while(last!=NULL)
{
cout<<last->roll;
puts(last->name);
fflush(stdout);
cout<<“\n”;
last =last->next;
}
int d;
cout<<“roll no 2 b deleted”;
cin>>d;
int found=0;
stud* ptr;
if(first->roll==d)
{
ptr=first;
first=first->next;
delete ptr;
}
ptr=first->next;
last=first;
while(ptr!=NULL)
{
if(ptr->roll==d)
{
last->next=ptr->next;
delete ptr;
found=1;
break;
}
else{
ptr=ptr->next;
last=ptr;
}
if( found==0)
{
cout<<“deletion not possible”;
}
}
cout<<“elements are”;
last=first;
while(last!=NULL)
{
cout<<last->roll;
puts(last->name);
fflush(stdout);
cout<<“\n”;
last =last->next;
}
delete first;
getch();
}

Class- Student

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class student
{
private:
int roll;char name[30];int clas[7];int marks[5];float percentage;
int calculate();
public:
void readmarks();
void displaymarks();
};
int student::calculate()
{
int s=0;
for(int a=0;a<5;a++)
{
s=s+marks[a];
}
percentage=s/5;
return (percentage);
}
void student::readmarks()
{
cout<<“enter”;
for(int b=0;b<5;b++)
{
cin>>marks[b];
}
percentage=calculate();
}
void student::displaymarks()
{
cout<<“marks r”;
for(int c=0;c<5; c++)
{
cout<<marks[c];
cout<<“\n”;
}
cout<<“\n”<<percentage;
}
void main()
{
clrscr();
student s1;
s1. readmarks();
s1. displaymarks();
getch();
}

Selection Sort

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
struct student
{
int marks;
}s1[100],s2,s[100];
void sel(student&s[],int x);
void main()
{
clrscr();
int n;
cout<<“enter n”;
cin>>n;
cout<<“enter”;
for(int a=0;a<n;a++)
{
cin>>s1[a].marks;

}
cout<<“list is”;
sel(s1,n);
getch();
}
void sel(student&s[],int x)
{
int pos,small,temp;
for(int i=0;i<x-1;i++)
{

small=s[i].marks;
pos=i;
for(int j=i+1;j<x;j++)
{
if(small>s[j].marks)
{
small=s[j].marks;
pos=j;
}
}
s2.marks=s[pos].marks;
s[pos].marks=s[i].marks;
s[i].marks=s2.marks;
}
for(int f=0;f<x;f++)
{
cout<<“\n “<<s[f].marks<<“\t”;

}
}

 

Pointers and structures

\\ pointers and structures
#include<iostream.h>
#include<stdio.h>
struct student
{
char name[30];
int age;
int roll;
}s;
void main()
{student *ptr;
ptr =&s;
cout<<“enter name,age,roll number”;
gets(s.name); \\reading through dot
cin>>s.age>>s.roll;
cout<<“details are”;
puts(s->name);\\printing through arrow
cout<<s->age<<s->roll;
}

Strings comparison

\\string comparison using pointers
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
void main()
{
clrscr();
char a[20],b[20];
char *p1,*p2;
p1=a;
p2=b;
int flag=0;
cout<<“enter two strings”;
gets(b);
gets(a);
while((*p1!=”)||(*p21=”))
{
if(*p1==*p2)
{
p1++;
p2++;
}
else
{
flag=1;
break;
}
}
if(flag==0)
{
cout<<“the two strings are equal”;
}
getch();
}

 

Usage of This pointer

#include<iostream.h>
#include<conio.h>
class emp
{
char name[30];
float salary;
int age;
public:
void reveal()
{
cout<<“\n my object’s address is \t”<<this;
}
};
void main()
{
clrscr();
emp e1,e2,e3;
e1.reveal();
e2.reveal();
e3.reveal();
getch();
}

PROJECT-telephone directory

//*****************************************************************************
// TELEPHONE ENQUIRY AND TELEPHONE BILLS
//*****************************************************************************

# include<fstream.h>
# include<ctype.h>
# include<iostream.h>
# include<iomanip.h>
# include<conio.h>
# include<stdio.h>
# include<stdlib.h>
# include<string.h>
# include<process.h>

//struture of telephonic program
struct tel
{
char telno[15];
char name[21];
}s; //Structure variable s
void add(void);
void display(void);
void enquiryt(void);
void mod(void);
void enquiryn(void);
void del(void);
//*****************************************************************************
// Main program starts
//*****************************************************************************
void main()
{
clrscr();
f:int j=0;
cout<<“\n MAIN MENU”;
cout<<“\n 1.ADD RECORDS”;
cout<<“\n 2.GET ENQUIRY FROM TELEPHONE NUMBER”;
cout<<“\n 3.GET ENQUIRY FROM NAME”;
cout<<“\n 4.DISPLAY THE RECORDS”;
cout<<“\n 5.MODIFY ANY RECORD”;
cout<<“\n 6.DELETE ANY RECORD”;
cout<<“\n 7.EXIT FOM THE MAIN MENU”;
cout<<“\n ENTER YOUR CHOICE :- “;
cin>>j;
switch(j)
{
case 1: add();
break;
case 2: enquiryt();
break;
case 3: enquiryn();
break;
case 4: display();
break;
case 5: mod();
break;
case 6: del();
break;
case 7: exit(0);
default:clrscr();
gotoxy(22,12);
cout<<“YOU HAVE ENTERED A WRONG CHOICE”;
getch();
break;
}
goto f;
}

// TO CREATE FILE OF CUSTOMER HAVING TELEPHONE CONNECTION
void add(void)
{
ofstream fp;
fp.open(“ka.dat”,ios::app);
cout<<“\n TELEPHONE NUMBER ->”;
gets(s.telno);
cout<<“\n NAME OF CUSTOMER ->”;
gets(s.name);
fp.write((char*)&s,sizeof(tel));
fp.close();
}

// TO GET ENQUIRY BY TELEPHONE FROM MAIN FILE
void enquiryt(void)
{
char v[10];
ifstream fp5;
fp5.open(“ka.dat”,ios::beg);
cout<<“\n ENTER THE DESIRE TELEPHONE NUMBER ->”;
cin>>v;
while(fp5)
{
fp5.read((char*)&s,sizeof(tel));
if(strcmp(s.telno,v)==0)
{
cout<<“\n TELEPHONE NUMBER ->”<<s.telno;
cout<<“\n NAME ->”<<s.name;
break;
}
else
{
cout<<“\n THE TELEPHONE NUMBER YOU HAVE ENTERED DOES NOT EXISTS”;
}
}
fp5.close();
}

//****************************************************************************
// TO GET ENQUIRY NAME BY FROM MAIN FILE
//****************************************************************************
void enquiryn(void)
{
char w[21];
ifstream fp5;
fp5.open(“ka.dat”,ios::beg);
cout<<“ENTER THE DESIRE NAME ->”;
cin>>w;
while(fp5)
{
fp5.read((char*)&s,sizeof(tel));
if(strcmp(s.name,w)==0)
{
cout<<“\n TELEPHONE NUMBER ->”<<s.telno;
cout<<“\n NAME ->”<<s.name;
break;
}
else
{
cout<<“\n THE NAME YOU HAVE ENTERED DOES NOT EXISTS”;
}
}
fp5.close();
}

//****************************************************************************
// TO DISPLAY ALL THE RECORDS FROM THE MAIN FILE
//****************************************************************************
void display(void)
{
int rrn;
cout<<“\n Enter the record no. : “;
cin>>rrn;
long b=((rrn-1)*(sizeof(tel)));
ifstream fg;
fg.open(“ka.dat”,ios::beg,ios::in);
fg.seekg(b,ios::beg);
cout<<“\n TEL. No. \t\t NAME”;
fg.read((char*)&s,sizeof(tel));
if(fg.eof())
{
cout<<“\n Record not entered yet.”;
}
else
//while(!fg.eof())
{
cout<<“\n “<<s.telno<<“\t\t”<<s.name;
//fg.read((char*)&s,sizeof(tel));
}
fg.close();
}

//****************************************************************************
// TO MODIFY ANY RECORD OF FROM THE MAIN FILE
//****************************************************************************
void mod(void)
{
char b[10];
fstream file ;
file.open(“ka.dat”, ios::in) ;
fstream temp ;
temp.open(“kar.dat”, ios::out) ;
file.seekg(0,ios::beg) ;
cout<<“ENTER THE DESIRE TELEPHONE NUMBER ->”;
cin>>b;
while (file.read((char *)&s, sizeof(tel)))
{
if ( file.eof() )
break ;
if (strcmp(s.telno,b)==0)
{
cout<<“\n *CURRENT DETAILS OF THE GIVEN NUMBER*”;
cout<<“\n TELEPHONE NUMBER ->”<<s.telno;
cout<<“\n NAME ->”<<s.name;
cout<<“\n\n *ENTER NEW DETAILS OF THE GIVEN NUMBER*”;
cout<<“\n If You Want To Retain Old Name & Tel No, Retype them”;
cout<<“\n TELEPHONE NUMBER ->”;
gets(s.telno);
cout<<“\n NAME ->”;
gets(s.name);
}
else
{
cout<<“\n THE TELEPHONE NUMBER YOU HAVE ENTERED DOES NOT EXISTS”;
}
temp.write((char *)&s, sizeof(tel)) ;
}
file.close() ;
temp.close() ;
}

//****************************************************************************
// TO DELETE ANY RECORD OF FROM THE MAIN FILE
//****************************************************************************

void del(void)
{
char b[10], confirm, found=’n’;
int c;
fstream file ;
file.open(“ka.dat”, ios::in) ;
fstream temp ;
temp.open(“kar.dat”, ios::out) ;
file.seekg(0,ios::end) ;
c=file.tellg();
c=c-sizeof(s);
file.seekg(0,ios::beg) ;
cout<<“ENTER THE DESIRE TELEPHONE NUMBER ->”;
cin>>b;
while(temp.tellp()<c )
{ file.read((char*)&s, sizeof(s));
if (strcmp(s.telno,b)==0)
{
cout<<“\n *CURRENT DETAILS OF THE GIVEN NUMBER*”;
cout<<“\n TELEPHONE NUMBER ->”<<s.telno;
cout<<“\n NAME ->”<<s.name;
found=’y’;
cout<<“\n ARE YOU SURE, YOU WANT TO DELETE THIS RECORD?(y/n)”;
cin>>confirm;
if(confirm==’n’)
temp.write((char*)&s, sizeof(s));

}
else
temp.write((char*)&s, sizeof(s));

}
if(found==’n’)
cout<<“No record found!\n”;

file.close();
temp.close();
remove(“ka.dat”);
rename(“kar.dat”,”ka.dat”);
}

Merit List using structures and selection sort

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
struct student
{
int marks;
char name[30];
}s1[100],s2,s3[100];
student sel(student s1[],int n,int b);
void main()
{
clrscr();
int n;
cout<<“enter n”;
cin>>n;
cout<<“enter”;
for(int a=0;a<n;a++)
{
cin>>s1[a].marks;
cin>>s1[a].name;
fflush(stdin);

}
cout<<“list is”;
cout<<“\n”;
for(int b=0;b<n;b++)
{
s3[b]=sel(s1,n,b);
cout<<s3[b].marks;
puts(s3[b].name);
}
getch();
}
student sel(student s1[],int n,int b)
{
int pos,small,temp;
for(int i=0;i<n-1;i++)
{

small=s1[i].marks;
pos=i;
for(int j=i+1;j<n;j++)
{
if(small<s1[j].marks)
{
small=s1[j].marks;
pos=j;
}
}
s2=s1[pos];
s1[pos]=s1[i];
s1[i]=s2;
}
int m;
m=b;

return s1[m];

}

 

Calendar

July 2012
M T W T F S S
 1
2345678
9101112131415
16171819202122
23242526272829
3031  

Categories

Archives