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]
Answer
(a) Difference Between Source Code and Object Code
| Basis | Source Code | Object Code |
|---|---|---|
| Definition | The program written by a programmer in a high-level language (e.g., C) that is human-readable | The machine-level code generated by the compiler after translating the source code |
| Readability | Easily readable and understandable by humans | Not directly readable by humans; consists of binary/machine instructions |
| File Extension | Stored as .c (in C language) | Stored as .obj or .o file |
| Processing | Cannot be directly executed by the CPU | Passed to the linker to produce an executable file |
| Error Detection | Syntax errors are detected at this stage during compilation | No syntax errors; errors at this stage are linking errors |
| Generation | Written manually by the programmer | Automatically 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
Bookwith 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
-
Structure Declaration:
struct Bookis defined with three members:Book_Name[50]-- stores the titlePrice-- stores the price as floatAuthor[50]-- stores the author name
-
Array of Structures:
struct Book books[10]creates a list of 10 book records. -
Input Loop: A
forloop runs fromi = 0toi < 10to accept all 10 records usingscanf. -
Condition Check: A second
forloop checksif (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.