Pr.Pg Next Pg

Sorting of Array Structures tutorials

 

 

Example: Program to write a structure that accepts the name,author and price of a book and then sorts the books by the name of the author and the name of the book

Sam Madam

/*WAP to write a structure that accepts the name,author and price of a book and then sorts the books by the name of the author and the name of the book

*/

#include <stdio.h>

#include <conio.h>

#include <string.h>

 

struct book

{

char name[20];

char author[20];

int price;

};

 

//swapping the structure

void swap (book &x,book &y)

{

book temp;

temp=x;

x=y;

y=temp;

}

 

//sorting books according to author

void sortauthor (book x[],int n)

{

for (int i=0;i<n-1;i++)

for (int j=i+1;j<n;j++)

if (strcmpi(x[i].author,x[j].author)>0)

swap(x[i],x[j]);

}

 

//sorting books according to title name

void sortname(book x[],int n)

{

for (int i=0;i<n-1;i++)

for (int j=i+1;j<n;j++)

if (strcmpi(x[i].name,x[j].name)>0)

swap(x[i],x[j]);

}

 

//display books

void display (book x[],int n)

{

printf("\nSr. No \t\t Name \t\t Author \t Price\n");

 

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

printf("%d \t\t %s \t\t %s \t %d\n",(i+1),x[i].name,x[i].author,x[i].price);

}

 

void main()

{

book b[100];

int n;

printf("Enter the no. of books ");

scanf("%d",&n);

printf("\nBook information");

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

{

printf("\nEnter name : ");

scanf("%s",&b[i].name);

printf("\nEnter author name : ");

scanf("%s",&b[i].author);

printf("\nEnter price : ");

scanf("%d",&b[i].price);

 

}

printf("\nBook list");

sortname (b,n);

display (b,n);

printf("\n\nList according to author");

sortauthor(b,n);

display(b,n);

getch();

}

 

 

Output

Enter the no. of books 3

 

Book information

Enter name : C

 

Enter author name : balaguruswamy

 

Enter price : 450

 

Enter name : C++

 

Enter author name : stroustrup

 

Enter price : 500

 

Enter name : Java

 

Enter author name : Schildt

 

Enter price : 550

 

Book list

Sr. No Name Author Price

1 C balaguruswamy 450

2 C++ stroustrup 500

3 Java Schildt 550

 

 

List according to author

Sr. No Name Author Price

1 C balaguruswamy 450

2 Java Schildt 550

3 C++ stroustrup 500

 


 

Example: Program to write a structure to enter the number of employees,name ,code and salary of the employee.The program should sort the employees in descending order of their salaries

Sam Madam

/*WAP to write a structure to enter the number of employees,name ,code and salary of the employee.The program should sort the employees in descending order of their salaries

*/

#include <stdio.h>

#include <conio.h>

#include <string.h>

 

struct emp

{

char name[20];

int code;

float sal;

};

emp read()

{

emp e;

printf("Enter name: ");

scanf("%s",&e.name);

printf("Enter code no: ");

scanf("%d",&e.code);

printf("Enter salary: ");

scanf("%f",&e.sal);

return e;

}

void display(emp e)

{

printf("Name: %s\n",e.name);

printf("Code no: %d\n",e.code);

printf("Salary: %f\n",e.sal);

}

short search(emp e[], int n, char name[])

{

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

if(strcmp(e[i].name, name)==0)

break;

return (i < n);

}

void sort(emp e[], int n)

{

for(int i = 1; i < n; i++)

for(int j = 0; j < n - i; j++)

if(e[j].sal < e[j+1].sal)

{

emp temp = e[j];

e[j] = e[j+1];

e[j+1] = temp;

}

}

void main()

{

emp e[100];

int n;

printf("Enter no of emloyees: ");

scanf("%d",&n);

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

e[i] = read();

sort(e, n);

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

display(e[i]);

printf("Enter name to be searched: ");

char name[20];

scanf("%s",&name);

if(search(e, n, name))

printf("Name Found\n");

else printf("Not found\n");

getch();

}

 

Output

Enter no of emloyees: 3

Enter name: Jack

Enter code no: 1000

Enter salary: 25000

Enter name: Peter

Enter code no: 1001

Enter salary: 28000

Enter name: Mark

Enter code no: 1002

Enter salary: 30000

Name: Mark

Code no: 1002

Salary: 30000.000000

Name: Peter

Code no: 1001

Salary: 28000.000000

Name: Jack0

Code no: 1000

Salary: 25000.000000

Enter name to be searched: Peter

Name Found

 


 

Example: Program to write a structure student that accepts the name,cet marks and the maths marks.

The program should display the the merit list by sorting it with the maths marks and then by sorting it with the name of the student

Sam Madam

/* WAP to write a structure student that accepts the name,cet marks and the maths marks.

The program should display the the merit list by sorting it with the maths marks and then by sorting it with the name of the student

*/

#include <stdio.h>

#include <conio.h>

#include <string.h>

 

struct student

{

char name[20];

int regno;

int cet;

int math;

};

void swap (student &x,student &y)

{

student temp;

temp=x;

x=y;

y=temp;

}

 

void sortmarks (student x[],int n)

{

for (int i=0;i<n-1;i++)

for (int j=i+1;j<n;j++)

if (x[i].cet<x[j].cet)

swap (x[i],x[j]);

else if (x[i].cet==x[j].cet && x[i].math<x[j].math)

swap (x[i],x[j]);

}

 

void sortname(student x[],int n)

{

for (int i=0;i<n-1;i++)

for (int j=i+1;j<n;j++)

if (strcmpi(x[i].name,x[j].name)>0)

swap(x[i],x[j]);

}

 

void display (student x[],int n)

{

printf("\nSr. no \t Name \t CET \t Math\n");

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

printf("\n%d \t %s \t %d \t %d",i,x[i].name,x[i].cet,x[i].math);

}

 

void main()

{

student s[100];

int n;

printf("Enter the no. of students ");

scanf("%d",&n);

printf("Student information");

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

{

printf("\nEnter name : ");

scanf("%s",&s[i].name);

printf("\nEnter reg no : ");

scanf("%d",&s[i].regno);

printf("\nEnter CET marks : ");

scanf("%d",&s[i].cet);

printf("Enter Maths marks : ");

scanf("%d",&s[i].math);

}

printf("\nMerit list");

sortmarks(s,n);

display(s,n);

printf("\nList according to name");

sortname(s,n);

display(s,n);

getch();

}

 

 

Output

Enter the no. of students 5

Student information

Enter name : Mohan

 

Enter reg no : 001

 

Enter CET marks : 180

Enter Maths marks : 92

 

Enter name : Rahul

 

Enter reg no : 002

 

Enter CET marks : 150

Enter Maths marks : 90

 

Enter name : Vivek

 

Enter reg no : 003

 

Enter CET marks : 152

Enter Maths marks : 85

 

Enter name : Pram

 

Enter reg no : 004

 

Enter CET marks : 120

Enter Maths marks : 85

 

Enter name : Mehul

 

Enter reg no : 005

 

Enter CET marks : 135

Enter Maths marks : 75

 

Merit list

Sr. no Name CET Math

 

0 Mohan 180 92

1 Vivek 152 85

2 Rahul 150 90

3 Mehul 135 75

4 Pram 120 85

List according to name

Sr. no Name CET Math

 

0 Mehul 135 75

1 Mohan 180 92

2 Pram 120 85

3 Rahul 150 90

4 Vivek 152 85

Pr.Pg border                                              Next Pg