u

Friday 14 September 2012

C++ Program to define a function and incorporate the array of structures to input and store any 30 records

C++ Program to define a function and incorporate the array of structures to input and store any 30 records

Display the following using functions incorporated through structure definition viz. display of all entered records,sort the records according to the marks in descending order and display of sorted records.



#include<iostream.h>

#include<conio.h>

#include<string.h>

#include<stdio.h>

struct ward

{

char name[20];

float marks;

} stud[5];

void display();

void sort();

void dispsort();

main()

{

clrscr();

// Data entry

clrscr();

cout<<"D A T A : ENTRY PROCESS: Enter 5 Records Initially"<<endl;

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

{

cout<<"\nEnter name";

cin>>stud[i].name;

cout<<"\nEnter marks";

cin>>stud[i].marks;

}

       clrscr();

       display(); // Call of Function

       // to display data entered.

       cout<<"Press any key to continue";

       getch();

       clrscr();

       sort(); // Call of Function

// to sort data

       clrscr();

       cout<<"Press any key to display ";

       cout<<"the sorted data";

       getch();

       dispsort(); // Call of Function

// to display the sorted data.

       cout<<endl;

       cout<<"Press any key to continue";

       getch();

       return 0;

}

void display() // Definition of the function

// to display the data values.

{

clrscr();

cout<<"DATA LIST AS ENTERED";

cout<<endl;

for(int k=0;k<5;k++)

{

cout<<"NAME OF THE STUDENT IS ";

cout<<stud[k].name;

cout<<endl;

cout<<"HIS MARKS IS ";

cout<<stud[k].marks;

cout<<endl;

cout<<"***************************************";

cout<<endl;

cout<<"***************************************";

cout<<endl;

}

}

void sort() // Definition of the sort function.

{

// sorting done according to marks.

char nam[20];

int mark=0;

for(int r=0;r<4;r++)

{

for(int s=r+1;s<5;s++)

{

if (stud[r].marks < stud[s].marks)

{

strcpy(nam,stud[r].name);

strcpy(stud[r].name,stud[s].name);

strcpy(stud[s].name,nam);

mark=stud[r].marks;

stud[r].marks=stud[s].marks;

stud[s].marks=mark;

}

}

}

}

void dispsort() // Definition of the function

// to print the sorted data.

{

clrscr();

cout<<"Display of sorted data according to marks";

cout<<endl;

display();

cout<<endl;

cout<<"End of data encountered";

cout<<endl;

cout<<"Press any key to continue";

getch();

}

0 comments:

Post a Comment

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites More