wherever u see ... remove that . and put << in that place.
if you see .. then remove that . and put < in that place..
11. Write a C++ program to read and write student objects with Variable -
Length records using any suitable record structure. Implement pack ( ),
unpack ( ), modify ( ) and search ( ) methods.
#include..stdio.h>
#include..stdlib.h>
#include..iostream.h>
#include..fstream.h>
#include..conio.h>
#include..string.h>
#include..iomanip.h>
class student
{
public: char name[15],usn[15],age[5],sem[5],branch[15],buffer[100];
};
student s2[100];
void writeRecord()
{
fstream app;
student s;
app.open("student.txt",ios::app); //Open file in append mode
if(!app)
{
cout..."cannot open the file in output mode";
getch();
exit(0);
}
cout..."\n Enter the student name = "; cin>>s.name;
cout..."\n Enter the usn = "; cin>>s.usn;
cout..."\n Enter the age = "; cin>>s.age;
cout..."\n Enter the sem = "; cin>>s.sem;
cout..."\n Enter the branch = "; cin>>s.branch;
//packing the information
strcpy(s.buffer, s.name); strcat(s.buffer,"|");
strcat(s.buffer, s.usn); strcat(s.buffer,"|");
strcat(s.buffer, s.age); strcat(s.buffer,"|");
strcat(s.buffer, s.sem); strcat(s.buffer,"|");
strcat(s.buffer, s.branch);strcat(s.buffer,"\n");
app...s.buffer; //writing the packed information to buffer
app.close();
}
void search()
{
fstream in;
char usn[15], extra[45];
in.open("student.txt",ios::in);
if(!in)
{
cout..."\nUnable to open the file in input mode";
getch();
exit(0);
}
cout..."\nEnter the record's usn you want to search = "; cin>>usn;
student s;
//Unpacking the record
while(!in.eof())
{
in.getline(s.name,15,'|');
in.getline(s.usn,15,'|');
in.getline(s.age,5,'|');
in.getline(s.sem,5,'|');
in.getline(s.branch,15,'\n');
if(strcmp(s.usn,usn)==0)
{
cout..."\nRecord found";
cout..."\n"...s.name..."\t"...s.usn..."\t"...s.age..."\t"...s.sem..."\t"...s.branch;
getch();
return;
}
}
cout..."\n Record not found";
getch();
return;
}
void displayFile()
{
student s;
int c,i;
fstream in;
in.open("student.txt",ios::in);
if(!in)
{
cout..."\nCannot open the file in output mode";
getch();
exit(0);
}
i=0;
printf("Name\t\tUsn\t\tAge\t\tSem\t\tBranch\n");
while(!in.eof())
{
in.getline(s.name,15,'|');
in.getline(s.usn,15,'|');
in.getline(s.age,5,'|');
in.getline(s.sem,5,'|');
in.getline(s.branch,15,'!');
printf("\n%s\t\t%s\t\t%s\t\t%s\t\t%s",s.name,s.usn,s.age,s.sem,s.branch);
i++;
}
in.close();
getch();
}
void modify()
{
fstream in;
char usn[15],buffer[45],extra[45];
int i,j;
student s1[100];
in.open("student.txt",ios::in);
if(!in)
{
cout..."\nUnable to open the file in input mode";
getch();
exit(0);
}
cout..."\nEnter the usn"; cin>>usn;
i=0;
//Loading the file to Main memory
while(!in.eof())
{
in.getline(s1[i].name,15,'|');
in.getline(s1[i].usn,15,'|');
in.getline(s1[i].age,5,'|');
in.getline(s1[i].sem,5,'|');
in.getline(s1[i]. branch, 15,'\n');
i++;
}
i--;
for(j=0;j..i;j++)
{
if(strcmp(usn,s1[j].usn)==0)
{
cout..."\nThe old values of the record with usn "...usn..." are ";
cout..."\nname = "... s1[j].name;
cout..."\nusn = "... s1[j].usn;
cout..."\nage = "... s1[j].age;
cout..."\nsem = "... s1[j].sem;
cout..."\nbranch = "... s1[j].branch;
cout..."\nEnter the new values \n";
cout..."\nname = "; cin>>s1[j].name;
cout..."\nusn = "; cin>>s1[j].usn;
cout..."\nage = "; cin>>s1[j].age;
cout..."\nsem = "; cin>>s1[j].sem;
cout..."\nbranch= "; cin>>s1[j].branch;
break;
}
}
if(j==i)
{
cout..."\n Record with usn "...usn..." is not present";
getch();
return;
}
in.close();
fstream out1;
out1.open("student.txt",ios::out);
if(!out1)
{
cout..."\nUnable to open file in output mode";
getch();
return;
}
for(j=0;j..i;j++)
{
out1...s1[j].name...'|'...s1[j].usn...'|'...s1[j].age...'|'...s1[j].sem...'|'...s1[j].sem...'|'...s1[j].branch...'\n';
}
out1.close();
}
void main()
{
fstream in;
fstream out;
int ch;
out.open("student.txt",ios::out);
if(!in)
{
cout..."\n\nCannot open the file in output mode";
getch();
exit(0);
}
out.close();
for(;;)
{
clrscr();
cout..."\n O:exit\n 1: write to file\n 2:display the file"
..."\n 3:modify the file\n 4:search";
cout..."\n\n Enter the choice: "; cin>>ch;
switch(ch)
{
case 1: writeRecord();break;
case 2: displayFile();break;
case 3: modify();break;
case 4: search (); break;
case 0: exit(0);
default: cout..."\nInvalid input....";break;
}
}
}
Tuesday, March 16, 2010
wherever u see ... remove that . and put << in that place.
if you see .. then remove that . and put < in that place..
10.Write a C++ program to read and write student objects with Variable -
Length records using any suitable record structure. Implement pack ( ),
unpack ( ), modify ( ) and search ( ) methods.
#include..stdio.h>
#include..stdlib.h>
#include..iostream.h>
#include..fstream.h>
#include..conio.h>
#include..string.h>
#include..iomanip.h>
class student
{
public: char name[15],usn[15],age[5],sem[5],branch[15],buffer[100];
};
student s2[100];
void writeRecord()
{
fstream app;
student s;
app.open("student.txt",ios::app); //Open file in append mode
if(!app)
{
cout..."cannot open the file in output mode";
getch();
exit(0);
}
cout..."\n Enter the student name = "; cin>>s.name;
cout..."\n Enter the usn = "; cin>>s.usn;
cout..."\n Enter the age = "; cin>>s.age;
cout..."\n Enter the sem = "; cin>>s.sem;
cout..."\n Enter the branch = "; cin>>s.branch;
//packing the information
strcpy(s.buffer, s.name); strcat(s.buffer,"|");
strcat(s.buffer, s.usn); strcat(s.buffer,"|");
strcat(s.buffer, s.age); strcat(s.buffer,"|");
strcat(s.buffer, s.sem); strcat(s.buffer,"|");
strcat(s.buffer, s.branch);strcat(s.buffer,"\n");
app...s.buffer; //writing the packed information to buffer
app.close();
}
void search()
{
fstream in;
char usn[15], extra[45];
in.open("student.txt",ios::in);
if(!in)
{
cout..."\nUnable to open the file in input mode";
getch();
exit(0);
}
cout..."\nEnter the record's usn you want to search = "; cin>>usn;
student s;
//Unpacking the record
while(!in.eof())
{
in.getline(s.name,15,'|');
in.getline(s.usn,15,'|');
in.getline(s.age,5,'|');
in.getline(s.sem,5,'|');
in.getline(s.branch,15,'\n');
if(strcmp(s.usn,usn)==0)
{
cout..."\nRecord found";
cout..."\n"...s.name..."\t"...s.usn..."\t"...s.age..."\t"...s.sem..."\t"...s.branch;
getch();
return;
}
}
cout..."\n Record not found";
getch();
return;
}
void displayFile()
{
student s;
int c,i;
fstream in;
in.open("student.txt",ios::in);
if(!in)
{
cout..."\nCannot open the file in output mode";
getch();
exit(0);
}
i=0;
printf("Name\t\tUsn\t\tAge\t\tSem\t\tBranch\n");
while(!in.eof())
{
in.getline(s.name,15,'|');
in.getline(s.usn,15,'|');
in.getline(s.age,5,'|');
in.getline(s.sem,5,'|');
in.getline(s.branch,15,'!');
printf("\n%s\t\t%s\t\t%s\t\t%s\t\t%s",s.name,s.usn,s.age,s.sem,s.branch);
i++;
}
in.close();
getch();
}
void modify()
{
fstream in;
char usn[15],buffer[45],extra[45];
int i,j;
student s1[100];
in.open("student.txt",ios::in);
if(!in)
{
cout..."\nUnable to open the file in input mode";
getch();
exit(0);
}
cout..."\nEnter the usn"; cin>>usn;
i=0;
//Loading the file to Main memory
while(!in.eof())
{
in.getline(s1[i].name,15,'|');
in.getline(s1[i].usn,15,'|');
in.getline(s1[i].age,5,'|');
in.getline(s1[i].sem,5,'|');
in.getline(s1[i]. branch, 15,'\n');
i++;
}
i--;
for(j=0;j..i;j++)
{
if(strcmp(usn,s1[j].usn)==0)
{
cout..."\nThe old values of the record with usn "<
cout..."\nusn = "... s1[j].usn;
cout..."\nage = "... s1[j].age;
cout..."\nsem = "... s1[j].sem;
cout..."\nbranch = "... s1[j].branch;
cout..."\nEnter the new values \n";
cout..."\nname = "; cin>>s1[j].name;
cout..."\nusn = "; cin>>s1[j].usn;
cout..."\nage = "; cin>>s1[j].age;
cout..."\nsem = "; cin>>s1[j].sem;
cout<<"\nbranch= "; cin>>s1[j].branch;
break;
}
}
if(j==i)
{
cout..."\n Record with usn "<
return;
}
in.close();
fstream out1;
out1.open("student.txt",ios::out);
if(!out1)
{
cout..."\nUnable to open file in output mode";
getch();
return;
}
for(j=0;j..i;j++)
{
out1...s1[j].name...'|'...s1[j].usn...'|'...s1[j].age...'|'...s1[j].sem...'|'...s1[j].sem...'|'...s1[j].branch...'\n';
}
out1.close();
}
void main()
{
fstream in;
fstream out;
int ch;
out.open("student.txt",ios::out);
if(!in)
{
cout..."\n\nCannot open the file in output mode";
getch();
exit(0);
}
out.close();
for(;;)
{
clrscr();
cout..."\n O:exit\n 1: write to file\n 2:display the file"
..."\n 3:modify the file\n 4:search";
cout..."\n\n Enter the choice: "; cin>>ch;
switch(ch)
{
case 1: writeRecord();break;
case 2: displayFile();break;
case 3: modify();break;
case 4: search (); break;
case 0: exit(0);
default: cout..."\nInvalid input....";break;
}
}
}
Monday, March 15, 2010
wherever u see ... remove that . and put << in that place.
if you see .. then remove that . and put < in that place..
9.Write a C++ program to read and write student objects with fixed-length
records and the fields delimited by “|”. Implement pack ( ), unpack ( ),
modify ( ) and search ( ) methods.
#include..stdio.h>
#include..stdlib.h>
#include..iostream.h>
#include..fstream.h>
#include..conio.h>
#include..string.h>
#include..iomanip.h>
class student
{
public: char name[15],usn[15],age[5],sem[5],branch[15],buffer[45];
};
student s2[100];
void writeRecord() //Function to add record to file
{
fstream app;
student s;
app.open("student.txt",ios::app); //Open file in append mode
if(!app)
{
cout..."cannot open the file in output mode";
getch();
exit(0);
}
cout..."\n Enter the student name = "; cin>>s.name;
cout..."\n Enter the usn = "; cin>>s.usn;
cout..."\n Enter the age = "; cin>>s.age;
cout..."\n Enter the sem = "; cin>>s.sem;
cout..."\n Enter the branch = "; cin>>s.branch;
//packing the information
strcpy(s.buffer, s.name); strcat(s.buffer,"|");
strcat(s.buffer, s.usn); strcat(s.buffer,"|");
strcat(s.buffer, s.age); strcat(s.buffer,"|");
strcat(s.buffer, s.sem); strcat(s.buffer,"|");
strcat(s.buffer, s.branch);
int count=strlen(s.buffer);
for(int k=0;k..45-count;k++)
strcat(s.buffer,"!");
strcat(s.buffer,"\n");
app...s.buffer; //writing the packed information to buffer
app.close();
}
void search()
{
fstream in;
char usn[15], extra[45];
in.open("student.txt",ios::in);
if(!in)
{
cout..."\nUnable to open the file in input mode";
getch();
exit(0);
}
cout..."\nEnter the record's usn you want to search = "; cin>>usn;
student s;
//Unpacking the record
while(!in.eof())
{
in.getline(s.name,15,'|');
in.getline(s.usn,15,'|');
in.getline(s.age,5,'|');
in.getline(s.sem,5,'|');
in.getline(s.branch,15,'|');
in.getline(extra,45,'\n');
if(strcmp(s.usn,usn)==0)
{
cout..."\nRecord found";
cout..."\n"...s.name..."\t"...s.usn..."\t"...s.age..."\t"...s.sem..."\t"...s.branch;
getch();
return;
}
}
cout..."\n Record not found";
getch();
return;
}
void displayFile()
{
student s;
int c,i;
char extra[45];
fstream in;
in.open("student.txt",ios::in);
if(!in)
{
cout..."\nCannot open the file in output mode";
getch();
exit(0);
}
i=0;
printf("Name\t\tUsn\t\tAge\t\tSem\t\tBranch\n");
while(!in.eof())
{
in.getline(s.name,15,'|');
in.getline(s.usn,15,'|');
in.getline(s.age,5,'|');
in.getline(s.sem,5,'|');
in.getline(s.branch,15,'!');
in.getline(extra,45,'\n');
printf("\n%s\t\t%s\t\t%s\t\t%s\t\t%s",s.name,s.usn,s.age,s.sem,s.branch);
i++;
}
in.close();
getch();
}
void modify()
{
fstream in;
char usn[15],buffer[45],extra[45];
int i,j;
student s1[100];
in.open("student.txt",ios::in);
if(!in)
{
cout..."\nUnable to open the file in input mode";
getch();
exit(0);
}
cout..."\nEnter the usn"; cin>>usn;
i=0;
//Loading the file to Main memory
while(!in.eof())
{
in.getline(s1[i].name,15,'|');
in.getline(s1[i].usn,15,'|');
in.getline(s1[i].age,5,'|');
in.getline(s1[i].sem,5,'|');
in.getline(s1[i]. branch, 15,'\n');
in.getline(extra,45,'\n');
i++;
}
i--;
for(j=0;j..i;j++)
{
if(strcmp(usn,s1[j].usn)==0)
{
cout..."\nThe old values of the record with usn "< cout..."\nname = "... s1[j].name;
cout..."\nusn = "...s1[j].usn;
cout..."\nage = "...s1[j].age;
cout..."\nsem = "... s1[j].sem;
cout..."\nbranch = "... s1[j].branch;
cout...\nEnter the new values \n";
cout..."\nname = "; cin>>s1[j].name;
cout..."\nusn = "; cin>>s1[j].usn;
cout..."\nage = "; cin>>s1[j].age;
cout..."\nsem = "; cin>>s1[j].sem;
cout..."\nbranch= "; cin>>s1[j].branch;
break;
}
}
if(j==i)
{
cout..."\n Record with usn "...usn..." is not present";
getch();
return;
}
in.close();
fstream out1;
out1.open("student.txt",ios::out);
if(!out1)
{
cout..."\nUnable to open file in output mode";
getch();
return;
}
for(j=0;j..i;j++)
{
strcpy(buffer,s1[j].name); strcat(buffer,"|");
strcat(buffer,s1[j].usn); strcat(buffer,"|");
strcat(buffer,s1[j].age); strcat(buffer,"|");
strcat(buffer,s1[j].sem); strcat(buffer,"|");
strcat(buffer,s1[j].branch);
int count=strlen(buffer);
for(int k=0;k..45-count;k++)
strcat(buffer,"!");
strcat(buffer,"\n");
out1...buffer;
}
out1.close();
}
void main()
{
fstream in;
fstream out;
int ch;
out.open("student.txt",ios::out);
if(!in)
{
cout..."\n\nCannot open the file in output mode";
getch();
exit(0);
}
out.close();
for(;;)
{
clrscr();
cout..."\n O:exit\n 1: write to file\n 2:display the file"
..."\n 3:modify the file\n 4:search";
cout..."\n\n Enter the choice: "; cin>>ch;
switch(ch)
{
case 1: writeRecord();break;
case 2: displayFile();break;
case 3: modify();break;
case 4: search (); break;
case 0: exit(0);
default: cout..."\nInvalid input";break;
}
}
}
if you see .. then remove that . and put < in that place..
9.Write a C++ program to read and write student objects with fixed-length
records and the fields delimited by “|”. Implement pack ( ), unpack ( ),
modify ( ) and search ( ) methods.
#include..stdio.h>
#include..stdlib.h>
#include..iostream.h>
#include..fstream.h>
#include..conio.h>
#include..string.h>
#include..iomanip.h>
class student
{
public: char name[15],usn[15],age[5],sem[5],branch[15],buffer[45];
};
student s2[100];
void writeRecord() //Function to add record to file
{
fstream app;
student s;
app.open("student.txt",ios::app); //Open file in append mode
if(!app)
{
cout..."cannot open the file in output mode";
getch();
exit(0);
}
cout..."\n Enter the student name = "; cin>>s.name;
cout..."\n Enter the usn = "; cin>>s.usn;
cout..."\n Enter the age = "; cin>>s.age;
cout..."\n Enter the sem = "; cin>>s.sem;
cout..."\n Enter the branch = "; cin>>s.branch;
//packing the information
strcpy(s.buffer, s.name); strcat(s.buffer,"|");
strcat(s.buffer, s.usn); strcat(s.buffer,"|");
strcat(s.buffer, s.age); strcat(s.buffer,"|");
strcat(s.buffer, s.sem); strcat(s.buffer,"|");
strcat(s.buffer, s.branch);
int count=strlen(s.buffer);
for(int k=0;k..45-count;k++)
strcat(s.buffer,"!");
strcat(s.buffer,"\n");
app...s.buffer; //writing the packed information to buffer
app.close();
}
void search()
{
fstream in;
char usn[15], extra[45];
in.open("student.txt",ios::in);
if(!in)
{
cout..."\nUnable to open the file in input mode";
getch();
exit(0);
}
cout..."\nEnter the record's usn you want to search = "; cin>>usn;
student s;
//Unpacking the record
while(!in.eof())
{
in.getline(s.name,15,'|');
in.getline(s.usn,15,'|');
in.getline(s.age,5,'|');
in.getline(s.sem,5,'|');
in.getline(s.branch,15,'|');
in.getline(extra,45,'\n');
if(strcmp(s.usn,usn)==0)
{
cout..."\nRecord found";
cout..."\n"...s.name..."\t"...s.usn..."\t"...s.age..."\t"...s.sem..."\t"...s.branch;
getch();
return;
}
}
cout..."\n Record not found";
getch();
return;
}
void displayFile()
{
student s;
int c,i;
char extra[45];
fstream in;
in.open("student.txt",ios::in);
if(!in)
{
cout..."\nCannot open the file in output mode";
getch();
exit(0);
}
i=0;
printf("Name\t\tUsn\t\tAge\t\tSem\t\tBranch\n");
while(!in.eof())
{
in.getline(s.name,15,'|');
in.getline(s.usn,15,'|');
in.getline(s.age,5,'|');
in.getline(s.sem,5,'|');
in.getline(s.branch,15,'!');
in.getline(extra,45,'\n');
printf("\n%s\t\t%s\t\t%s\t\t%s\t\t%s",s.name,s.usn,s.age,s.sem,s.branch);
i++;
}
in.close();
getch();
}
void modify()
{
fstream in;
char usn[15],buffer[45],extra[45];
int i,j;
student s1[100];
in.open("student.txt",ios::in);
if(!in)
{
cout..."\nUnable to open the file in input mode";
getch();
exit(0);
}
cout..."\nEnter the usn"; cin>>usn;
i=0;
//Loading the file to Main memory
while(!in.eof())
{
in.getline(s1[i].name,15,'|');
in.getline(s1[i].usn,15,'|');
in.getline(s1[i].age,5,'|');
in.getline(s1[i].sem,5,'|');
in.getline(s1[i]. branch, 15,'\n');
in.getline(extra,45,'\n');
i++;
}
i--;
for(j=0;j..i;j++)
{
if(strcmp(usn,s1[j].usn)==0)
{
cout..."\nThe old values of the record with usn "<
cout..."\nusn = "...s1[j].usn;
cout..."\nage = "...s1[j].age;
cout..."\nsem = "... s1[j].sem;
cout..."\nbranch = "... s1[j].branch;
cout...\nEnter the new values \n";
cout..."\nname = "; cin>>s1[j].name;
cout..."\nusn = "; cin>>s1[j].usn;
cout..."\nage = "; cin>>s1[j].age;
cout..."\nsem = "; cin>>s1[j].sem;
cout..."\nbranch= "; cin>>s1[j].branch;
break;
}
}
if(j==i)
{
cout..."\n Record with usn "...usn..." is not present";
getch();
return;
}
in.close();
fstream out1;
out1.open("student.txt",ios::out);
if(!out1)
{
cout..."\nUnable to open file in output mode";
getch();
return;
}
for(j=0;j..i;j++)
{
strcpy(buffer,s1[j].name); strcat(buffer,"|");
strcat(buffer,s1[j].usn); strcat(buffer,"|");
strcat(buffer,s1[j].age); strcat(buffer,"|");
strcat(buffer,s1[j].sem); strcat(buffer,"|");
strcat(buffer,s1[j].branch);
int count=strlen(buffer);
for(int k=0;k..45-count;k++)
strcat(buffer,"!");
strcat(buffer,"\n");
out1...buffer;
}
out1.close();
}
void main()
{
fstream in;
fstream out;
int ch;
out.open("student.txt",ios::out);
if(!in)
{
cout..."\n\nCannot open the file in output mode";
getch();
exit(0);
}
out.close();
for(;;)
{
clrscr();
cout..."\n O:exit\n 1: write to file\n 2:display the file"
..."\n 3:modify the file\n 4:search";
cout..."\n\n Enter the choice: "; cin>>ch;
switch(ch)
{
case 1: writeRecord();break;
case 2: displayFile();break;
case 3: modify();break;
case 4: search (); break;
case 0: exit(0);
default: cout..."\nInvalid input";break;
}
}
}
wherever u see ... remove that . and put << in that place.
if you see .. then remove that . and put < in that place..
8. Write a C++ program to read series of names, one per line, from standard
input and write these names spelled in reverse order to the standard
output using I/O redirection and pipes. Repeat the exercise using an
input file specified by the user instead of the standard input and using an
output file specified by the user instead of the standard output.
#include..fstream.h>
#include..iostream.h>
#include..string.h>
#include..conio.h>
int main()
{
char name[10][20];
char temp[20];
char ifilename[15], ofilename[15];
int num;
int i=0;
cout..."Enter how many person details you want to enter: "; cin>>num;
cout..."Enter the file name where it has to be written: "; cin>>ofilename;
ofstream ofileobj(ofilename,ios::out);
cout..."Enter person details: ";
while(i {
cin>>name[i];
ofileobj...name[i]...endl;
i++;
}
ofileobj.close();
cout..."Enter the file name where output has to be written: \n ";
cin>>ifilename;
ifstream inobj(ofilename,ios::in);
ofileobj.open(ifilename,ios::out);
cout..."The details of the person in reverse that are entered is:\n";
while(inobj >> temp != NULL)
{
cout...strrev(temp)...endl;
ofileobj...temp...endl;
}
getch();
return 0;
}
if you see .. then remove that . and put < in that place..
8. Write a C++ program to read series of names, one per line, from standard
input and write these names spelled in reverse order to the standard
output using I/O redirection and pipes. Repeat the exercise using an
input file specified by the user instead of the standard input and using an
output file specified by the user instead of the standard output.
#include..fstream.h>
#include..iostream.h>
#include..string.h>
#include..conio.h>
int main()
{
char name[10][20];
char temp[20];
char ifilename[15], ofilename[15];
int num;
int i=0;
cout..."Enter how many person details you want to enter: "; cin>>num;
cout..."Enter the file name where it has to be written: "; cin>>ofilename;
ofstream ofileobj(ofilename,ios::out);
cout..."Enter person details: ";
while(i
cin>>name[i];
ofileobj...name[i]...endl;
i++;
}
ofileobj.close();
cout..."Enter the file name where output has to be written: \n ";
cin>>ifilename;
ifstream inobj(ofilename,ios::in);
ofileobj.open(ifilename,ios::out);
cout..."The details of the person in reverse that are entered is:\n";
while(inobj >> temp != NULL)
{
cout...strrev(temp)...endl;
ofileobj...temp...endl;
}
getch();
return 0;
}
Saturday, February 20, 2010
Obtain the topological ordering of vertices in a given graph
WHEREVER U SEE ... REMOVE THAT . AND PUT << IN THAT PLACE.
IF YOU SEE .. THEN REMOVE THAT . AND PUT < IN THAT PLACE..
7.#include..iostream.h>
#include..stdlib.h>
class topo
{
private:
int n;
int a[10][10];
int indeg[10];
public:
void read_data();
void find_indeg();
void topological_sort();
};
void topo:: read_data()
{
cout..."enter the no of vertices \n";
cin>>n;
cout..."enter the adjacency matrix \n";
for(int i=0;i..n;i++)
{
for(int j=0;j..n;j++)
{
cin>>a[i][j];
}
}
}
void topo:: find_indeg()
{
for(int j=0;j..n;j++)
{
int sum=0;
for(int i=0;i..n;i++)
{
sum+=a[i][j];
}
indeg[j]=sum;
}
}
void topo:: topological_sort()
{
int u,v,t[10],s[10];
find_indeg();
int top=-1;
int k=0;
for(int i=0;i..n;i++)
{
if(indeg[i]==0)
{
s[++top]=i;
}
}
while(top!=-1)
{
u=s[top--];
t[k++]=u;
for(v=0;v..n;v++)
{
if(a[u][v]==1)
{
indeg[v]--;
if(indeg[v]==0)
{
s[++top]=v;
}
}
}
}
cout..."the topological sequence is \n";
for(i=0;i..n;i++)
cout...t[i]..." ";
}
void main()
{
topo t;
t.read_data();
t.topological_sort();
}
Output
Enter the no of vertices
4
Enter the adjacency matrix
0 1 1 0
0 0 0 1
0 0 0 0
0 0 1 0
The topological sequence is
0 1 3 2 Press any key to continue
IF YOU SEE .. THEN REMOVE THAT . AND PUT < IN THAT PLACE..
7.#include..iostream.h>
#include..stdlib.h>
class topo
{
private:
int n;
int a[10][10];
int indeg[10];
public:
void read_data();
void find_indeg();
void topological_sort();
};
void topo:: read_data()
{
cout..."enter the no of vertices \n";
cin>>n;
cout..."enter the adjacency matrix \n";
for(int i=0;i..n;i++)
{
for(int j=0;j..n;j++)
{
cin>>a[i][j];
}
}
}
void topo:: find_indeg()
{
for(int j=0;j..n;j++)
{
int sum=0;
for(int i=0;i..n;i++)
{
sum+=a[i][j];
}
indeg[j]=sum;
}
}
void topo:: topological_sort()
{
int u,v,t[10],s[10];
find_indeg();
int top=-1;
int k=0;
for(int i=0;i..n;i++)
{
if(indeg[i]==0)
{
s[++top]=i;
}
}
while(top!=-1)
{
u=s[top--];
t[k++]=u;
for(v=0;v..n;v++)
{
if(a[u][v]==1)
{
indeg[v]--;
if(indeg[v]==0)
{
s[++top]=v;
}
}
}
}
cout..."the topological sequence is \n";
for(i=0;i..n;i++)
cout...t[i]..." ";
}
void main()
{
topo t;
t.read_data();
t.topological_sort();
}
Output
Enter the no of vertices
4
Enter the adjacency matrix
0 1 1 0
0 0 0 1
0 0 0 0
0 0 1 0
The topological sequence is
0 1 3 2 Press any key to continue
Sort a given set of elements using Selection sort and hence find the time required to sort the elements
WHEREVER U SEE ... REMOVE THAT . AND PUT << IN THAT PLACE.
IF YOU SEE .. THEN REMOVE THAT . AND PUT < IN THAT PLACE..
6.#include ..iostream.h>
#include ..stdlib.h>
#include ..time.h>
const long max = 10000;
void sleep(clock_t wait)
{
clock_t goal;
goal = wait + clock();
while(goal > clock())
;
}
void selsrt(int *,int);
//Main function
void main()
{
double duration;
clock_t st,et;//start and end time
int a[max];//array to be sorted
int n;//input array size
cout..."enter array size\n";
cin>>n;
srand((unsigned)time(NULL));
cout..."Enter the array\n";
for (int i=0;i..n;i++)
cin>>a[i];
st=clock();
sleep((clock_t)1 * CLOCKS_PER_SEC);
selsrt(a,n);
et=clock();
cout..."the sorted array is:\n";
for(i=0;i..n;i++)
cout...a[i]..." ";
cout...endl;
duration=(double)(et-st)/CLOCKS_PER_SEC;
cout..."Time taken :-\t"...duration..." sec"...endl;
}
//Selection sort function
void selsrt(int a[],int n)
{
int min,temp;
for(int i=0;i..n-1;i++)
{
min=i;
for(int j=i+1;j..n;j++)
if(a[j]..a[min])
min = j;
temp = a[i];
a[i] = a[min];
a[min] = temp;
}
}
Output
Enter array size
9
Enter the array
55
66
99
88
77
44
22
33
11
The sorted array is:
11 22 33 44 55 66 77 88 99
Time taken: - 1 sec
Press any key to continue
IF YOU SEE .. THEN REMOVE THAT . AND PUT < IN THAT PLACE..
6.#include ..iostream.h>
#include ..stdlib.h>
#include ..time.h>
const long max = 10000;
void sleep(clock_t wait)
{
clock_t goal;
goal = wait + clock();
while(goal > clock())
;
}
void selsrt(int *,int);
//Main function
void main()
{
double duration;
clock_t st,et;//start and end time
int a[max];//array to be sorted
int n;//input array size
cout..."enter array size\n";
cin>>n;
srand((unsigned)time(NULL));
cout..."Enter the array\n";
for (int i=0;i..n;i++)
cin>>a[i];
st=clock();
sleep((clock_t)1 * CLOCKS_PER_SEC);
selsrt(a,n);
et=clock();
cout..."the sorted array is:\n";
for(i=0;i..n;i++)
cout...a[i]..." ";
cout...endl;
duration=(double)(et-st)/CLOCKS_PER_SEC;
cout..."Time taken :-\t"...duration..." sec"...endl;
}
//Selection sort function
void selsrt(int a[],int n)
{
int min,temp;
for(int i=0;i..n-1;i++)
{
min=i;
for(int j=i+1;j..n;j++)
if(a[j]..a[min])
min = j;
temp = a[i];
a[i] = a[min];
a[min] = temp;
}
}
Output
Enter array size
9
Enter the array
55
66
99
88
77
44
22
33
11
The sorted array is:
11 22 33 44 55 66 77 88 99
Time taken: - 1 sec
Press any key to continue
Check whether the given graph is connected or not using DFS method
WHEREVER U SEE ... REMOVE THAT . AND PUT << IN THAT PLACE.
IF YOU SEE .. THEN REMOVE THAT . AND PUT < IN THAT PLACE..
5.#include..iostream.h>
#include..conio.h>
#include..stdlib.h>
const int MAX=15;
class dfs
{
int adj[MAX][MAX];
int reach[MAX];
int n;
public:
void dfsearch(int,int &);
void adjmatrix();
void dfsc();
};
//function to read the matrix
void dfs::adjmatrix()
{
cout..."Enter the vertices"...endl;
cin>>n;
cout..."Enter the adjacency matrix"...endl;
for(int i=1;i..=n;i++)
for(int j=1;j..=n;j++)
cin>>adj[i][j];
for(i=1;i..=n;i++)
reach[i]=0;
}
//function to traverse the vertices in the graph
void dfs::dfsearch(int u,int &count)
{
reach[u]=1;
count++;
cout..."\t"...u;
for(int j=1;j..=n;j++)
{
if(adj[u][j]!=0)
{
if(!reach[j])
dfsearch(j,count);
}
}
}
//function to check whether the graph is connected or not
void dfs::dfsc()
{
int count=0;
dfsearch(1,count);
if(count==n)
cout...endl..."Graph is connected\n";
else
cout...endl..."Graph is not connected\n";
}
void main()
{
dfs d;
cout..."Depth first search"...endl;
d.adjmatrix();
d.dfsc();
getch();
}
Output
Depth first search
Enter the vertices
4
Enter the adjacency matrix
0 1 0 1
1 0 1 0
0 1 0 1
1 0 1 0
1 2 3 4
Graph is connected
Press any key to continue
Depth first search
Enter the vertices
4
Enter the adjacency matrix
0 1 0 1
1 0 0 1
0 0 0 0
1 1 0 0
1 2 4
Graph is not connected
Press any key to continue
IF YOU SEE .. THEN REMOVE THAT . AND PUT < IN THAT PLACE..
5.#include..iostream.h>
#include..conio.h>
#include..stdlib.h>
const int MAX=15;
class dfs
{
int adj[MAX][MAX];
int reach[MAX];
int n;
public:
void dfsearch(int,int &);
void adjmatrix();
void dfsc();
};
//function to read the matrix
void dfs::adjmatrix()
{
cout..."Enter the vertices"...endl;
cin>>n;
cout..."Enter the adjacency matrix"...endl;
for(int i=1;i..=n;i++)
for(int j=1;j..=n;j++)
cin>>adj[i][j];
for(i=1;i..=n;i++)
reach[i]=0;
}
//function to traverse the vertices in the graph
void dfs::dfsearch(int u,int &count)
{
reach[u]=1;
count++;
cout..."\t"...u;
for(int j=1;j..=n;j++)
{
if(adj[u][j]!=0)
{
if(!reach[j])
dfsearch(j,count);
}
}
}
//function to check whether the graph is connected or not
void dfs::dfsc()
{
int count=0;
dfsearch(1,count);
if(count==n)
cout...endl..."Graph is connected\n";
else
cout...endl..."Graph is not connected\n";
}
void main()
{
dfs d;
cout..."Depth first search"...endl;
d.adjmatrix();
d.dfsc();
getch();
}
Output
Depth first search
Enter the vertices
4
Enter the adjacency matrix
0 1 0 1
1 0 1 0
0 1 0 1
1 0 1 0
1 2 3 4
Graph is connected
Press any key to continue
Depth first search
Enter the vertices
4
Enter the adjacency matrix
0 1 0 1
1 0 0 1
0 0 0 0
1 1 0 0
1 2 4
Graph is not connected
Press any key to continue
Subscribe to:
Comments (Atom)