"The best way to cheer yourself up is to try to cheer somebody else up." Mark Twain

Friday, January 15, 2010

Write a program, which will read in student records and print them in sorted order. Each record should be represented by a C structure having member/fields for the first name, last name and student number(an integer). You should sort first by first name, then by last name (this way they will be fully sorted by name when you finish). You can read the information in from the console or from a file, and should print out the sorted list to the console. You can assume some reasonable upper limit on number of records (say , 25) if you want to use an array.


#include<iostream.h>
#include<conio.h>
#include<stdio.h>

// program is done by console input and the array with upper limit 25



struct student
{
char fname[20];
char lname[20];
int id_no;
};
typedef student S;
void main()
{
S s[25];

char tfname[20], tlname[20];
int t;
for(int i = 0; i<25; i++)
{

cout<<"\n Enter Student's first name:";
cin>>s[i].fname;
cout<<"\n Enter Student's last name:";
cin>>s[i].lname;
cout<<"\n Enter ID NO";
cin>>s[i].id_no;
}

for(i = 0; i<24; i++)

{
for(int j = i+1; j<25; j++)
{
if(strcmp(s[i].fname, s[j].fname)>0)
{
tfname = s[i].fname;
s[i].fname = s[j].fname;
s[j].fname = tfname;

tlname = s[i].lname;

s[i].lname = s[j].lname;
s[j].lname = tlname;

t = s[i].id_no;
s[i].id_no = s[j].id_no;
s[j].id_no = t;
}
else
{
 if(strcmp(s[i].fname, s[j].fname)==0)

  {
       if(strcmp(s[i].lname, s[j].lname)>0)        {tfname = s[i].fname;


         s[i].fname = s[j].fname;
         s[j].fname = tfname;

         tlname = s[i].lname;
         s[i].lname = s[j].lname;
         s[j].lname = tlname;


         t = s[i].id_no;
         s[i].id_no = s[j].id_no;
         s[j].id_no = t;
        }
}
}
}

cout<<"\n\n FIRST NAME \t LASTNAME \t ID NO ";

for(int i = 0; i<25; i++)

{
cout<<"\n"<< c[i].fname<<"\t" <lt; c[i].lname <<="" <
}
getch();
}



 



No comments:

Post a Comment

Loading...
C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off.
Now Playing: Ballade Pour Adeline

About Me

My Photo
I m an IT lecturer of a college. I love social-work. I want to do something beneficial for society before dying , that can promote our society, to some extent.