Write a program to read Title, Author and Price of 5 books using array of structures. Display the records in ascending order of Price.
Team Answered question April 13, 2024
#include <stdio.h> //#include <conio.h> struct book { int price; char title[80]; char author[80]; }; void accept(struct book list[80]); //func declare void display(struct book list[80]); void bsortAsc(struct book list[80]); void main() { struct book data[20]; int n; //clrscr(); accept(data); //func call bsortAsc(data); display(data); //getch(); } void accept(struct book list[5]) // func initialize { int i; for (i = 0; i <5; i++) { printf("\nEnter title : "); scanf("%s", &list[i].title); printf("Enter Author name: "); scanf("%s",&list[i].author); printf("Enter price : "); scanf("%d", &list[i].price); } } void display(struct book list[80]) { int i; printf("\n\nTitle\t\tAuthor\t\tprice\n"); printf("-------------------------------------\n"); for (i = 0; i<5; i++) { printf("%s\t\t%s\t\t%d\n", list[i].title, list[i].author, list[i].price); } } void bsortAsc(struct book list[80]) { int i, j; struct book temp; for (i = 0; i <5 ; i++) { for (j = 0; j < (5 -i); j++) { if (list[j].price >list[j + 1].price) { temp = list[j]; list[j] = list[j + 1]; list[j + 1] = temp; } } }}
Team Answered question April 13, 2024