Maharishi University of Information Technology
Name of the faculty: Mr. Abhay Bajpai
Course : BCA
Year/Semester: 1/1
Subject code: BCA 101
Session: 2024-25
Subject Name: Computer Fundamental & C Programming
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.
Key Concepts in Programming:
• Algorithm: A step-by-step procedure or formula for solving a problem. Algorithms are
fundamental to programming as they define the logic and sequence of operations.
• 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.
2. Devise a Plan: Develop an algorithm or a step-by-step approach to solve the problem.
3. Write the Code: Implement the algorithm in a programming language.
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:
Problem: Calculate the sum of integers from 1 to 10.
1. Understand: We need the sum of numbers from 1 to 10.
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.
5. Refine: The code is already efficient for this problem.
2. Introduction to Components of Computer System:
2.1 Disk:
• 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:
• Volatile Memory: Requires power to maintain stored information (e.g., RAM).
• Non-Volatile Memory: Retains information even when the power is turned off (e.g., ROM,
SSD).
2.3 Processor (CPU - Central Processing Unit):
• Definition: The brain of the computer that performs instructions from programs through
basic arithmetic, logic, control, and input/output operations.
• Components:
o ALU (Arithmetic Logic Unit): Performs arithmetic and logical operations.
o Control Unit: Directs operations of the processor.
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.
2.4 Operating System (OS):
• Definition: System software that manages hardware resources and provides common
services for computer programs.
• Functions:
o Process Management: Manages running processes and their scheduling.
o Memory Management: Handles allocation and deallocation of memory space.
o File System Management: Manages files and directories on storage devices.
o Device Management: Controls peripheral devices such as printers, keyboards, and
mice.
Examples: Windows, macOS, Linux, Android.
Maharishi University of Information Technology
2.5 Compilers:
• Definition: A compiler translates code written in a high-level programming language into
machine language (binary code) that the CPU can execute.
• Process:
1. Lexical Analysis: Breaking down the code into tokens.
2. Syntax Analysis: Parsing tokens to check for grammatical correctness.
3. Semantic Analysis: Ensuring the code follows language rules.
4. Optimization: Improving the code for performance.
5. Code Generation: Producing the final machine code.
Example: GCC (GNU Compiler Collection), Clang.
2.6 Loaders:
• Definition: A loader is responsible for loading executable files into memory and preparing
them for execution.
• Functions:
o Loading: Moves executable code into RAM.
o Relocation: Adjusts addresses in the code and data to reflect their locations in
memory.
o Linking: Combines multiple object files into a single executable.
Types:
• Static Loader: Loads programs at compile time.
• Dynamic Loader: Loads programs or libraries at runtime.
2.7 Linkers:
• Definition: A linker combines several object files generated by a compiler into a single
executable or library.
• Functions:
o Symbol Resolution: Connects function calls to their definitions.
o Address Binding: Assigns memory addresses to various parts of the program.
o Library Linking: Links to libraries and their functions.
Maharishi University of Information Technology
Types:
• Static Linking: Libraries are included in the executable at compile time.
• 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:
• Input: Receives zero or more inputs.
• Output: Produces at least one output.
• Definiteness: Each step is precisely defined.
• Finiteness: The algorithm must terminate after a finite number of steps.
• Effectiveness: Each step should be basic enough to be performed exactly and in a finite
amount of time.
Example Algorithm:
To find the largest of three numbers:
1. Input three numbers: a, b, and c.
2. Compare a with b and c:
o If a is greater than both, then a is the largest.
o Otherwise, compare b with c:
▪ If b is greater than c, then b is the largest.
▪ Otherwise, c is the largest.
3. Output the largest number.
4. Steps to Solve Logical & Numerical Problems:
To solve logical and numerical problems, follow these steps:
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.
4. Write Pseudocode or Flowchart: Outline the algorithm in pseudocode or draw a flowchart.
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.
7. Debug: Identify and fix any errors in the program.
Example:
To solve a problem of calculating the factorial of a number:
1. Understand: Compute n! = n * (n-1) * (n-2) * ... * 1.
2. Analyze: Determine the input (number n) and output (factorial of n).
3. Algorithm:
o Input n.
o Initialize factorial to 1.
o For each number from 1 to n, multiply factorial by that number.
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:
A flowchart is a graphical representation of an algorithm. It uses different shapes to denote different
types of operations, such as processes, decisions, and inputs/outputs.
Common Flowchart Symbols:
• Oval: Start/End
• Rectangle: Process (e.g., calculation or assignment)
• Diamond: Decision (e.g., if-else condition)
• Parallelogram: Input/Output
• Arrow: Flow of control
Example Flowchart for Finding the Largest of Two Numbers:
Maharishi University of Information Technology
Pseudocode:
Pseudocode is a high-level description of an algorithm using plain language or a mixture of
programming constructs. It is used to design algorithms before coding.
Example Pseudocode:
START
INPUT a, b
IF a > b THEN
OUTPUT a is the largest
ELSE
OUTPUT b is the largest
END
6. From Algorithms/Flowcharts to Programs:
To convert algorithms or flowcharts into source code:
1. Understand the Algorithm/Flowchart: Know the logical steps.
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;
}
7. Source Code, Variables, and Memory Locations:
Source Code:
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:
int num = 5; // Variable 'num' of type int
float pi = 3.14; // Variable 'pi' of type float
char letter = 'A'; // Variable 'letter' of type char
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() {
int num = 10;
printf("Value of num: %d\n", num);
printf("Memory address of num: %p\n", (void*)&num);
Maharishi University of Information Technology
return 0;
8. Syntax & Logical Errors in Compilation:
Syntax Errors:
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 +).
9. Object & Executable Code:
Object Code:
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:
1. Source Code: Written in a high-level language (e.g., C code).
2. Compilation: Translates source code to object code.
3. Linking: Combines object code with libraries to produce executable code.
4. Executable File: Ready to be run by the computer.
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.
• Algorithms: Step-by-step procedures for solving problems.
• Flowcharts/Pseudocode: Tools for designing algorithms.