Introduction to Programming Concept

By Notes Vandar

Introduction to Programming Concept

1.1 Introduction of Programming Language

C is a general-purpose high level language that was originally developed by Dennis Ritchie for the
UNIX operating system. It was first implemented on the Digital Equipment Corporation PDP-11
computer in 1972.
The UNIX operating system and virtually all UNIX applications are written in the C language. C has
now become a widely used professional language for various reasons.

  •  Easy to learn
  • Structured language
  • It produces efficient programs.
  • It can handle low-level activities.
  • It can be compiled on a variety of computers.

Facts about C

  • C was invented to write an operating system called UNIX.
  • C is a successor of B language which was introduced around 1970
  •  The language was formalized in 1988 by the American National Standard Institute (ANSI).
  •  By 1973 UNIX OS was almost totally written in C.
  •  Today C is the most widely used System Programming Language.
  •  Most of the state of the art software have been implemented using C

Why to use C?

C was initially used for system development work, in particular the programs that make-up the
operating system. C was adopted as a system development language because it produces code that runs
nearly as fast as code written in assembly language. Some examples of the use of C might be:

  • Operating Systems
  • Language Compilers
  • Assemblers
  • Text Editors
  •  Print Spoolers
  • Network Drivers
  • Modern Programs
  •  Data Bases Language
  • Interpreters
  • Utilities

C Compilers

When you write any program in C language then to run that program you need to compile that program
using a C Compiler which converts your program into a language understandable by a computer. This is
called machine language (i.e. binary format). So before proceeding, make sure you have C Compiler
available at your computer. Some examples of C compilers are Turbo C and Borland C.

C – Program Structure

A C program basically has the following form:

  • Preprocessor Commands
  • Functions
  • Variables
  • Statements & Expressions
  • Comments

Preprocessor Commands: This command tells the compiler to do preprocessing before doing actual compilation. Like;
#include <stdio.h> is a preprocessor command which tells a C compiler to include stdio.h file before going to actual compilation. You will learn more about C Preprocessors in C Preprocessors session.

Functions: are main building blocks of any C Program. Every C Program will have one or more functions and there is one mandatory function which is called main () function. This function is prefixed with keyword int which means this function returns an integer value when it exits. This integer value returned using return statement. The C Programming language provides a set of built-in functions. print () is a C built-in function which is used to print anything on the screen.

Variables: Variables are used to hold numbers, strings and complex data for manipulation.

Statements & Expressions: Expressions combine variables and constants to create new values. Statements are expressions, assignments, function calls, or control flow statements which make up C programs.

Comments: are used to give additional useful information inside a C Program. All the comments will be put inside /*…*/ as given in the example above. A comment can span through multiple lines.

1.3 Syntax and Semantics

Syntax:

In a programming language, Syntax defines the rules that govern the structure and arrangement of keywords, symbols, and other elements. Syntax doesn’t have any relationship with the meaning of the statement; it is only associated with the grammar and structure of the programming language.

A line of code is syntactically valid and correct if it follows all the rules of syntax. Syntax does not have to do anything with the meaning of the statement. Syntax errors are encountered after the program has been executed.

Some examples of syntax errors include missing semicolons in C++, undeclared variables in Java, although such errors are easy to catch.

Semantics:

Semantics refers to the meaning of the associated line of code and how they are executed in a programming language. Hence, semantics helps interpret what function the line of code/program is performing.

Semantic errors are encountered and handled during runtime of the program execution. If there is any semantic error and even when the statement has corrected syntax, it wouldn’t perform the function that was intended for it to do. Thus, such errors are difficult to catch.

 

1.4 Programming Design Tools

     A programming design tool in C refers to software or techniques that assist in the design, development, testing, and debugging of C programs. These tools help developers structure their programs effectively, enhance productivity, ensure code quality, and optimize performance.
Characteristics of an Algorithm:
  • Definiteness: Each step of the algorithm must be clear and unambiguous.
  • Input: The algorithm must have one or more inputs, which are provided to initiate the process.
  • Output: It should produce at least one output, which is the solution to the problem.
  • Finiteness: The algorithm must terminate after a finite number of steps.
  • Effectiveness: Each step should be basic enough to be carried out, ideally by a human or a machine.

Example of a Simple Algorithm (Finding the Maximum Number):

Let’s consider an algorithm to find the maximum number in an array of integers.

  1. Step 1 (Input): Start with an array of numbers, say A[].
  2. Step 2 (Initialization): Let max be the first element of the array, i.e., max = A[0].
  3. Step 3 (Loop): Go through each element in the array starting from the second element:
    • If A[i] > max, update max = A[i].
  4. Step 4 (Output): After checking all elements, the value of max will be the largest number in the array.
  5. Step 5 (End): Return the value of max.

Types of Algorithms:

  • Sorting Algorithms: Organize data in a particular order.
    • Examples: Bubble Sort, Quick Sort, Merge Sort.
  • Searching Algorithms: Find the position of an element in a data structure.
    • Examples: Binary Search, Linear Search.
  • Greedy Algorithms: Make a series of choices that seem best at the moment to find a global optimum.
    • Examples: Dijkstra’s Algorithm, Kruskal’s Algorithm.
  • Dynamic Programming: Break problems into subproblems, solve each subproblem once, and store its solution for future use.
    • Examples: Fibonacci Sequence, Knapsack Problem.
  • Divide and Conquer: Break a problem into smaller subproblems, solve each one, and combine their results.
    • Examples: Merge Sort, Quick Sort, Binary Search.

 

Importance of Algorithms:

  • Efficiency: Well-designed algorithms improve the efficiency of programs, particularly in terms of time and space complexity.
  • Problem Solving: They provide a structured approach to problem-solving, enabling the design of effective solutions.
  • Reusability: Algorithms are often abstract, meaning they can be applied to various problems with little or no modification.

 

1.4.2 Flow chart

A flowchart is a graphical representation of a process or an algorithm, typically using different shapes to represent various steps and arrows to indicate the flow of control between steps. Flowcharts are used to visualize the sequence of actions or decisions needed to solve a problem or complete a process.

Common Flowchart Symbols:

  • Oval (Terminator): Represents the start or end of the process.
    • Example: “Start” or “End”.
  • Rectangle (Process): Represents a process, action, or operation.
    • Example: “Add two numbers”, “Calculate area”.
  • Diamond (Decision): Represents a decision or branching point. The outcome can be “Yes/No” or “True/False”.
    • Example: “Is x > 10?”
  • Parallelogram (Input/Output): Represents input or output operations, such as reading or displaying data.
    • Example: “Input value of x”, “Display result”.
  • Arrow: Represents the flow of control or the direction of process steps.

Example Flowchart: Find the Maximum of Two Numbers

Here’s a flowchart for finding the larger of two numbers, A and B:

  1. Start: Begin the process.
  2. Input: Enter values for A and B.
  3. Decision: Check if A > B.
    • If true: Move to the “Output A is the maximum” step.
    • If false: Move to the “Output B is the maximum” step.
  4. End: The process is complete.

Flowchart Diagram:

 

Flowchart Explanation:

  • Start: The process begins.
  • Input number n: The user inputs a number.
  • Decision (Is n % 2 == 0?): The program checks if the number n is divisible by 2 without a remainder (i.e., whether n is even).
    • If true (Yes), the program prints “The number is even.”
    • If false (No), the program prints “The number is odd.”
  • End: The process terminates.

Why Use a Flowchart in C Programming?

  1. Visualize the logic: Flowcharts help in understanding the flow of a program before actually writing code.
  2. Identify logical errors: They make it easier to spot errors or inefficiencies in the design.
  3. Documentation: Flowcharts can act as a visual guide, making it easier for others to understand your program.
  4. Communication: When collaborating with team members, flowcharts make explaining algorithms and processes simpler.

Flowcharts are a great starting point for problem-solving and algorithm design in C programming. They lay a solid foundation for writing clear, error-free code.

 

1.4.3 Pseudo codes

            Pseudocode is an informal, high-level description of a program’s logic or algorithm that uses plain language and basic programming structures. It helps in designing algorithms before actually writing code in a specific programming language, such as C. Pseudocode is language-agnostic, meaning it can be understood without knowing any specific programming language.

Characteristics of Pseudocode:

  1. Simplicity: Written in plain language with a focus on the logic, not syntax.
  2. Structure: Uses programming-like structures, such as if, else, while, for, etc.
  3. No Specific Syntax: No strict syntax rules, allowing flexibility to focus on the steps and logic.
  4. Readability: Easy to understand for both programmers and non-programmers

 

Example of Pseudocode: Finding the Maximum of Two Numbers

Algorithm Find Max
Input: Two numbers A and B
Output: The maximum number

Step 1: Start
Step 2: Read A and B
Step 3: If A > B
Print “A is the maximum”
Else
Print “B is the maximum”
Step 4: End

 

1.5 Features of good programme

A good program possesses several key features that make it efficient, maintainable, and user-friendly. These features ensure that the program performs well, is easy to understand, and can be adapted or extended over time. Below are the important features of a good program:

  1. Correctness
  2. Efficiency (Time & Space)
  3. Readability
  4. Maintainability
  5. Modularity
  6. Reusability
  7. Robustness
  8. Scalability
  9. Portability
  10. Usability
  11. Security
  12. Testability
  13. Extensibility
  14. Minimal Complexity
  15. Documentation
  16. Consistency

1. Correctness

  • The program must produce the correct results as per the specifications and requirements. It should handle all inputs correctly and deliver the expected outputs without errors.

2. Efficiency

  • Time Efficiency (Speed): The program should execute quickly and efficiently. It should minimize the time taken to complete tasks, especially when processing large datasets.
  • Space Efficiency (Memory): The program should use memory wisely, avoiding unnecessary consumption of resources like RAM and disk space.

3. Readability

  • The program should be easy to read and understand by others (including the programmer themselves). This involves using clear, meaningful variable names, proper indentation, and writing comments where necessary.
  • Readability improves collaboration and maintenance, allowing other developers to modify or debug the program easily.

4. Maintainability

  • The program should be easy to modify and update. A well-structured and modular program allows for easier fixing of bugs, adding new features, and improving functionality without affecting the entire codebase.

5. Modularity

  • The program should be divided into smaller, self-contained modules or functions. Each module should perform a specific task, making the program easier to debug, maintain, and understand.
  • Advantages of modularity: Easier testing of individual components and reuse of code across different programs.

6. Reusability

  • Good programs are written in such a way that certain parts or functions can be reused in other programs. Writing reusable code saves development time and ensures consistency across different projects.

7. Robustness

  • A good program should be able to handle unexpected situations or inputs gracefully, without crashing. This involves error checking and handling, ensuring the program can deal with incorrect inputs, missing files, or invalid user actions.

8. Scalability

  • The program should be designed to handle increased loads, such as more data or users, without a significant drop in performance. Scalability ensures that the program remains efficient and usable even as its scope or requirements grow.

9. Portability

  • A good program should be portable, meaning it can be executed on different platforms or systems (e.g., Windows, Linux, macOS) with minimal or no modifications. This is typically achieved by adhering to standards and avoiding system-specific features.

10. Usability

  • The program should be user-friendly and intuitive. This involves providing a simple and clear interface, meaningful error messages, and guidance to the user, ensuring that the program is easy to operate and understand.

11. Security

  • A good program must be secure, protecting user data and preventing unauthorized access or manipulation. This includes implementing encryption, authentication, and safe handling of user inputs (e.g., avoiding buffer overflows, SQL injections).

12. Testability

  • The program should be easy to test. It should include mechanisms that allow developers to test individual components (unit testing) or the entire program (integration testing) to ensure it functions correctly.
  • Automated tests can further ensure that changes or updates do not break existing functionality.

13. Extensibility

  • The program should be designed in a way that it can be extended with new features or functionality without requiring significant changes to the existing codebase. Extensible programs are adaptable and can evolve with changing requirements.

14. Minimal Complexity

  • The program should be as simple as possible, avoiding unnecessary complexity. Clear and concise code ensures better readability, lower error rates, and easier maintenance.
  • Adhering to the KISS principle (“Keep It Simple, Stupid”) helps in reducing the program’s complexity.

15. Documentation

  • Proper documentation is an essential feature of a good program. It includes:
    • Comments within the code to explain complex sections.
    • External documentation that explains the purpose of the program, how to install/use it, and how it works.
    • Documentation ensures that future developers or users can easily understand and work with the program.

16. Consistency

  • The program should follow consistent naming conventions, indentation, and coding styles throughout. Consistency helps in improving readability and makes the code easier to maintain.

 

Next Chapter
Important Questions
Comments
Discussion
0 Comments
  Loading . . .