YosefRoba ID 1556 15 Assignment 1
YosefRoba ID 1556 15 Assignment 1
UNIVERSITY - EiABC
COMPUTER PROGRAMMING ASSIGNMENT 1
1
1)Distinctions
The distinctions between Syntax, Semantics and Static Semantics are mainly
viewed on their fields of focus i.e. what they focus on.
i. Syntax:
• Focus: The rules that ensure a program is well-formed and consistent before
it's executed.
• Analogy: Think of it like the "type checking" that happens in programming
languages. It ensures that variables are used in a way that makes sense and
avoids potential errors.
Example: In many programming languages, you can't add a number to a string
directly. This is a static semantic rule that prevents a program from executing with
an error.
2
Key Distinctions:
Syntax is about the form, semantics is about the meaning.
Static semantics is a bridge between syntax and semantics. It ensures that the
structure of a program (syntax) is consistent with its intended meaning (semantics).
In summary:
Syntax: "Abebe sat on the grass" is grammatically correct.
Semantics: "Abebe sat on the grass" makes sense.
Static Semantics: A program that tries to add a number to a string would be
flagged as a static semantic error.
3
2)8-bit ASCII
Here is the 8-Bit ASCII representation of my name, "Yosef Roba Megersa"
following your specifications:
3)Programmability
4
iii. Innovation: Programmability fuels innovation. By creating new programs,
we can explore new ideas, develop new solutions, and push the boundaries of
what computers can do.
5
4) Algorithms
An algorithm is like a recipe for solving a problem or completing a task. It's a well-
defined set of instructions that, when followed step-by-step, leads to a specific
outcome. Think of it as a blueprint for achieving a desired result.
6
Examples of Algorithms:
Sorting Algorithms: These algorithms arrange items in a specific order, such as
alphabetically or numerically. Examples include bubble sort, insertion sort, and
quicksort.
Search Algorithms: These algorithms find specific items within a collection of data.
Examples include linear search and binary search.
Navigation Algorithms: These algorithms determine the shortest or most efficient
route between two locations. Examples include Dijkstra's algorithm and A* search.
Encryption Algorithms: These algorithms transform data into a secure format,
making it difficult for unauthorized individuals to access. Examples include RSA
and AES.
Importance of Algorithms:
Algorithms are the backbone of modern technology. They power everything from
search engines and social media platforms to self-driving cars and medical
diagnostics. They are essential for:
Computer Science: Algorithms are the foundation of computer programming and
software development.
Data Science: Algorithms are used to analyze data, extract insights, and make
predictions.
Artificial Intelligence: Algorithms are used to develop intelligent systems that can
learn, reason, and make decisions.
7
a . Let's consider the mathematical expression: (x + y) * z
Sentence-based Explanation:
Start: Begin by initializing three variables: 'x', 'y', and 'z' with their respective input
values.
Addition: Calculate the sum of 'x' and 'y' and store the result in a variable called
'sum'.
Multiplication: Multiply the value of 'sum' by 'z' and store the final result in a
variable called 'product'.
Return: Output the value of 'product' as the result of the expression.
b . Graphical diagrams
i. Flowchart:
graph LR
A[Start] --> B{Initialize x, y, z}
B --> C{sum = x + y}
C --> D{product = sum * z}
D --> E{Return product}
E --> F[End]
Explanation:
Rectangles: Represent actions or processes.
Diamonds: Represent decision points (if-else statements).
Arrows: Indicate the flow of control.
Start and End: Marked by rounded rectangles.
8
ii. Pseudocode:
9
5) Bugs, Debugging and Errors
In the context of computer programming, a "bug" refers to an error, flaw, or defect
in the code that causes a program to behave unexpectedly or incorrectly. These
bugs can range from simple typos to complex logical errors, and they can have a
variety of consequences, from minor annoyances to major system crashes.
Debugging is the process of identifying, analyzing, and fixing these bugs. It's a
crucial part of software development, ensuring that programs function as
intended and meet the desired requirements. Here's a detailed explanation of
debugging, supported by examples:
i. Identifying Bugs:
Unexpected Output: The program produces incorrect results or outputs that differ
from the expected behavior. For example, a calculation program might give a
wrong answer due to a misplaced operator or a faulty mathematical formula.
Program Crashes: The program terminates abruptly or unexpectedly, often
displaying error messages. This could be caused by issues like accessing invalid
memory locations, dividing by zero, or encountering an infinite loop.
Unexpected Behavior: The program behaves in ways that were not intended, such
as displaying the wrong information, ignoring user input, or performing actions
out of sequence.
ii. Analyzing Bugs:
Reproducing the Bug: The first step is to reproduce the bug consistently. This
involves identifying the specific conditions under which the bug occurs, such as a
particular input, sequence of actions, or environment setup.
Tracing the Code: Once the bug is reproducible, the programmer needs to trace
the execution of the code step-by-step to pinpoint the source of the error. This can
be done by using debugging tools like breakpoints, stepping through the code line
by line, and inspecting the values of variables at different points.
10
Analyzing Error Messages: Error messages generated by the compiler or runtime
environment can provide valuable clues about the nature of the bug. These
messages often indicate the specific line of code where the error occurred and the
type of error (e.g., syntax error, runtime error).
iii. Fixing Bugs:
Correcting the Code: Once the bug is identified, the programmer needs to correct
the code to eliminate the error. This may involve fixing a typo, changing a logical
condition, adding missing code, or removing unnecessary code.
Testing the Fix: After making the correction, it's essential to thoroughly test the
code to ensure that the bug is fixed and that the program now behaves as
expected. This may involve running the program with various inputs, scenarios,
and edge cases to ensure that the fix does not introduce new bugs.
Examples of Debugging:
Example 1: Simple Typo
11
Example 2: Logical Error
12
a ) The analogy of "Needle in a haystack" is a fitting one for the debugging
process, highlighting the challenge of finding a tiny error within a vast
amount of code. Here's how I'd break down the analogy:
The Needle:
The bug itself: This is the actual source of the problem, the specific error or flaw in
the code. It's small, often hidden, and can be difficult to locate. Just like a needle,
it's a tiny element within a much larger system.
The specific line of code causing the issue: While the bug is the root cause, the
actual manifestation of the bug might be a single line of code that produces the
unexpected behavior. This line of code is like the needle, the precise point where
the error is triggered.
The Haystack:
The entire codebase: This represents the vast amount of code that makes up the
program. It's like a haystack, full of seemingly similar lines of code, making it
difficult to find the single needle that's causing the issue.
The context surrounding the bug: The bug doesn't exist in isolation. It's influenced
by the surrounding code, variables, and functions. This context can be confusing
and make it harder to isolate the bug, like trying to find a needle in a haystack
that's constantly shifting and changing.
Reasoning and Explanations:
The Needle's Size and Importance: The bug is small, often a single character or a
misplaced line of code, but its impact can be significant. It's like a tiny needle that
can cause a large haystack to unravel.
The Haystack's Complexity: The codebase can be massive, with thousands of lines
of code, making it difficult to search for a single bug. This complexity is like the
haystack's vastness, making it hard to find the needle.
The Search Process: Debugging involves carefully examining the code, testing
different scenarios, and tracing the flow of execution to isolate the bug. This
13
process is like searching through the haystack, meticulously examining each piece
to find the needle.
b ) In programming, errors are very common and hard to avoid. They can stem
from typos, logical flaws, or even interactions with the system. Understanding
the different types of errors is crucial for effective debugging and writing robust
code. Here are some common error types with illustrative examples:
1. Syntax Errors:
Definition: These errors occur when the code violates the grammatical rules of the
programming language. The compiler or interpreter cannot understand the code
and throws an error.
Example:
print("Hello, world!"
Error: SyntaxError: invalid syntax
Explanation: The missing closing parenthesis ) after the string violates the syntax
rules of Python.
2. Runtime Errors (Exceptions):
Definition: These errors occur during the execution of the program, usually due to
unexpected events or conditions that the program is not designed to handle.
Example:
14
3. Logical Errors:
Definition: These errors occur when the code is syntactically correct but does not
produce the intended results. The program runs without crashing, but it does not
behave as expected due to flawed logic.
Example:
Explanation: The code adds the length and width instead of multiplying them,
resulting in an incorrect area calculation.
15
5. Resource Errors:
Definition: These errors occur when the program encounters problems accessing
or managing resources like files, databases, or network connections.
Example:
16
Example:
17
REFERENCES
✓ GitHub: github.org
✓ GeeksforGeeks: geeksforgeeks.org; source for images.
✓ Scribd: www.scribd.com
✓ Udemy: udemy.com
✓ The Rust Programming Language Forum
✓ AAU course materials
18