Unit 1 BCA 101
Unit 1 BCA 101
UNIT I
1. Introduction to Programming & Problem Solving:
1.1 What is Programming?
Programming is the process of designing and creating executable computer software to accomplish
a specific task. It involves writing code in programming languages that a computer can understand
and execute.
• Programming Language: A formal language comprising a set of instructions that can be used
to produce various kinds of output, including software. Examples include C, C++, Java,
Python, etc.
• Syntax: The set of rules that defines the combinations of symbols that are considered to be
correctly structured programs in that language.
• Semantics: The meaning behind the syntax, i.e., what the code does.
Problem-Solving Steps:
1. Understand the Problem: Clearly define what the problem is and what the desired output
should be.
4. Test and Debug: Run the program with various test cases to ensure it works correctly and fix
any issues.
5. Refine the Solution: Optimize the code for efficiency and readability if necessary.
Maharishi University of Information Technology
Example of Problem-Solving:
2. Plan: Use a loop to iterate through numbers and accumulate the sum.
3. Code:
#include <stdio.h>
int main() {
int sum = 0;
for (int i = 1; i <= 10; i++) {
sum += i;
}
printf("Sum: %d\n", sum);
return 0;
}
4. Test and Debug: Ensure the output is 55.
• Definition: A storage device used to save and retrieve digital information. Common types
include hard disk drives (HDDs) and solid-state drives (SSDs).
• Function: Provides long-term storage for the operating system, applications, and data.
Key Concepts:
• Hard Disk Drive (HDD): Uses spinning disks and read/write heads to access data. Generally
has larger storage capacity but slower access times.
• Solid-State Drive (SSD): Uses flash memory to store data. Faster access times and better
durability compared to HDDs but can be more expensive.
2.2 Memory:
• Definition: The component of a computer that temporarily stores data and instructions that
are being used by the CPU.
• Types:
o RAM (Random Access Memory): Volatile memory used for temporary storage while
programs are running. Data is lost when the computer is turned off.
Maharishi University of Information Technology
o ROM (Read-Only Memory): Non-volatile memory that is used to store firmware or
software that is rarely changed.
Key Concepts:
• Non-Volatile Memory: Retains information even when the power is turned off (e.g., ROM,
SSD).
• Definition: The brain of the computer that performs instructions from programs through
basic arithmetic, logic, control, and input/output operations.
• Components:
o Registers: Small, fast storage locations within the CPU for temporary data.
Key Concepts:
• Clock Speed: Measured in GHz, determines how many cycles per second the CPU can
execute.
• Cores: Modern CPUs have multiple cores, allowing them to process multiple instructions
simultaneously.
• Definition: System software that manages hardware resources and provides common
services for computer programs.
• Functions:
• Process:
2.6 Loaders:
• Definition: A loader is responsible for loading executable files into memory and preparing
them for execution.
• Functions:
o Relocation: Adjusts addresses in the code and data to reflect their locations in
memory.
Types:
2.7 Linkers:
• Definition: A linker combines several object files generated by a compiler into a single
executable or library.
• Functions:
Types:
• Dynamic Linking: Libraries are linked during runtime, allowing for updates without
recompiling.
3. Introduction to Algorithms:
An algorithm is a step-by-step procedure or formula for solving a problem. It is a finite sequence of
well-defined instructions that perform a specific task or solve a particular problem.
Characteristics of an Algorithm:
• Effectiveness: Each step should be basic enough to be performed exactly and in a finite
amount of time.
Example Algorithm:
Step-by-Step Approach:
Maharishi University of Information Technology
1. Understand the Problem: Read the problem statement carefully and ensure you understand
what is being asked.
2. Analyze the Problem: Break down the problem into smaller parts and determine what
information you need.
3. Design the Algorithm: Create a step-by-step plan (algorithm) to solve the problem.
5. Convert to Code: Translate the pseudocode or flowchart into actual source code.
6. Test the Program: Run the program with different inputs to ensure it works correctly.
Example:
3. Algorithm:
o Input n.
o Initialize factorial to 1.
o Output factorial.
4. Pseudocode:
Input n
factorial = 1
For i from 1 to n
factorial = factorial * i
Output factorial
5. Code:
#include <stdio.h>
int main() {
int n, i;
long long factorial = 1;
Maharishi University of Information Technology
printf("Enter a number: ");
scanf("%d", &n);
for (i = 1; i <= n; i++) {
factorial *= i;
}
printf("Factorial of %d = %lld\n", n, factorial);
return 0;
}
5. Introduction to Flowcharts/Pseudocode:
Flowcharts:
• Oval: Start/End
• Parallelogram: Input/Output
Example Pseudocode:
START
INPUT a, b
IF a > b THEN
ELSE
END
2. Translate Steps to Code: Write the code in the chosen programming language (e.g., C).
3. Use Proper Syntax: Ensure that the code adheres to the syntax rules of the programming
language.
4. Test and Debug: Validate the code against different inputs to ensure correctness.
Example Conversion:
From the pseudocode for finding the largest of three numbers, you would write the following C
program.
#include <stdio.h>
int main() {
int a, b, c;
printf("Enter three numbers: ");
scanf("%d %d %d", &a, &b, &c);
if (a > b) {
if (a > c) {
printf("Largest number is: %d\n", a);
} else {
printf("Largest number is: %d\n", c);
Maharishi University of Information Technology
}
} else {
if (b > c) {
printf("Largest number is: %d\n", b);
} else {
printf("Largest number is: %d\n", c);
}
}
return 0;
}
Source code is the human-readable code written in a programming language. It is the initial form of
the program before compilation.
Variables:
Variables are named storage locations in memory that hold data. They must be declared before use
and can be of different types such as int, float, char, etc.
Example:
Memory Locations:
Each variable is stored in a specific location in memory. The memory address of a variable can be
accessed using pointers in C.
Example:
#include <stdio.h>
int main() {
Syntax errors occur when the code violates the grammatical rules of the programming language.
These errors are detected during compilation.
Example:
#include <stdio.h>
int main() {
int num = 10
printf("Value of num: %d\n", num); // Missing semicolon
return 0;
}
Error: Missing semicolon (;).
Logical Errors:
Logical errors occur when the program runs without syntax errors but produces incorrect results.
These errors are harder to detect because they do not cause compilation issues.
Example:
#include <stdio.h>
int main() {
int a = 5, b = 10;
int sum = a - b; // Incorrect operation for sum
printf("Sum: %d\n", sum); // Will print a difference instead of sum
return 0;
}
Error: Incorrect operation (- instead of +).
Object code is the intermediate machine code generated by the compiler after translating the source
code. It is not executable by itself and is usually in a file with an .obj or .o extension.
Executable Code:
Executable code is the final output of the compilation process. It is a binary file that the operating
system can run directly. It contains machine code instructions and is usually in a file with an .exe
extension (on Windows) or no extension (on Unix/Linux).
Maharishi University of Information Technology
Example of Compilation Process:
Summary:
• Programming involves creating algorithms and coding them in a programming language.
• Problem Solving includes understanding the problem, planning, coding, testing, and
refining.
• Computer System Components include disk storage, memory, processor, operating system,
and tools like compilers, loaders, and linkers, each playing a critical role in the functioning
and management of a computer system.