2080 3

CSC115 ยท 2080 ยท 10 marks

Differentiate between source code and object code.Create a structure named Book which remembers Book_Name, Price and Author. Then, make the list of top 10 records of book and print the name of authors having the price of book greater than 1000.[3+7]

Differentiate between source code and object code.Create a structure named Book which remembers Book_Name, Price and Author. Then, make the list of top 10 records of book and print the name of authors having the price of book greater than 1000.[3+7]

C ProgrammingChapter 8: Structure and UnionArray of structure

Answer

(a) Difference Between Source Code and Object Code

BasisSource CodeObject Code
DefinitionThe program written by a programmer in a high-level language (e.g., C) that is human-readableThe machine-level code generated by the compiler after translating the source code
ReadabilityEasily readable and understandable by humansNot directly readable by humans; consists of binary/machine instructions
File ExtensionStored as .c (in C language)Stored as .obj or .o file
ProcessingCannot be directly executed by the CPUPassed to the linker to produce an executable file
Error DetectionSyntax errors are detected at this stage during compilationNo syntax errors; errors at this stage are linking errors
GenerationWritten manually by the programmerAutomatically generated by the compiler

Flow (from notes):

source.c --> Compiler --> source.obj --> Linker --> source.exe --> Output

(b) Structure Book with Top 10 Records

Problem Analysis

  • Define a structure Book with fields: Book_Name, Price, Author
  • Store 10 book records
  • Print the names of authors whose book price is greater than 1000

Program

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

/* Define the structure Book */
struct Book {
    char Book_Name[50];
    float Price;
    char Author[50];
};

void main() {
    int i;

    /* Declare an array of 10 Book structures */
    struct Book books[10];

    clrscr();

    /* Input 10 book records */
    printf("Enter details of 10 books:\n");
    for (i = 0; i < 10; i++) {
        printf("\n--- Book %d ---\n", i + 1);

        printf("Enter Book Name  : ");
        scanf(" %[^\n]", books[i].Book_Name);

        printf("Enter Author Name: ");
        scanf(" %[^\n]", books[i].Author);

        printf("Enter Price      : ");
        scanf("%f", &books[i].Price);
    }

    /* Print authors whose book price is greater than 1000 */
    printf("\n\nAuthors with Book Price greater than 1000:\n");
    printf("--------------------------------------------\n");

    for (i = 0; i < 10; i++) {
        if (books[i].Price > 1000) {
            printf("Author: %s  |  Book: %s  |  Price: %.2f\n",
                   books[i].Author,
                   books[i].Book_Name,
                   books[i].Price);
        }
    }

    getch();
}

Sample Output

Enter details of 10 books:

--- Book 1 ---
Enter Book Name  : Let Us C
Enter Author Name: Yashavant Kanetkar
Enter Price      : 850

--- Book 2 ---
Enter Book Name  : The C Programming Language
Enter Author Name: Dennis Ritchie
Enter Price      : 1200

--- Book 3 ---
Enter Book Name  : Data Structures
Enter Author Name: Seymour Lipschutz
Enter Price      : 1500
...

Authors with Book Price greater than 1000:
--------------------------------------------
Author: Dennis Ritchie   |  Book: The C Programming Language  |  Price: 1200.00
Author: Seymour Lipschutz|  Book: Data Structures             |  Price: 1500.00

Explanation of Key Steps

  1. Structure Declaration: struct Book is defined with three members:

    • Book_Name[50] -- stores the title
    • Price -- stores the price as float
    • Author[50] -- stores the author name
  2. Array of Structures: struct Book books[10] creates a list of 10 book records.

  3. Input Loop: A for loop runs from i = 0 to i < 10 to accept all 10 records using scanf.

  4. Condition Check: A second for loop checks if (books[i].Price > 1000) and prints the author name if the condition is true.

Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.