Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 11

1 | Computer Programming 1 1

UNIT I: Programming Fundamentals


1.0 Learning Outcomes
After completing this module, you will be able to:
a. Create programming algorithm, pseudocode, and flowchart;
b. Demonstrate understanding of procedural programming and C programming
language;

1.1 Introduction
In this section, we will explore the importance of understanding algorithms,
pseudocode, and flowcharts in the context of computer programming. Algorithms
are the heart of efficient problem-solving, while pseudocode and flowcharts act as
powerful tools to represent and visualize complex procedures, aiding in the
development of effective software solutions.

1.2 Topics/Discussion
This module covers the following topics:

1.2.1 The Programming Process


A programming process refers to the systematic approach taken by software
developers to create, design, and maintain computer programs. It involves several
stages that guide the development of software from conception to deployment and
beyond. While there are different methodologies and specific variations, the common
stages of a programming process typically include.
1. Requirements Gathering: Interact with stakeholders (clients, users, project managers) to understand
and document software requirements, defining program objectives and features.
2. Analysis: Carefully examine and analyze gathered requirements, ensuring feasibility and defining
potential solutions.
3. Design: Create a high-level design plan, including software architecture, data structures, algorithms,
and interfaces.
4. Implementation (Coding): Write code based on the design, following coding standards and the
defined requirements.
5. Testing: Identify and fix defects through unit, integration, and system testing techniques.
6. Deployment: Thoroughly test and deploy the software to the production environment or end-users.
7. Maintenance: Continuously update and adapt the software to fix bugs, add features, and address
changing requirements.
8. Documentation: Maintain comprehensive documentation, including design documents, user
manuals, code comments, etc.
9. Version Control: Use version control systems to track code changes, collaborate, and revert to
previous versions if necessary.
10. Project Management: Utilize effective project management techniques to plan, schedule, and monitor
progress to meet objectives.

The programming process in C is essentially the same as the general programming


process outlined in the previous response. However, in the context of C programming,
there are some specific steps and considerations that are worth highlighting. Let's go
through the programming process in C:
1. Problem Definition: Clearly define the task and requirements the C program needs to address.
2. Algorithm Design: Create a logical algorithm, breaking down the task into smaller steps.
1 | Computer Programming 1 2

3. Pseudocode or Flowchart: Use pseudocode or flowcharts to clarify the program's logic and structure.
4. Code Writing: Implement the algorithm in C, keeping the code organized and well-commented.
5. Compilation: Compile the C code into machine-readable format using a C compiler.
6. Testing & Debugging: Thoroughly test the program, fixing any errors or bugs.
7. Optimization: Improve code performance and efficiency if needed.
8. Documentation: Maintain comprehensive code comments for clarity and future modifications.
9. Version Control: Use Git or similar tools to manage and track code changes.
10. Deployment: Deploy the stable program for end-users or integration.
11. Maintenance: Regularly update and maintain the program for reliability.

1.2.2 Procedural Programming


Procedural programming is a programming paradigm where the focus is on writing
procedures (also known as functions or subroutines) that perform specific tasks and
manipulate data. It is a straightforward and linear approach to programming, where the
program's execution consists of calling procedures one after the other in a sequence to
solve a problem. Procedural programming in C is characterized by the following key
features:

1. Functions: Building blocks of the program, taking input, performing operations, and returning
results.
2. Global Variables: Declared outside functions, accessible from any part of the program. Excessive use
discouraged due to complexity and data integrity issues.
3. Sequential Execution: Follows step-by-step execution, managing flow through function calls and
return statements.
4. Limited Data Abstraction: Data and functions not tightly encapsulated, leading to potential
management challenges.
5. Code Reusability: Promotes code reusability by breaking functionality into smaller functions.
6. Top-Down Design: Divides problems into smaller sub-problems, solved by separate functions.

1.2.3 Algorithm
1 | Computer Programming 1 3

An algorithm is a step-by-step procedure or a set of instructions designed to solve a


specific problem or perform a specific task. In programming, algorithms are essential as
they serve as the fundamental building blocks for writing efficient and effective
software.
An algorithm can be represented in various ways, such as natural language,
pseudocode, flowcharts, or actual programming code. Regardless of the representation,
an algorithm must exhibit the following characteristics:

1. Well-Defined: The steps of the algorithm must be precisely and unambiguously defined. Each step
should have a clear purpose and should be executable without any ambiguity.
2. Finite: An algorithm must have a finite number of steps. It should eventually terminate after
completing all the necessary instructions.
3. Input: Algorithms take some input, which is processed to produce an output. The input could be
data, values, or information required to perform the task.
4. Output: After the algorithm has completed its execution, it should produce a result or output that
solves the problem or accomplishes the task.
5. Deterministic: The steps in an algorithm should be deterministic, meaning that given the same input,
it will produce the same output every time it's executed.
6. Feasible: The algorithm should be practical and feasible to execute. It should not rely on infinite
resources or take an unreasonable amount of time to complete.
7. Correctness: The algorithm must produce the correct output for all valid inputs. It should solve the
problem it is designed to address

Example:
Algorithm: Adding Two Numbers
1. Get the value of the first number.
2. Get the value of the second number.
3. Add the value of the first number to the value of the second number.
4. Display the sum obtained from step 3 as the output.

IPO Model

Input Process Output

Get the value of first Add the value of first


Display the sum
and second number and second number

1.2.4 Pseudocode
Pseudocode is a high-level, informal way of representing an algorithm or program.
It is not specific to any particular programming language and is designed to be easily
understandable by humans. Pseudocode uses simple and natural language-like
constructs to outline the logical steps of an algorithm or program without focusing on
specific syntax.

The purpose of pseudocode is to provide a clear and concise representation of the


algorithm's logic, making it easier to understand the overall flow and structure of the
solution. It acts as an intermediate step between a written description of an algorithm
and the actual code implementation.
1 | Computer Programming 1 4

Pseudocode is often used in the early stages of software development to plan and
design algorithms before writing code in a specific programming language. It is a
valuable tool for developers to communicate and collaborate with each other, as well as
with stakeholders, without getting bogged down by language-specific syntax and
technical details.

Example
Pseudocode: Addition of Two Numbers
1. Start
2. Read the value of num1
3. Read the value of num2
4. Set sum = num1 + num2
5. Display sum as the output
6. End

1.2.5 Flowchart
A flowchart is a visual representation of an algorithm or a process. It uses various
symbols and arrows to depict the sequence of steps and decision points in a clear and
easy-to-understand manner. Flowcharts are widely used in various fields, including
computer programming, business processes, and problem-solving, to visually represent
complex procedures.

Flowcharts typically consist of the following key elements:


1. Terminal/Start-End Symbol: This symbol represents the starting and ending points of the flowchart.
It is usually an oval shape with the word "Start" or "End" written inside.
2. Process Symbol: This symbol represents a specific action or process to be performed. It is usually
represented by a rectangle with a description of the action written inside.
3. Input/Output Symbol: This symbol represents an input or output operation. It is usually represented
by a parallelogram with a description of the input or output operation written inside.
4. Decision Symbol: This symbol represents a decision point where a question is asked, and the flow of
the process may diverge based on the answer. It is usually represented by a diamond shape with a
question inside.
5. Arrow/Flow Lines: These arrows connect the different symbols and show the flow of the process
from one step to another.

A flowchart is defined as a pictorial representation of an algorithm. It serves as a


means of recording, analyzing, and communicating problem information.
Programmers often use a flowchart before writing a program, although this is not
1 | Computer Programming 1 5

always mandatory. Practically speaking, sometimes drawing of the flowchart and


writing of the code in a high-level language go side by side. Flowcharts are of two
types.
(a) Program flowchart
(b) System flowchart
A program flowchart (or simply a flowchart) shows the detailed processing steps
within one computer program and the sequence in which those steps must be executed.
Different notations are used in a flowchart to denote the different operations that take
place in a program.

On the other hand, system flowcharts show the procedures involved in


converting data on input media to data in output form. Here, the focus is on the data
flow into or out of a program, the forms of input, and the forms of the output. Please
understand that a system flowchart makes no attempt to depict the function-oriented
processing steps within a program. A system flowchart may be constructed by the
system analyst as part of problem definition.

Golden Rule
1. Each algorithm should have only one entry point and one exit point.
2. A null statement is shown by a flow line; there is no symbol for null.
1 | Computer Programming 1 6

Some of the notations used are shown in Figure 1.2.


An oval shows the beginning or ending of an algorithm. Flow lines show the order or
sequence of actions in a program. A connector is used when we reach the end of a page.
It is also used when we need to show the logic details that do not fit in the flow.
Assignment statements are shown using a rectangle, as in the following:

Variable ← Expression

A parallelogram shows any input read or output produced. A rectangle with two
vertical bars inside represents a module call. Selection statements (Decisions) may be
two-way or multi-way selections. In both cases, the statement ends with a connector
where the true and false flows join. In this case, the connector has nothing in it. This is
applicable to switch statements or even for, while, and do-while loops of C

Both flowcharts and algorithms are isomorphic—that is, one can be converted to
another form without the loss of data (although there are some differences).

Let us now distinguish between a flowchart and an algorithm


Flowchart Algorithm
1. The graphical or pictorial representation of the logic of 1. Step-by-step finite procedure of
the problem in hand solving the problem in hand
2. Use of different shapes that are joined by flow lines 2. Written step by step
1 | Computer Programming 1 7

3. Use of flow lines 3. Flow control moves from top to bottom


4. The task to be performed written within boxes in 4. All instructions written in English
English
5. Easily converted into algorithm or program in high- 5. Easily converted into flowchart or program in high-
level language level language
6. Drawn after writing algorithm. 6. Normally written before flowchart

1.2.6 C Programming Language


C is a general purpose, block-structured, procedural, case sensitive, freeflow,
portable, powerful high-level programming language. This language is so powerful that
an operating system like UNIX is itself coded in C. It is said that programming
languages are born, age, and eventually die but the C programming language has only
matured from the time it was born. It has the same relevance today as it had when it
was developed by Dennis Ritchie at Bell Telephone Laboratories in 1972.

Key Features of C:
1. Procedural Language: C is a procedural programming language, which means it follows a top-down
approach, breaking a program into functions that perform specific tasks.
2. Portability: C programs can be compiled and run on different platforms with minimal changes,
making them highly portable.
3. Efficiency: C allows direct access to memory and hardware, making it an efficient language for
system-level programming and resource-constrained environments.
4. Standard Library: C comes with a standard library that provides a set of functions for common
operations, such as I/O, string manipulation, and mathematical operations.
5. Pointers: C supports pointers, which are variables that store memory addresses. Pointers are a
fundamental feature for memory management and data manipulation.
6. Static Typing: C is statically typed, meaning variable types must be declared before they are used,
enhancing type safety and performance.
1 | Computer Programming 1 8

7. Modularity: C allows modular programming, enabling developers to create reusable code through
functions and libraries.
8. Low-Level Access: C provides low-level access to memory and allows bitwise operations, making it
suitable for low-level programming tasks.
9. Extensibility: C can be extended with custom libraries or system-specific code, making it suitable for
embedded systems and operating systems development.

Working of the Preprocessor

When you issue the command to compile a C program, the program is run
automatically through the preprocessor. The preprocessor is a program that modifies the
C source program according to the directives supplied in the program. An original
source program is usually stored in a file. The preprocessor does not modify this
program file but creates a new file that contains the processed version of the program.
This new file is then submitted to the compiler. When we compile this program, the
compiler shows the number of lines compiled to be greater than 6. This is because during
the preprocessing stage, the include preprocessor directive searches the file iostream.h in
the prescribed list of directories and if the header file is found, the include directive is
replaced by the entire content of the header file. If the included header file contains
another include directive, it will also be processed. This processing is carried out
recursively till either no include directive remains or till the maximum translation limit is
achieved (ISO specifies a maximum of 15 nested levels of include files). Thus, one line of
source code gets replaced by multiple lines of the header file. During the compilation
stage, these added lines will also be compiled. Thus, the compiler shows the number of
lines to be greater than 6.

Some compilers enable the programmer to run only the preprocessor on the source program
and to view the results of the preprocessor stage.

Please remember the following points regarding preprocessor directives:

1. They are placed at the beginning of the program.


2. They start with a pound/hash (#) sign. This is its syntax (rule).
3. There is no semicolon at the end of these directives.
4. They can start in any column but usually start in column 1.
5. They tell the compiler to include the standard input/output library file in the program. This library file
prints a message onto the screen. Printing is one of the input/output processes identified in this library.
6. The syntax of this command must be exact.
7. There is no space between the pound/hash sign and the reserved keyword include.
8. It tells the preprocessor that you want the library file in angular brackets (< >) to be included in your
program. The name of this header file is iostream.h. It stands for “Standard input/output header stream
file.”
9. The angular brackets tell the preprocessor to search for the file in one or more standard directories.
These directories contain header files that are provided by the system and those commonly used by
several programmers (if the computer is a multiple-user machine).
10. Sometimes the brackets are replaced with double quotation marks as in the following:
#include “iostream.h”
In this case the preprocessor will first look in the programmer’s own directory or the same one that
contains the program file. If it is NOT found there, then the standard directories are searched.
11. The preprocessor can also add or delete C program statements.
1 | Computer Programming 1 9

12. A preprocessor symbol is never replaced if it occurs within single or double quotation marks. 13.
Header files also provide consistency among several program files. Often a program is large and thus it
is possible to break it into smaller units, each of which is stored in a separate file. After these separate
program files are compiled, they must somehow be linked together to form a single program. This
linking is usually done by a program called the linkage editor (which is run automatically when a
program is compiled).
14. A header file can contain other #include directives. Please note that it cannot include itself because this
would result in an infinite recursion. Also note that it cannot include another file that includes the first
file as this would also result in an infinite recursion.
15. Header files can contain any text at all. They may also contain C code to define structure templates,
global variables, or function definitions. However, header files usually do not contain function
definitions as it is more efficient to store these in the function libraries set up by the user. But global
variables are sometimes included.
16. If iostream.h is included in the program, the macro versions (to be discussed later) are used. On the
other hand, if the programmer wishes to use the function versions they can be undefined after the
#include directive.
17. A preprocessor directive is terminated by the end of the line it occupies but if necessary it can be
continued onto one or more lines by ending all but the last line with backslashes.

Definition Section
In this section, we define a variable with some value in it. This is also known as a
special constant section. Here, a define statement is used. The general syntax of a
symbolic preprocessor or symbolic compiler directive can be defined using the following
#define statement:
1 | Computer Programming 1 10

Global Declaration Section


Sometimes we need the same variables in both the functions and in the main
program. In such cases, we use global variables. These variables are declared normally
but are placed before the start of the main program. Please understand here that if you
want the data variables to be available in all parts of the program, then you have to
either declare the variables as global or they must be passed as arguments (to be studied
a bit later). Also understand that by using any of these methods the ultimate objective is
that the data be available in all of the functions.
Whenever there is a conflict between a global and a local variable, the local variable gets first priority.

The main( ) Function


The main function is where the compiler starts executing the program first. If you
don’t have a main( ), your program will be compiled successfully but it will not run and
the compiler will report an error that you have not defined a main( ) function. That is
why a main( ) function is necessary. It is a special function that acts as a container for
the entire C program. This section of our code contains input and output as well as the
processing statements. It may or may not have a return type as follows:

void main(void) //here everything is void as there is no value being returned


by the main and that no arguments are being passed

On the other hand, compare the following statement:

Here the opening curly bracket “{” shows the starting of the main program while “}”
shows its end. The variables declared after these braces are known as local variables that is,
they have a scope local to this main function. Then comes the portion where the user writes
his or her program. This is known as the user-defined section.

ASSESSMENT
1 | Computer Programming 1 11

1. What are the two main learning outcomes of this module?


2. Explain the significance of algorithms, pseudocode, and flowcharts in computer
programming.
3. List and briefly explain the stages of the programming process.
4. How does the C programming process differ from the general programming
process?
5. Describe the key features of procedural programming in the context of C.
6. Define an algorithm and list the essential characteristics of a well-defined algorithm.
7. Create a simple pseudocode representation of the algorithm for adding two
numbers.
8. What is the IPO model, and how does it relate to algorithms?
9. Differentiate between a flowchart and an algorithm. Provide examples of both.
10. Discuss the importance of the "main" function in the C programming language, and
explain its purpose in a program.

You might also like