C Mod 1 BCA Honours
C Mod 1 BCA Honours
BCA (Honours)
Module 1
Objectives:
Example:
Suppose you are tasked with writing a program that calculates the total price of items in
a shopping cart, applying discounts when applicable.
● Problem Statement: Write a program that reads the price of each item in a
shopping cart and applies a 10% discount to items that cost more than Rs. 1000.
The program should output the total price after all discounts are applied.
● Inputs: Prices of items.
● Outputs: Total price after applying discounts.
● Constraints: Only items costing more than Rs. 1000 will receive a discount.
Understanding this clearly is the first step toward building the right solution.
Objectives:
Example:
For the shopping cart problem:
● Subtasks:
1. Read the prices of items from the user.
2. Check if each price is greater than Rs. 1000 and apply a 10% discount if so.
3. Sum the total prices after applying discounts.
● Edge Cases:
1. What if there are no items in the cart?
2. What if all items are below Rs. 1000?
● Algorithm:
1. Start.
2. Read the prices of all items in the cart.
3. For each item, if the price is greater than Rs. 1000, apply a 10% discount.
4. Sum the total prices of all items.
5. Output the total price.
6. End.
Hierarchy Charts
A Hierarchy Chart, also known as a structure chart, visually represents the organization
of a program. It breaks down the program into modules or functions, showing their
relationships in a hierarchical manner.
Characteristics:
A program to calculate the payroll for employees could have a hierarchy chart as follows:
Benefits:
Top-down Approach
The Top-down approach is a program design strategy where the overall system is
defined first, and then it is broken down into smaller, more detailed components. The
focus starts at the high level, gradually moving towards the finer details.
In this approach, the given problem is divided into two or more sub problems, each of
which resembles the original problem. The solution of each sub problem is taken out
independently.
Finally, the solution of all sub problems is combined to obtain the solution of the main
problem. The following figure shows the meaning of top down approach.
● Starts by identifying the main problem or system and then decomposes it into
smaller subproblems.
● Each subproblem is further broken down until the solution becomes simple
enough to implement.
● Focuses on modular design, ensuring that each module handles a specific task.
Bottom-up Approach
This technique is just reverse of the top down programming. In this programming
technique, the solutions of the independent sub-problems are designed first. Then these
solutions are combined or composed in a main module in order to design the final
solution of the problem. The following figure shows the bottom-up approach.
Algorithms
An algorithm is a step-by-step, well-defined procedure or set of instructions designed to
solve a problem or perform a specific task. It is the blueprint for writing the code.
Algorithms are used to plan out the logic before the actual coding process begins.
Attributes of an Algorithm:
Example:
An algorithm for finding the sum of two numbers could be:
1. Start
2. Input two numbers (A and B)
3. Add the numbers and store the result in a variable called "Sum"
4. Output the value of "Sum"
5. Stop
Flow Chart
A flowchart is a graphical representation of an algorithm or process. It uses symbols and
arrows to depict the flow of control in a system or algorithm, showing the sequence of
steps to solve a problem. Flowcharts help visualize the structure of an algorithm before
implementation.
Attributes of a Flowchart:
1. Flow: It shows the flow of control from one process to another, following a logical
sequence.
2. Clarity: The symbols and flow should be clear and easily understandable.
3. Modularity: Flowcharts often break down complex problems into smaller,
manageable parts, which can be represented in separate flowcharts.
4. Symbols: Various symbols represent different types of operations or decisions in
the process.
Symbol Meaning
The following flowchart is for the program for finding the greatest number from given
three numbers.
Statements
Input-Output Statements
These are the basic statements that handle the input and output operations in a
program.
● Input Statements: Commands that allow the user to provide input to the program.
○ Example in C: scanf("%d", &num); for reading an integer.
● Output Statements: Commands that display information to the user.
○ Example in C: printf("Sum is %d", sum); for printing the result.
Decision-Making Statements
Looping Statements
Module Representation
Modules represent the division of a program into separate functions or procedures, each
performing a specific task. This approach promotes modularity, making the program
easier to manage, test, and maintain.
In this example, addNumbers is a module (function) that performs the task of adding
two numbers and printing the result. The main function calls this module to solve part
of the problem.
Introduction to Programming
Computer Program
A computer program is a set of instructions written in a programming language that a
computer executes to perform a specific task or set of functions. A program acts as a
bridge between the computer hardware and the user, instructing the computer how to
process input data and generate the desired output.
1. Machine Language
2. Assembly Language
3. High-Level Language
Machine Language
Machine language (or machine code) is the most basic programming language,
consisting of binary digits (0s and 1s). This is the only language directly understood by
the computer’s CPU.
Characteristics:
● No Translation Required: Machine language does not need to be translated, as it is
directly executed by the CPU.
● Hardware-Specific: Machine code is specific to the CPU architecture (e.g., x86,
ARM).
● Difficult to Understand: Writing programs in machine language is complex, as
the programmer must manage memory addresses, CPU registers, and instruction
sets.
Assembly Language
Characteristics:
● Requires an assembler to convert the assembly code into machine code.
● Still hardware-specific, but more readable than machine language.
● Easier to debug than machine language but still challenging compared to
high-level languages.
High-Level Language
High-level languages are closer to human languages and provide greater abstraction
from the hardware. They allow programmers to write complex algorithms without
worrying about the underlying machine architecture. Example: C, C++, Java, etc
Characteristics:
● Translator Required: High-level languages require a compiler or interpreter to
translate the code into machine language.
● Portable: High-level languages are hardware-independent, meaning the same
code can run on different types of hardware with minimal changes.
● Easy to Read and Write: These languages use human-readable syntax, making
programming more accessible and reducing development time.
Language Translator
Language translators are essential tools that convert high-level or assembly language
programs into machine language so that computers can understand and execute them.
Without translators, programs written by humans in various languages would be useless
to the computer hardware.
1. Assembler
2. Compiler
3. Interpreter
Assembler
● The assembler reads the assembly code line by line and converts each mnemonic
into its equivalent machine instruction.
● It also handles any symbolic references (labels, variables) in the program by
translating them into actual memory addresses.
● The output of an assembler is an object file, which contains the translated
machine code, ready for execution by the CPU or further processing by a linker.
Compiler
● Reads the entire source code and breaks it into tokens (keywords, identifiers,
operators).
● Checks the tokens against the language's grammatical rules, constructing a parse
tree to represent the code structure.
● Validates the logic of the code, ensuring it adheres to the rules (e.g., data type
compatibility).
● Improves the performance of the code by optimizing instructions, reducing
resource usage, and eliminating redundancies.
● Translates the optimized code into machine language, producing an object code
that the CPU can execute.
Interpreter
An interpreter is a translator that translates and executes high-level language code line
by line, without producing a complete machine code program before execution. Unlike a
compiler, which processes the entire code at once, an interpreter translates a single line
of code and immediately executes it.
● The interpreter reads a single line or instruction from the source code.
● It translates the instruction into machine code and executes it immediately.
● The process continues line by line until the entire program is executed
Linker
Linker is a utility program in the software development process that plays a critical role
in creating a final executable file from object files generated by the compiler or
assembler. When a program is written in multiple modules or uses external libraries,
the linker is responsible for combining these modules and resolving any references
between them. Linkers are also called as link editors.
● Symbol Resolution: The linker resolves symbols, which are references to variables
or functions used in one module but defined in another. If the linker cannot
resolve a symbol, it generates an error indicating that the reference is undefined.
● Relocation: Each object file assumes it starts at address 0. The linker adjusts these
addresses so that all object files can coexist in memory without conflict. This
process is called relocation, and it ensures that each part of the program is placed
in its proper memory location.
● Library Linking: The linker must also ensure that external libraries are properly
linked to the program. In dynamic linking, this is done by referencing the shared
library files, which are loaded at runtime.
● Generating the Executable File: Finally, after all modules and libraries are linked,
the linker creates the final executable file. This file contains the machine code and
is ready for execution by the operating system.
Linking
Linking is a process of collecting and maintaining pieces of code and data into a single
file. There are two types of Linking:
1. Static Linking
2. Dynamic Linking
Static Linking
In static linking, all the required libraries and object files are combined into a single
executable file during the linking process. The resulting executable contains everything
it needs to run independently.
Dynamic Linking
In dynamic linking, the program relies on external libraries that are linked at runtime.
The program's executable file doesn’t include the library code itself but instead includes
references to the necessary libraries.
Testing
Testing is the process of evaluating a software application to ensure that it meets
specified requirements, behaves as expected, and is free from defects. It is a critical
phase in the software development life cycle (SDLC) because it ensures that the software
is reliable, efficient, and meets user needs. Effective testing helps uncover bugs, errors,
and inconsistencies that could degrade the quality or functionality of the software.
Validation: It refers to a different set of tasks that ensure that the software that has been
built is traceable to customer requirements. It means “Are we building the right
product?”.
Importance of Testing
Detects Errors: Testing helps identify both obvious and subtle bugs that can cause the
software to malfunction or behave unexpectedly.
Ensures Quality: It ensures the software meets the necessary standards, both
functionally and non-functionally, providing confidence in its reliability, performance,
and security.
Prevents Costly Fixes: Catching and fixing bugs early in the development process is
significantly cheaper and faster than doing so after deployment.
Types of Testing
There are various types of testing in software development, each designed to focus on
specific aspects of the application. These testing types fall under different categories,
depending on their purpose and scope.
Functional Testing: Verifies that the software functions according to its requirements.
Eg: Unit testing, integration testing, system testing, etc.
Manual Testing: Performed by human testers who manually execute test cases without
the help of automation tools. Best for exploratory testing, usability testing, and when the
software is at an early stage.
Automated Testing: Uses automated tools to run predefined test scripts and compare
actual outcomes with expected results. Ideal for regression testing and testing large
systems with repetitive test cases.
Benefits of Testing:
Debugging
Debugging is the process of identifying, analyzing, and removing bugs or errors in the
software. It comes into play after testing has detected issues in the software. Debugging
aims to pinpoint the root cause of a defect and correct it so that the software works as
intended.
Types of Errors
In programming, errors can be classified into three main types:
1. Syntax Errors,
2. Logical Errors
3. Runtime Errors.
Syntax Errors
Syntax errors occur when the program violates the rules of the programming language.
These errors are detected during compilation, and the program won't execute until the
errors are fixed.
Characteristics:
Example:
The error here is the missing semicolon (;) after the printf statement. The correct
code is:
Logical Errors
Logical errors occur when the program runs successfully, but the output or behavior is
incorrect due to flawed logic. The program executes without crashing, but the results are
not as expected. These errors are harder to find because they don't cause a compilation
error.
Characteristics:
Example:
The error here is the logic for calculating the area of a rectangle is incorrect because it
uses addition (+) instead of multiplication (*). The corrected code is:
Runtime Errors
Runtime errors occur while the program is running. These errors are not detected by the
compiler but happen due to illegal operations or unexpected conditions during
execution, such as dividing by zero or accessing memory that doesn't exist.
Characteristics:
Examples:
Division by Zero:
The program crashes because dividing by zero is undefined. You can fix this by checking
the divisor:
Summary of Errors: