ET4102 Answers
ET4102 Answers
A sentinel controlled loop is used when the number of iterations is not known in
advance. It continues looping until a special value (sentinel) is encountered, which
signals the end of input (e.g., reading numbers until -1 is entered).
Merits:
Demerits:
• -fgnu-runtime:
Use the GNU Objective-C runtime instead of the NeXT runtime.
• -fno-nil-receivers:
Disables the assumption that sending messages to nil (null objects) is safe.
This makes the compiler generate more defensive code.
GNU Binary Utilities (binutils) are a collection of tools for working with binary files.
They include:
Given:
python
CopyEdit
Results:
• p << 2 = 160
• q >> 2 = 20
• p | q = 120
python
CopyEdit
tuple_1 = (1, 0, 4, 6)
tuple_2 = (5, 6, 7, 9)
Output:
python
CopyEdit
10. Using the imaging library write a Python code to convert a color image to
grayscale image
python
CopyEdit
image = Image.open("color_image.jpg")
gray_image = image.convert("L")
gray_image.save("gray_image.jpg")
PART B — (5 × 13 = 65 marks)
1. Editor
2. Preprocessor
3. Compiler
4. Assembler
5. Linker
• Combines all object files and libraries into a final executable (.exe or .out).
6. Debugger
• Tools like GDB used for breakpoints, memory inspection, and stepwise
execution.
Diagram:
pgsql
CopyEdit
+-------+
| Debug |
+-------+
CopyEdit
#include <stdio.h>
void menu() {
printf("\nMenu:\n");
printf("1. Addition\n");
printf("2. Subtraction\n");
printf("3. Exit\n");
}
int main() {
int a, b, choice;
do {
menu();
scanf("%d", &choice);
switch (choice) {
case 1:
break;
case 2:
break;
case 3:
printf("Exiting program.\n");
break;
default:
printf("Invalid choice.\n");
return 0;
12. (a)
(i) Explain the need for Project Header and Port Header files
In C/C++ development, header files are crucial for organizing code and enabling
reuse. A project header file (.h) defines the interface for a project's components,
while a port header file, often named port.h, contains platform-specific code, often
targeting specific hardware or operating systems.
• Interface Definition:
Project header files define the functions, structures, classes, and other elements that
other parts of the project can use. They provide a contract for interacting with the
project's components.
• Organization:
They help organize code by grouping related functionalities into logical units, making
it easier to manage large projects.
• Reusability:
By clearly defining interfaces, header files enable code to be reused across different
modules and even different projects.
• Example:
A header file might define functions for interacting with a database, a user interface,
or a networking library.
Port header files provide platform-specific code that can be used to adapt the project
to different environments, hardware, or operating systems.
• Conditional Compilation:
They often use #ifdef and #ifndef preprocessor directives to conditionally include
platform-specific code.
• Targeting Hardware:
• Example:
A port header might contain code for interacting with a specific microcontroller, a
specific operating system API, or different hardware interfaces.
• Code Reusability:
Header files allow for the reuse of code across different modules and even different
projects.
• Improved Organization:
They help organize code, making it easier to manage and maintain large projects.
• Enhanced Maintainability:
By clearly defining interfaces, header files make it easier to modify and update code
without affecting other parts of the project.
Using header files can significantly reduce compilation time, especially in large
projects.
In summary: Project header files define the interface of a project, while port header
files provide platform-specific code, making them essential for organizing and
adapting projects to different environments.
Elaboration:
1. 1. Timer Configuration:
2. 2. Interrupt Handling:
3. 3. ISR Implementation:
• The ISR is responsible for handling the timer interrupt and any
necessary actions upon the delay completion.
4. 4. Benefits:
• Non-Blocking: Unlike software delays, which can tie up the CPU while
waiting, hardware delays allow the microcontroller to continue
executing other tasks while the timer runs in the background.
(b) With suitable example explain the creation of loop time out and hardware time
out?
Loop Timeout
• Definition: A loop timeout is a mechanism that limits how long a loop can
execute. If the loop doesn't complete within the specified time, it's terminated,
preventing the program from getting stuck in an infinite loop.
• Example:
#include <stdio.h>
#include <time.h>
int main() {
time_t startTime = time(NULL);
time_t timeout = 5; // Timeout of 5 seconds
while (1) {
// Code to be executed in the loop
printf("Looping...\n");
if (time(NULL) - startTime > timeout) {
printf("Loop timed out!\n");
break; // Exit the loop
}
}
return 0;
}
Hardware Timeout
• Example:
int check_timeout() {
return (SysTick->CTRL & SysTick_CTRL_COUNTFLAG_Msk) != 0;
}
Key Differences
• Scope:
• Purpose:
Loop timeouts are used to prevent indefinite loops in software, while hardware
timeouts are used to prevent indefinite waits in hardware devices.
• Granularity:
Loop timeouts can be configured with relatively fine-grained control, while hardware
timeouts may have limited configuration options.
• Accuracy:
Hardware timeouts are generally more accurate and reliable than software-based
loop timeouts.
• Use loop timeouts when you need to limit the execution time of a loop in your
software.
• Use hardware timeouts when you are working with hardware devices and
need to ensure that the device does not wait indefinitely for a response.
11. (a)
1. Editor: This is where you write your source code. Popular editors include
Visual Studio Code, Sublime Text, and Notepad++.
3. Compiler: The compiler translates the source code into object code. GCC (GNU
Compiler Collection) is a widely used compiler for C.
4. Linker: The linker combines multiple object files into a single executable file.
It resolves references between different object files.
5. Debugger: The debugger helps you find and fix errors in your code. GDB (GNU
Debugger) is a common tool used for debugging C programs.
• Case Statement: The case statement is part of the switch-case construct, which
allows you to execute one block of code among many based on the value of a
variable.
• Do While Statement: The do-while loop is similar to the while loop, but it
guarantees that the loop body is executed at least once before the condition is
checked.
11. (b)
To find the minimum of three numbers, you can write a function like this:
12. (a)
• Port Header: The port header file contains hardware-specific definitions and
includes. It is used to define ports and other hardware-related configurations.
• Interrupts: Interrupts can be used to handle timing events. When the timer
reaches the specified period, it generates an interrupt, and the interrupt
handler executes the delay logic.
12. (b)
13. (a)
GDB (GNU Debugger) is a powerful tool for debugging C programs. It allows you to set
breakpoints, step through code, inspect variables, and more.
3. Assembly: The assembler converts the assembly code into machine code,
generating object files.
4. Linking: The linker combines multiple object files into a single executable file,
resolving references between different object files.
1. Multi-language Support:
o GCC can generate machine code for different platforms (ARM, x86,
PowerPC).
3. Standards Compliance:
o Optimization flags:
▪ -O0: No optimization
8. Wide Adoption:
The GNU Build System, also known as Autotools, is a set of utilities used to create
portable software packages that can be compiled on various UNIX-like systems.
Components of the GNU Build System:
1. Autoconf:
2. Automake:
3. Libtool:
4. Configure Script:
5. Make:
6. Standardization:
o Used by major open-source projects like GNOME, GIMP, and GNU tools.
7. Portability:
• Python uses indentation to define code blocks instead of curly braces {}.
• All statements within a control structure (like if, while, def) must be
consistently indented.
Importance:
Example:
python
CopyEdit
def greet():
Implicit Conversion:
python
CopyEdit
x=5
y = 2.5
Explicit Conversion:
Example:
python
CopyEdit
a = "100"
3. Exception Handling
Syntax:
python
CopyEdit
try:
x = 10 / 0
except ZeroDivisionError:
finally:
print("Done.")
Functions in Python
python
CopyEdit
Dictionaries
python
CopyEdit
Tuples
python
CopyEdit
If-Else Statements
python
CopyEdit
x = 10
if x > 0:
print("Positive")
else:
print("Non-positive")
15. (a) Python Modules, Packages, GUI, Networking
python
CopyEdit
# mymodule.py
def hello():
return "Hello"
bash
CopyEdit
mypackage/
__init__.py
tools.py
Networking Libraries
1. Pillow (PIL):
python
CopyEdit
img = Image.open("img.jpg")
img.show()
2. OpenCV:
Example:
python
CopyEdit
x = [1, 2, 3]
plt.plot(x, y)
plt.title("Sample Graph")
plt.show()
6. (a) Structured Code to Count the Number of Goats Passing a Milk Parlour Using
Embedded C
Theory + Code Explanation:
• A counter variable,
Embedded C Code:
CopyEdit
#include <stdio.h>
#include <stdbool.h>
#include <stdint.h>
bool read_sensor() {
pass_count++;
return true;
return false;
void delay() {
int main() {
uint16_t goat_count = 0;
goat_count++;
last_state = sensor;
return 0;
CopyEdit
#include <stdio.h>
#define TOTAL_SEATS 20
#define BUSINESS_END 10
else
seats[i] = 1;
return i + 1;
int main() {
int choice;
while (1) {
scanf("%d", &choice);
if (choice == 0)
break;
if (choice == 1) {
if (seat != -1) {
print_boarding_pass(seat);
} else {
printf("Business class is full. Do you want Economy class instead? (1 = Yes, 0 =
No): ");
int opt;
scanf("%d", &opt);
if (opt == 1) {
if (seat != -1)
print_boarding_pass(seat);
else
} else {
} else if (choice == 2) {
if (seat != -1) {
print_boarding_pass(seat);
} else {
int opt;
scanf("%d", &opt);
if (opt == 1) {
if (seat != -1)
print_boarding_pass(seat);
else
} else {
} else {
return 0;
This program helps a department store determine whether a customer's credit limit
has been exceeded. The necessary input includes:
1. Account number
Theory Explanation:
Structured C Code:
#include <stdio.h>
int main() {
int accountNumber;
scanf("%d", &accountNumber);
scanf("%f", &beginningBalance);
scanf("%f", &totalCharges);
scanf("%f", &totalCredits);
scanf("%f", &creditLimit);
} else {
return 0;
1. if Statement:
Executes a block of code if a specified condition is true.
printf("Eligible to vote.\n");
2. if-else Statement:
Used when a condition has two outcomes.
printf("Pass\n");
} else {
printf("Fail\n");
3. switch Statement:
Used to execute one block of code among many alternatives.
int grade = 2;
switch (grade) {
Example: project.h
#define LED_ON 1
#define LED_OFF 0
void init_system();
Purpose:
• Provides consistency
• Reduces redundancy
• Easy maintenance
Example: port.h
Purpose:
• Hardware abstraction
Example:
while (!device_ready()) {
if (--timeout == 0) {
printf("Timeout occurred\n");
break;
• Device initialization
Features:
• Multilanguage support
• Platform independence
• Standards compliant
1. Compile a program:
3. Enable optimization:
4. Generate assembly:
gcc -S program.c
5. Compile only:
gcc -c file.c
gprof:
A performance analysis tool for applications compiled with GCC.
Steps:
./prog
3. Analyze:
Features:
• Signal handling
Definition:
Functions are reusable blocks of code.
Syntax:
def greet(name):
print("Hello,", name)
greet("Alice")
Types:
• User-defined
Advantages:
• Code reuse
• Easy debugging
• Modular design
i. Creating List:
print(fruits[1]) # banana
fruits[1] = "orange"
del fruits[2]
Module:
A Python file with functions/classes.
# mymodule.py
Package:
A directory with __init__.py file containing modules.
Usage:
print(mymodule.add(2, 3))
GUI Libraries:
• Tkinter: built-in GUI toolkit
Example:
import tkinter as tk
root = tk.Tk()
root.title("GUI Example")
root.mainloop()
PART C (1 x 15 = 15 marks)
This problem involves analyzing test results of students who took a licensing
examination. The objective is to:
Theoretical Explanation:
• Arrays and counters are used to collect and analyze test results.
C Code:
#include <stdio.h>
int main() {
int result, pass = 0, fail = 0;
scanf("%d", &result);
if (result == 1)
pass++;
else if (result == 2)
fail++;
else {
i--;
printf("\nSummary of Results:\n");
if (pass > 8) {
printf("Bonus to instructor!\n");
return 0;
Theoretical Explanation:
Python Code:
A = {1, 2, 3, 4, 5}
B = {4, 5, 6, 7, 8}
print("Set A:", A)
print("Set B:", B)
print("Intersection:", A & B)
print("Union:", A | B)
print("Difference (A - B):", A - B)
print("Symmetric Difference:", A ^ B)
These operations are widely used in data processing, filtering, and logic comparisons.