CS-101-T Unit 1 – Problem Solving Using Computers
Problem solving is the process of analyzing a problem and then implementing a solution
using a computer.
Program:
Program is the set of instructions which is run by the computer to perform specific task. The
task of developing program is called programming.
Steps of Problem-Solving Using Computers
General Steps in Problem Solving
1. Understand the Problem – What is required? What is the input/output?
Analyze the Problem – Break it into smaller parts, identify constraints(conditions).
2. Develop Algorithm – Step-by-step instructions to complete a task.
3. Represent the Algorithm – Using Flowcharts or Pseudocode.
4. Coding – Writing program in a language like C.
5. Compilation and execution -
6. Testing & Debugging – Checking and correcting errors.
7. Documentation & Maintenance – Keeping records, improving in future
Algorithm:
Algorithm is the set of rules that define how particular problem can be solved in finite
number of steps. Any good algorithm must have following characteristics:
1. Input: Specify and require input
2. Output: Solution of any problem
3. Definite: Solution must be clearly defined
4. Effective : Each step should be effective in meaning.
5. Finite: Steps must be finite
6. Correct: Correct output must be generated
Advantages of Algorithms:
1. It is the way to solve a problem step-wise so it is easy to understand.
2. It uses definite procedure.
3. It is not dependent with any programming language.
4. Each step has its own meaning so it is easy to debug.
Disadvantage of Algorithms:
1. It is time consuming.
2. Difficult to show branching and looping statement.
3. Large problems are difficult to implement.
Algorithm: Addition of Two Numbers
Step 1: Start
Step 2: Input first number → A
Step 3: Input second number → B
Step 4: Add the two numbers → SUM = A + B
Step 5: Display the result → SUM
Step 6: Stop
Flowchart
• Flowchart: Visual diagram (Input → Compare → Output).
Graphical representation of algorithm using standard symbols
Advantages of Flowchart:
1. Clarity:
Shows the process step-by-step with symbols, making the logic easy to understand.
2. Easy Communication:
Even non-technical people can understand the process because it is graphical.
3. Easy Debugging:
Helps in finding errors quickly since the process is visible.
4. Better Documentation:
Useful as a reference for future work.
5. Efficient Analysis:
Unnecessary steps can be identified and removed, improving efficiency.
6. Helps in Program Development:
Once the flowchart is ready, writing actual code becomes easier.
Disadvantage of Flowchart:
1. Time-Consuming:
Creating a flowchart for complex logic takes a lot of time.
2. Difficult to Modify:
If the logic changes, the entire diagram needs to be redrawn.
3. Not Suitable for Large Programs:
Large flowcharts become complicated and confusing.
4. Requires Knowledge of Symbols:
One must remember standard symbols to understand or create flowcharts.
5. Storage Problems:
Large flowcharts are difficult to store and manage (on paper or digitally).
Comparison Between Algorithm and Flowchart:
❖ Programming Languages as a tool
Why We Need Programming Languages
• Humans communicate with languages like English, Hindi, or Marathi.
• But a computer does not understand human languages.
• A computer only understands Machine Language (0s and 1s) called binary code.
Example:
Add 5 and 3
101101 + 000011 = 110000
Role of Programming Languages
• Programming languages (like C, Python, Java) act as a bridge.
• They allow us to write instructions in human-readable form.
• A translator (compiler/interpreter) then converts our code into machine code (binary)
that the computer can execute.
❖ Programming Paradigm:
Definition:
A programming paradigm is a style or way of programming.
It represents the approach or method used to solve problems and organize code using a
programming language.
Example: Just like in real life, one problem can be solved in different ways, programming
paradigms provide different ways to write programs.
Imperative Paradigm
• Focus: Tells the computer HOW to do things (step-by-step instructions).
• It is like giving a recipe to cook food.
• Examples: C, C++, Java
Sub-types:
1. Procedural Programming
o Divides the program into functions/procedures.
o Example: C language (main(), add(), etc.).
2. Object-Oriented Programming (OOP)
o Organizes programs into objects (data + methods).
o Example: Java, C++, Python (classes and objects).
3. Structured Programming
o Focus on clear control structures: loops, conditions, blocks.
o Example: C language with if, for, while.
Declarative Paradigm
• Focus: Tells the computer WHAT to do, not how to do it.
• It is like saying “I want a pizza” without explaining the recipe.
• Examples: SQL, Prolog
Sub-types:
1. Logic Programming
o Based on facts and rules.
o Example: Prolog.
2. Functional Programming
o Uses pure mathematical functions, avoids changing data.
o Example: Haskell, Lisp, Scala, Python (partly).
3. Data-Driven Programming
o Focuses on data and its flow instead of control flow.
o Example: SQL queries, data analysis tools.
Summary (Easy Way to Remember)
• Imperative = HOW to do → step-by-step instructions.
• Declarative = WHAT to do → just describe the result you want
❖ Types of Programming languages:
Programming languages are classified into different types based on how they interact with
machines and how we write programs.
1. Low-Level Languages
• Close to the hardware, difficult for humans to understand.
• Very fast execution but hard to write and debug.
(a) Machine Level Language (1GL)
• Written in binary (0s and 1s).
• Directly understood by the computer.
• Example: 10110011
(b) Assembly Language (2GL)
• Uses mnemonics (short codes) like MOV, ADD.
• Needs an assembler to convert to machine code.
• Example: MOV A, B
2. High-Level Languages
• Closer to human languages.
• Easy to write, read, and debug.
• Needs compiler/interpreter to convert into machine code.
(a) Procedural Oriented Language (3GL)
• Uses procedures/functions.
• Step-by-step instructions.
• Examples: C, Pascal, Fortran
(b) Object Oriented Language (4GL)
• Uses objects (data + methods).
• Focus on reusability and modularity.
• Examples: Java, C++, Python
(c) Natural Language (5GL)
• Uses human-like instructions (AI-based).
• User specifies what to do, system decides how to do it.
• Examples: AI languages, Prolog, LISP, SQL (partly)
Summary
• 1GL & 2GL → Low-level (hardware-oriented, fast, but hard to use).
• 3GL, 4GL & 5GL → High-level (human-oriented, easy, widely used).
❖ Compilation Process:
When a C program is written, it cannot be executed directly.
It must go through several steps to convert source code into an executable file.
Steps in Compilation
1. Source Code (hello.c)
• The program written by the programmer in C language.
Example:
#include <stdio.h>
int main() {
printf("Hello");
return 0;
}
2. Preprocessing
• Handled by the preprocessor.
• It processes all lines starting with #.
• Tasks:
o Includes header files (#include <stdio.h>)
o Expands macros (#define)
o Removes comments
Output: Modified source code
3. Compilation
• Done by the compiler.
• Converts pre-processed code into assembly code.
• Assembly is a low-level language, closer to machine code.
Output: Assembly Code (hello.s)
4. Assembly
• Done by the assembler.
• Converts assembly code into machine code (object file).
• Object file contains binary instructions but is incomplete.
Output: Object Code (hello.o)
5. Linking
• Done by the linker.
• Links object code with necessary libraries (e.g., printf() from stdio).
• Produces the final executable file.
Output: Executable ([Link] or [Link])
Final Result
• The executable file can now run directly on the computer.
• Output of program → Hello
❖ Syntax and Semantic Error
1. Syntax Errors
• Errors in the structure or grammar of the program.
• Occur when the rules of the programming language are not followed.
• Detected by the compiler at compile time.
• Program will not run until syntax errors are fixed.
Examples:
int main( {
printf("Hello") // Missing semicolon
}
Errors:
• Wrong bracket ( instead of {
• Missing ;
2. Semantic Errors
• Errors in the meaning or logic of the program.
• The syntax is correct, but the program does not do what the programmer intended.
• Not caught by the compiler, may run but give wrong output.
Examples:
int a = 5, b = 0;
int c = a / b; // Division by zero (wrong logic)
int x = 10;
int y = "Hello"; // Wrong meaning (string assigned to int)
Good Programming Practices (In Broad)
Good programming practices are techniques followed by programmers to make code
readable, reliable, efficient, and maintainable.
1. Meaningful Names
• Variables, functions, and classes should have descriptive names.
• Example: studentMarks is better than sm.
2. Indentation & Formatting
• Proper indentation makes code visually clear.
• Each block (loops, conditions, functions) should be neatly formatted.
3. Comments
• Use comments to explain important logic, algorithms, or complex steps.
• Avoid over-commenting obvious code.
4. Modular Programming
• Divide program into small, reusable modules/functions.
• Increases readability, reusability, and easier debugging.
5. Avoid Hardcoding
• Avoid directly putting values inside the code.
• Use constants, variables, or configuration files.
• Example:
const float PI = 3.14;
area = PI * r * r;
6. Error Handling
• Anticipate errors (invalid input, division by zero, file not found).
• Handle them properly with validation or exception handling.
7. Consistent Naming Conventions
• Follow a consistent style like camelCase (studentName) or snake_case
(student_name).
• Makes code uniform and easy to understand.
8. Code Reusability
• Reuse functions, libraries, and classes instead of rewriting code.
• Saves time and effort.
9. Testing & Debugging
• Test program with multiple inputs including edge cases.
• Debug systematically to find and fix errors.
10. Documentation
• Write proper documentation for large programs.
• Include algorithm, flowchart, and code explanation for future reference.
• Use meaningful names for variables and functions.
• Follow proper indentation and formatting.
• Write comments to explain logic.
• Use modular programming (functions/modules).
• Avoid hardcoding values, use constants/variables.
• Do proper error handling.
• Maintain consistent naming conventions.
• Ensure code reusability.
• Perform testing and debugging.
• Maintain documentation for future reference.