0
888views
What are structures? Comment on nested structures. Write a program to read Title, Author and Price of 10 books using array of structure. Display the records in ascending order of price.

Subject : Structured Programming Approach

Title : Arrays, String, Structures and Union

Difficulty : Medium

1 Answer
0
1views

Structure:

  1. Structure is a collection of multiple data elements that can be of different data types.

  2. Array is a collection of data items of same type, while structure can have data items of different types.

  3. The memory space required to store one variable of structure is equal to the memory space required by all the elements independently to be stored in memory.

Syntax:

struct structure_name

{

    data_type variable_name;

    data_type variable_name;

    -

    -

}

Program:

#include <stdio.h>

struct book

{

    int price;

    char title[80];

    char author[80];

};

void accept(struct book list[80], int s);

void display(struct book list[80], int s);

void bsortDesc(struct book list[80], int s);

void main()

{

    struct book data[20];

    int n;

    clrscr();

    accept(data,10);

    bsortDesc(data, 10);

    display(data, 10);

    getch();

}

void accept(struct book list[80], int s)

{

    int i;

    for (i = 0; i < s; 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 s)

{

    int i;

    printf("\n\nTitle\t\tAuthor\t\tprice\n");

    printf("-------------------------------------\n");

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

    {

        printf("%s\t\t%s\t\t%d\n", list[i].title, list[i].author, list[i].price);

    }

}

void bsortDesc(struct book list[80], int s)

{

    int i, j;

    struct book temp;

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

    {

        for (j = 0; j < (s -i); j++)

        {

            if (list[j].price > list[j + 1].price)

            {

                temp = list[j];

                list[j] = list[j + 1];

                list[j + 1] = temp;

            }

        }

    }

}

Output:

Enter Title: physics

Enter Author name: Einstein

Enter Price:1000


Enter Title:english

Enter Author name:willey

Enter Price:900


Enter Title:chemistry

Enter Author name:narayan

Enter Price:800


Enter Title:history

Enter Author name:george

Enter Price:700


Enter Title:geography

Enter Author name:bailey

Enter Price:600


Enter Title:AOA

Enter Author name:Koreman

Enter Price:500


Enter Title:SPA

Enter Author name:Harish

Enter Price:400


Enter Title:Maths

Enter Author name:kumbhojkar

Enter Price:300


Enter Title:CG

Enter Author name:hern

Enter Price:200


Enter Title:python

Enter Author name:naryan

Enter Price:100

Title Author price
Python Narayan 100
CG hern 200
Maths kumbhojkar 300
SPA Harish 400
AOA koreman 500
Geography bailey 600
History George 700
Chemistry Narayan 800
English willey 900
Physics Einstein 1000
Please log in to add an answer.