
Introduction to Computer Programming – I Year I Part
Introduction to Computer Programming is a foundational course for first-year engineering students. It introduces the essential concepts of problem-solving using computers, starting from the basic definition of a program to the entire development lifecycle. This subject builds the logical and structural thinking required for all future programming and software development courses.
Chapter Information
Chapter 1: Introduction to Computer Programming (3 hours) – 5 marks
Course: Computer Programming, I Year I Part
Description: Comprehensive study notes covering the introduction to Computer Programming, including algorithms, flowcharts, languages, and the compilation process.
Credit: Sujan karki
Table of Contents
Detailed Chapter Notes
1.1 Definition of a Program and Programming Language
What is a Program?
A program is a set of instructions or commands arranged in a specific sequence to guide a computer to find a solution for a given problem. Think of it like a recipe: it contains a list of ingredients (variables) and a list of directions (statements) that tell the computer what to do with those variables. Programs are the fundamental units of software; without them, computers are useless.
What is a Programming Language?
A programming language is a set of rules (syntax) and symbols that provides a way of instructing a computer to perform certain operations. It acts as a bridge between human logic and the machine’s binary understanding. Examples include C, C++, Java, and Python.
1.2 Types and Generations of Programming Languages
Types of Programming Languages
Programming languages are broadly categorized into two main types:
- Low-Level Language: These are much closer to the computer’s hardware and require deep knowledge of its architecture.
- Machine Language (1GL): The lowest level, consisting of binary code (0s and 1s) that the CPU can execute directly.
- Assembly Language (2GL): Uses mnemonic codes (e.g.,
ADD
,SUB
,MOV
) to represent machine instructions, making it slightly more human-readable.
- High-Level Language (3GL, 4GL, 5GL): These languages are closer to human language, using English-like words and syntax. They are easier to read, write, and maintain, and are generally machine-independent. Examples include C, Java, Python, and SQL.
Generations of Programming Languages
- First Generation (1GL): Machine Language (binary code).
- Second Generation (2GL): Assembly Language (mnemonics).
- Third Generation (3GL): Procedure-Oriented (C, Pascal) and Object-Oriented (C++, Java) languages.
- Fourth Generation (4GL): Problem-Oriented languages designed for specific applications, like SQL for databases.
- Fifth Generation (5GL): Languages used in AI that can process natural language, such as Prolog and Mercury.
1.3 Problem-Solving using a Computer
Problem-solving is a systematic approach to finding and implementing a solution. The main steps in the program development life cycle are:
1.3.1 Problem Analysis
This is the first and most critical step. It involves thoroughly understanding the problem, identifying constraints, determining the required inputs, and defining the desired outputs. The problem is often decomposed into smaller, more manageable sub-problems.
1.3.2 Algorithm and Flowchart
An algorithm is a finite, step-by-step, unambiguous procedure for solving a problem. A good algorithm must have well-defined inputs and outputs and be effective. A flowchart is a diagrammatic representation of an algorithm, which helps in visualizing the program logic and flow using standard symbols.
1.3.3 Programming (Coding)
This phase involves translating the algorithm or flowchart into source code using a chosen programming language. The programmer writes the instructions following the language’s syntax.
1.3.4 Compilation, Linking and Execution
This is the process of converting your source code into a runnable program:
- Preprocessing: The source code is processed to handle directives like
#include
and expand macros. - Compilation: The compiler translates the processed code into assembly code.
- Assembling: The assembler converts the assembly code into machine-readable object code.
- Linking: The linker combines the object code with necessary library files to create the final executable program.
1.3.5 Debugging and Testing
Debugging is the process of finding and fixing errors (bugs). Testing is the process of verifying that the program works correctly for various inputs. Errors can be of two main types:
- Syntax Errors: Violations of the programming language’s rules (e.g., a typo, a missing semicolon). Caught by the compiler.
- Logical Errors (Semantic Errors): The program runs but produces an incorrect result due to flawed logic. These are often harder to find.
1.3.6 Documentation
Documentation involves creating descriptive text, both within the code (as comments) and in separate manuals. It explains the program’s purpose, functionality, and how to use it, which is crucial for maintenance and for other developers.
PDF Notes by Sujan karki
Solved Assignments
1. List and Explain program development (problem solving) and compilation process in details. Draw a flowchart to find all the possible roots of a quadratic equation.
Program Development (Problem Solving) Process:
The program development life cycle is a systematic process that programmers follow to create high-quality software. The key steps are:
- Problem Analysis: Understand the problem, inputs, and desired outputs.
- Algorithm and Flowchart Design: Create a logical step-by-step plan.
- Coding: Write the program in a programming language.
- Compilation and Execution: Translate the code into a machine-readable format.
- Debugging and Testing: Find and fix errors, and verify the program works correctly.
- Documentation: Write comments and manuals for future reference.
Compilation Process:
This is the process of converting source code into an executable file. It involves the Preprocessor, Compiler, Assembler, and Linker, as detailed in section 1.3.4 above.
Flowchart for Roots of a Quadratic Equation (ax² + bx + c = 0):
[ Start ] ↓ [ Read a, b, c ] ↓ [ D = b*b - 4*a*c ] ↓ < Is D < 0 ? > ├─(Yes)→ [ Print "Imaginary Roots" ] → [ Stop ] ↓ (No) < Is D == 0 ? > ├─(Yes)→ [ r = -b / (2*a) ] → [ Print "Real & Equal Root:", r ] → [ Stop ] ↓ (No) [ r1 = (-b+√D)/(2*a) ] [ r2 = (-b-√D)/(2*a) ] ↓ [ Print "Real & Distinct Roots:", r1, r2 ] ↓ [ Stop ]
2. Describe the recent software trends. Explain in details about the features that a software/program should include.
Recent Software Trends:
- Artificial Intelligence (AI) and Machine Learning (ML): Integration of AI/ML for automation, prediction, and data analysis in almost every application.
- Cloud Computing: Shift from on-premise servers to cloud platforms (AWS, Azure, GCP) for scalability, flexibility, and cost-efficiency.
- Cybersecurity: Increased focus on protecting data and systems from evolving cyber threats.
- Low-Code/No-Code Platforms: Tools that allow users to create applications with minimal programming knowledge.
Essential Features of a Good Software/Program:
- Correctness & Reliability: The program should produce the correct output for all valid inputs and perform consistently.
- Efficiency: It should use system resources (CPU, memory) optimally.
- Usability: The software should be easy to learn and use for its target audience.
- Maintainability & Portability: The code should be well-structured and documented, making it easy to debug, update, and move to other systems.
3. List out the general rules for flowcharting. What are the errors that might occur during debugging? Explain.
General Rules for Flowcharting:
- A flowchart should have only one Start and one Stop symbol.
- The flow of logic is typically from top to bottom and left to right.
- Flow lines should not cross each other; use connectors if necessary.
- Decision symbols must have one entry point and at least two exit points (e.g., Yes/No).
- All symbols must be connected with flow lines (arrows).
Errors During Programming:
- Syntax Errors: These are grammatical errors in the code that violate the rules of the programming language. For example, missing a semicolon at the end of a statement in C. The compiler catches these errors and prevents the program from being built.
- Logical (Semantic) Errors: These are errors in the program’s logic. The code is syntactically correct and compiles, but it does not produce the intended result. For example, using `+` instead of `-` in a calculation. These are the hardest errors to find.
4. Explain compilation process with suitable diagram. Why we need to analyze the problem before solving it.
Compilation Process:
The compilation process transforms human-readable source code into a machine-executable program. The key stages are:
Source Code (*.c) ↓ (Preprocessor) Expanded Source Code (*.i) ↓ (Compiler) Assembly Code (*.s) ↓ (Assembler) Object Code (*.o / *.obj) + Library Files ↓ (Linker) Executable File (*.exe / a.out)
Why Problem Analysis is Crucial:
Problem analysis is vital because without a complete understanding of the problem, it’s impossible to create an effective and correct solution. It helps to clarify requirements, identify constraints, and define the scope of the project. Skipping this phase often leads to writing incorrect programs, wasting time on rework, and creating software that doesn’t meet the user’s needs.
5. What is program? Explain different types of programming language in brief.
Program: A program is a set of sequential instructions that a computer follows to perform a task. It’s like a recipe that tells the computer exactly what to do, step by step, to achieve a specific outcome.
Types of Programming Languages:
- Low-Level Languages: These are close to the computer’s hardware. They include Machine Language (binary code) and Assembly Language (using mnemonics). They are fast but difficult for humans to work with.
- High-Level Languages: These use English-like syntax and are easier for humans to read and write (e.g., C, Python, Java). They are machine-independent but must be translated into machine code by a compiler or interpreter before they can be run.
6. What is algorithm? Explain how does algorithm and flowchart helps in computer programming.
Algorithm: An algorithm is a finite, well-defined, step-by-step procedure for solving a specific problem or accomplishing a specific task.
How They Help in Programming:
- Clear Logic: They force the programmer to think through the logic of the problem before writing any code, which helps prevent logical errors.
- Communication: They serve as a blueprint that can be easily understood by other programmers, making collaboration easier.
- Debugging: When a program fails, the algorithm and flowchart can be reviewed to find the flaw in the logic.
- Efficiency: They provide a language-independent way to design and analyze the solution before committing to a specific programming language.
7. What are different types of computer software? What do you mean by high level language and low-level language?
Types of Computer Software:
- System Software: Manages the computer hardware and provides a platform for applications to run. Examples include Operating Systems (Windows, Linux), device drivers, and utility programs.
- Application Software: Designed to perform specific tasks for the user. Examples include web browsers, word processors, games, and database applications.
High-Level and Low-Level Languages:
A low-level language (like Assembly) is very close to the computer’s native machine code, offering fine-grained control but being hard to write. A high-level language (like C or Python) is more abstract and human-readable, making programming easier and faster, but it requires translation to machine code.
8. What are preprocessor directives? Explain with example.
A preprocessor directive is an instruction for the compiler’s preprocessor. In C, these lines start with a hash symbol (#
). They are processed before the actual compilation of the program begins.
Example: The most common directive is #include
.
#include <stdio.h>
This directive tells the preprocessor to find the file stdio.h
(Standard Input/Output header) and copy its entire content into the current source file. This gives the program access to standard functions like printf()
and scanf()
.
9. What is computer programming and computer software? Explain types of programming language. Define compiler.
Computer Programming & Software: Computer programming is the act of writing instructions (programs) for a computer to execute. Computer software is a collection of these programs and related data that provide instructions for telling a computer what to do and how to do it.
Types of Programming Languages: The main types are low-level languages (Machine, Assembly) and high-level languages (C, Python, Java).
Compiler: A compiler is a special program that translates the entire source code written in a high-level programming language into a lower-level language (like assembly or machine code) all at once, creating an executable file.
10. Explain different generation(evolution) of programming language.
The evolution of programming languages is categorized into five generations:
- First Generation (1GL) – Machine Language: Consists of binary code (0s and 1s) that the computer’s CPU can directly execute. It is machine-dependent and extremely difficult for humans to work with.
- Second Generation (2GL) – Assembly Language: Uses mnemonic codes (e.g., ADD, MOV) to represent machine instructions, making it slightly easier than 1GL. An assembler is required to translate it into machine code.
- Third Generation (3GL) – High-Level Languages: Uses English-like syntax, making them much easier to read, write, and maintain. They are largely machine-independent. Examples include C, C++, Java, and Pascal.
- Fourth Generation (4GL) – Problem-Oriented Languages: Designed for specific purposes, allowing users to specify what they want without detailing how to do it. SQL (for databases) is a prime example.
- Fifth Generation (5GL) – AI Languages: Used in artificial intelligence research. These languages are designed to make the computer solve a problem without the programmer. Examples include Prolog and Mercury.
11. Explain compilation linking and loading process with example. How do you debug a c-program?
Compilation, Linking, and Loading:
- Compilation: A C source file (e.g., `my_program.c`) is translated by the compiler into an object file (`my_program.o`) containing machine code.
- Linking: The linker combines this object file with other necessary object files (e.g., from libraries like the standard C library) to resolve references to functions like `printf`. This produces a single executable file (e.g., `my_program.exe`).
- Loading: When you run the program, the operating system’s loader copies the executable file from the hard disk into the main memory (RAM) so the CPU can begin executing it.
How to Debug a C-Program:
- Print Statements: The simplest method is to insert
printf()
statements at various points in the code to print the values of variables and track the program’s flow. - Using a Debugger: A debugger (like GDB) is a tool that allows you to run your program step-by-step, set breakpoints to pause execution at specific lines, and inspect the values of variables at any point.
- Code Review: Manually reading through the code, or having someone else review it, to spot logical errors.
12. Define algorithm and flowchart. Use the various commonly used symbols used in flowcharts. How does flowchart help in programming?
Algorithm & Flowchart Definition: An algorithm is a step-by-step textual plan for solving a problem, while a flowchart is a graphical representation of that plan.
Commonly Used Flowchart Symbols:
- Oval (Terminator): Represents the Start and Stop points.
- Parallelogram (Input/Output): Represents reading data (input) or displaying results (output).
- Rectangle (Process): Represents a calculation or an operation.
- Diamond (Decision): Represents a question or a branch in the process (e.g., an if-else statement).
- Arrow (Flow Line): Shows the direction of flow and connects the symbols.
How Flowcharts Help: They provide a clear visual representation of the program’s logic, making it easier to design, understand, and debug before and during the coding phase.
13. Explain structure of C-program with example. How compiler differ from assembler and interpreter?
Structure of a C-Program:
// 1. Documentation Section (Comments) /* This is a simple C program */ // 2. Link Section (Preprocessor Directives) #include <stdio.h> // 3. Global Declaration Section (Optional) int global_var = 10; // 4. Main Function - The entry point of the program int main() { // Declaration Part - where variables are declared int local_var; // Executable Part - where instructions are performed local_var = 5; printf("Hello, World! %d\n", local_var); return 0; // Return statement } // 5. Sub-program Section (User-defined functions - Optional)
Compiler vs. Assembler vs. Interpreter:
- Compiler: Translates the entire high-level program into machine code at once. The output is a standalone executable file.
- Interpreter: Translates and executes the high-level program line by line. It does not produce a separate executable file.
- Assembler: Translates a program written in assembly language (a low-level language) into machine code.
14. Draw a flowchart to calculate the area of a given triangle using Heron’s formula.
[ Start ] ↓ [ Read sides a, b, c ] ↓ [ s = (a + b + c) / 2 ] ↓ [ Area = √[s(s-a)(s-b)(s-c)] ] ↓ [ Print Area ] ↓ [ Stop ]
15. Write an algorithm and flowchart to find the greatest number among four numbers.
Algorithm:
- Start.
- Declare variables a, b, c, d, max.
- Read the four numbers a, b, c, d.
- Assume the first number is the largest: `max = a`.
- If `b > max`, then `max = b`.
- If `c > max`, then `max = c`.
- If `d > max`, then `max = d`.
- Display `max`.
- Stop.
Flowchart:
[ Start ] ↓ [ Read a, b, c, d ] ↓ [ max = a ] ↓ < Is b > max ? > ├─(Yes)→ [ max = b ] ↓ (No) < Is c > max ? > ├─(Yes)→ [ max = c ] ↓ (No) < Is d > max ? > ├─(Yes)→ [ max = d ] ↓ (No) [ Print max ] ↓ [ Stop ]
16. Write an algorithm and flowchart factorial of a given number.
Algorithm for Factorial:
- Start.
- Declare variables n (input), fact (result), i (counter).
- Initialize `fact = 1` and `i = 1`.
- Read the value of `n`.
- Check if `i <= n`. If yes, continue. If no, go to step 8.
- Calculate `fact = fact * i`.
- Increment `i` by 1 (`i = i + 1`). Go back to step 5.
- Display the value of `fact`.
- Stop.
Flowchart:
[ Start ] ↓ [ Read n ] ↓ [ fact = 1, i = 1 ] ↓ ┌─< Is i <= n ? >─(Yes)→ [ fact = fact * i ] → [ i = i + 1 ]─┐ │ ↓ (No) │ │ [ Print fact ] │ │ ↓ │ └─→ [ Stop ] <──────────────────────────────────────────────┘
Disclaimer
The educational materials provided on this website are intended as supplementary resources to support your learning journey. These study materials are sample documents designed to help students understand complex concepts in Computer Programming.
We have made every effort to ensure the accuracy of the content. However, we recommend students to refer to standard textbooks and consult with professors for authoritative explanations. These materials should be used as references only.