CSC 2312 Structured Programming 2-1
CSC 2312 Structured Programming 2-1
FACULTY OF SCIENCE
DEPARTEMNT OF COMPUTER SCIENCE
PART TWO
CHAPTER FOUR
Elements of structured Programming:
CSC2312
Structured Programming
Each function is design to do a specific task with its own data and logic. Information can be
passed from one function to another function through parameters. A function can have local
data that cannot be accessed outside the function’s scope. The result of this process is that
all the other different functions are synthesized in another function. This function is known
as main function. Many of the high-level languages support structured programming.
Structured programming minimizes the chances of the function affecting another. It allows
for clearer programs code. It made global variables to disappear and replaced by the local
variables. Due to this change one can save the memory allocation space occupied by the
global variable. Its organization helps in the easy understanding of programming logic. So
that one can easily understand the logic behind the programs. It also helps the newcomers
of any industrial technology company to understand the programs created by their senior
workers of the industry. The languages that support Structured programming approach are:
•C
• C++
• Java
• C#
• Pascal
1. Structured programming
The structured programming method is a simplified form of imperative
programming. The crucial difference from the basic principle is that instead of
absolute jump commands (instructions that lead to processing continuing at another
point instead of the next command), this software programming paradigm makes use
of control loops and structures. An example is the use of “do…while”, which
executes an instruction automatically for as long as a particular condition is true (at
least once).
2. Procedural programming
The procedural programming paradigm extends the imperative approach with the
possibility of dividing algorithms into more manageable sections. Depending on the
programming language, these are referred to as sub-programs, routines, or functions.
The purpose of this division is to make the programming code clearer and to prevent
unnecessary code repetitions. Because of the abstraction of algorithms, the procedural
software paradigm represents a crucial step from simple assembly languages towards
more complex high-level languages.
3. Modular programming
Modular programming is categorized as a subordinate form of the imperative
programming paradigm. It is essentially very similar to the procedural method and
applies this programming style to the requirements and demands of larger and more
comprehensive software projects. It involves selectively breaking down the source
code into logical, independent sub-blocks to ensure greater clarity and to simplify the
debugging process. The sub-blocks, known as modules, can be tested individually
before they are subsequently linked together to create a collective application.
4. Object Oriented
In object-oriented programming, OOP, a program code is organized into objects that
contain state that is only modified by the code that is part of the object. Most object-
oriented languages are also imperative languages to some extent. Examples of these
languages include: Java, C++, Python, Smalltalk, C# and a lots of others. The key
concepts of OOP are
3
i. Objects
ii. Classes
iii. Subclasses
iv. Inheritance
v. Inclusion polymorphism.
There are much bigger differences between subordinate forms of the declarative
programming paradigm than there are between those of the imperative approach. In
addition, they are not always so precisely defined or categorized. The two most
important methods in the declarative programming paradigm are functional and logic
programming.
1. Functional programming
Functions exist in every higher-level programming language. However, the
functional approach in software development deals with functions in a very particular
way.
A functionally programmed program is made up of a string of function calls, where
each program section can be understood as a function. In functional programming,
the functions can take on different forms. For example, they can be linked to one
another like data or be used in the form of parameters. In addition, they can
subsequently be used as function results. Conversely, the paradigm leads to there
being no independent assignment of values.
2. Logic programming
The logic programming method, also known as predicate programming, is based on
mathematical logic. Instead of a sequence of instructions, software programmed
using this method contains a set of principles, which can be understood as a collection
of facts and assumptions. All inquiries to the program are processed, with the
4
interpreter applying these principles and previously defined rules to them in order to
obtain the desired result.
The key concepts of logic programming are therefore:
i. Assertions
ii. Horn clauses
iii. Relations
5
CHAPTER FIVE
STRUCTURED PROGRAMMING WITH C
5.1 Brief History of C
• The C programming language is a structure-oriented programming language,
developed at Bell Laboratories in 1972 by Dennis Ritchie.
• C programming language features were derived from an earlier language called “B”
(Basic Combined Programming Language –BCPL)
• C language was invented for implementing UNIX operating system.
• In 1978, Dennis Ritchie and Brian Kernighan published the first edition “The C
Programming Language” and is commonly known as K&RC.
• In 1983, the American National Standards Institute (ANSI) established a committee
to provide a modern, comprehensive definition of C. The resulting definition, the
ANSI standard, or “ANSI C” was completed late 1988.
• Many of C’s ideas & principles were derived from the earlier language B, thereby
naming this new language “C”.
5.6 Uses of C
The C programming language is used for developing system applications that forms a
major portion of operating systems such as Windows, UNIX and Linux. Below are some
examples of C being used:
Database systems
Graphics packages
Word processors
Spreadsheets
Operating system development
Compilers and Assemblers
Network drivers and Interpreters
7
CHAPTER SIX
C PROGRAM DESIGN
6.1 C Program Structure
The structure of a C program is a protocol (rules) to the programmer, which he has to follow
while writing a C program. The general basic structure of C program is shown in the code
below. Based on this structure, we can write a C program.
Example:
/* This program accepts a number and displays it to the user*/
#include <stdio.h>
void main(void)
{ int number;
printf( "Please enter a number: " );
scanf( "%d", &number );
printf( "You entered %d", number );
return 0;
}
Explanation:
o #include: The part of the compiler which actually gets your program from the source
file is called the preprocessor.
o #include <stdio.h>:#include is a pre-processor directive. It is not really part of our
program, but instead it is an instruction to the compiler to make it do something. It
tells the C compiler to include the contents of a file (in this case the system file called
stdio.h).
o The compiler knows it is a system file, and therefore must be looked for in a special
place, by the fact that the filename is enclosed in <> characters <stdio.h>: stdio.h is
the name of the standard library definition file for all STanDard Input and Output
functions.
✓ The program will almost certainly want to send information to the screen and read
things from the keyboard, and stdio.h is the name of the file in which the functions
that we want to use are defined.
✓ The function we want to use is called printf. The actual code of printf will be tied in
later by the linker.
✓ The ".h" portion of the filename is the language extension, which denotes an include
file.
o void:This literally means that this means nothing. In this case, it is referring to the
function whose name follows. Void tells C compiler that a given entity has no
meaning and produces no error.
o main:In this example, the only function in the program is called main. A C program
is typically made up of large number of functions. Each of these is given a name by
the programmer and they refer to each other as the program runs. C regards the name
main as a special case and will run this function first i.e. the program execution starts
from main.
8
o (void): This is a pair of brackets enclosing the keyword void. It tells the compiler that
the function main has no parameters.
✓ A parameter to a function gives the function something to work on.
o { (Brace): This is a brace (or curly bracket). As the name implies, braces come in
packs of two - for every open brace there must be a matching close one. Braces allow
us to group pieces of program together, often called a block. A block can contain the
declaration of variable used within it, followed by a sequence of program statements.
In this case the braces enclose the working parts of the function main.
o ; (semicolon): The semicolon marks the end of the list of variable names, and also the
end of that declaration statement. All statements in C programs are separated by ";"
(semicolon) characters. The ";" character is actually very important. It tells the
compiler where a given statement ends.
o If the compiler does not find one of these characters where it expects to see one, then
it will produce an error.
o scanf:In other programming languages, the printing and reading functions are a part
of the language. In C this is not the case; instead they are defined as standard functions
which are part of the language specification, but are not a part of the language itself.
o The standard input/output library contains a number of functions for formatted data
transfer; the two we are going to use are scanf (scan formatted) and printf (print
formatted).
o printf:The printf function is the opposite of scanf.
o It takes text and values from within the program and sends it out onto the screen.
o Just like scanf, it is common to all versions of C and just like scanf, it is described in
the system file stdio.h.
o The first parameter to a printf is the format string, which contains text, value
descriptions and formatting instructions.