0% found this document useful (0 votes)
2 views9 pages

CSC 2312 Structured Programming 2-1

The document discusses structured programming concepts, emphasizing the importance of breaking programs into functions or modules for clarity and efficiency. It outlines the advantages and disadvantages of structured programming, as well as various programming paradigms including imperative and declarative approaches. Additionally, it provides a brief history of the C programming language, its characteristics, and its structure, along with examples of its applications.

Uploaded by

xcellency700
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views9 pages

CSC 2312 Structured Programming 2-1

The document discusses structured programming concepts, emphasizing the importance of breaking programs into functions or modules for clarity and efficiency. It outlines the advantages and disadvantages of structured programming, as well as various programming paradigms including imperative and declarative approaches. Additionally, it provides a brief history of the C programming language, its characteristics, and its structure, along with examples of its applications.

Uploaded by

xcellency700
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

YOBE STATE UNIVERSITY, DAMATURU

FACULTY OF SCIENCE
DEPARTEMNT OF COMPUTER SCIENCE

PART TWO
CHAPTER FOUR
Elements of structured Programming:
CSC2312
Structured Programming

4.1 Structured Programming Concept:


In structured programming design, programs are broken into different functions these
functions are also known as modules, subprogram, subroutines or procedures.

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

Advantages of Structured programming


i. It is user friendly and easy to understand.
ii. Similar to English vocabulary of words and symbols.
iii. It is easier to learn.
iv. They require less time to write.
v. They are easier to maintain.
vi. These are mainly problem oriented rather than machine based.
vii. Program written in a higher-level language can be translated into many machine
languages and therefore can run on any computer for which there exists an
appropriate translator.
viii. It is independent of machine on which it is used, i.e., programs developed in high
level languages can be run on any computer.

Disadvantages of Structured Programming


i. Structured programming codes implemented in high-level language has to be
translated into the machine language by translator and thus a price in computer time
is paid.
ii. The object code generated by a translator might be inefficient compared to an
equivalent assembly language program.
iii. Data type are proceeds in many functions in a structured program. When changes
occur in those data types, the corresponding change must be made to every location
that acts on those data types within the program. This is really a very time-consuming
task if the program is very large.
iv. In a structured program, each programmer is assigned to build a specific set of
functions and data types. Since different programmers handle separate functions that
have mutually shared data type, other programmers in the team must reflect the
changes in data types done by the programmer in data type handled. Otherwise, it
requires rewriting several functions.

4.2 PROGRAMMING PARADIGMS


Programming paradigms are the fundamental principles used when developing
software. They are best described as fundamentally different programming styles,
which in turn result in differently structured software code.

When programs are developed to solve real-life problems like inventory


management, payroll processing, student admissions, examination result processing,
etc. they tend to be huge and complex. The approach to analyzing such complex
problems, planning for software development and controlling the development
process is called programming methodology. New software development
methodologies (e.g. object Oriented Software Development) led to new paradigms in
programming and by extension, to new programming languages. A programming
paradigm is a pattern of problem-solving thought that underlies a particular genre of
programs and languages. Also, a programming paradigm is the concept by which the
methodology of a programming language adheres to.

Basic classes of programming paradigms include the Imperative (Classical) and


Declarative (New Trend).
Among programming paradigms for software development, imperative programming
is the classic variant. The first programming languages – and correspondingly, the
first computer programs – were based entirely on this classic approach, which
provides a controlled sequence of specific commands (the name comes from the Latin
imperare meaning “command”) or instructions. For example, this programming
paradigm is the basis for early classics like Pascal and C, as well as all assembly
languages. Imperative programming focuses in part on working as closely as possible
2
with the system. The resulting program code is therefore easy to understand but also
very extensive.
Within the imperative programming paradigm, there are three important subordinate
methods for writing and structuring software code: structured, procedural, Object
Oriented and modular programming.
The key concepts of imperative programming languages are
i. Variables
ii. Commands
iii. Procedures
iv. Data abstraction.

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.

Declarative programming: Paradigm of The Recent Past


In parallel to the continuous ongoing development of hardware and software, the
declarative method developed as an alternative paradigm for code programming. The
fundamental principle of declarative programming is that it describes the desired end
result. Thus, it is primarily about the “what” – the result – rather than the “how” – the
steps towards the solution – as is the case with imperative programming. As a
consequence, because of the high level of abstraction, the code generated by
declarative programming is much more difficult to understand. At the same time, it
is short and precise.

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.

This subsidiary form of declarative programming is very important for computer


science in general – and at the same time can be used for a wide range of specific
purposes. The special handling of functions enables programmers using the
functional method to create and use extensive calculation rules made up of functions.

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

Prolog is an example of a programming language that follows the Logic paradigm.


PROLOG's primitive values are numbers and atoms. Atoms can be compared with
one another, but have no other properties. They are used to represent real-world
objects that are primitive.

Comparison of programming paradigms: imperative and declarative


programming
Imperative programming Declarative programming paradigm
paradigm
“How?” “What?”
Classic New trend
A program based on this paradigm is A program based on this paradigm is
made up of a series of instructions that made up of instructions for how the program
tell the computer what it should should deal with an input. Calculations are
calculate/do and in what order. performed by manipulation of values, with the
procedure controlled by the process of
recursion.
The name comes from “imperare”, The name comes from “declarare”, the Latin
the Latin word for “command”. word for “describe”.
The desired solution path is specified The desired result is specified.
Typical programming languages Typical programming languages include Lisp,
include C, Pascal, Fortran, ALGOL, ML, Haskell, F#, Prolog, and Oz
and all assembly languages.

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.2 Taxonomy of C Types


o Scalar types
o Arithmetic types
o Integral types: char, short, int, long
o Floating-point types: float, double, long double
o Pointer types
o Aggregate types
o Array types
o Structure types
o Union types
o Function types
o Void types

5.3 Why is C Popular?


o It is reliable, simple and easy to use.
o C is a small, block-structured programming language.
o C is a portable language, which means that C programs written on one system can be
run on other systems with little or no modification.
o C has one of the largest assortments of operators, such as those used for calculations
and data comparisons.
o Although the programmer has more freedom with data storage, the languages do not
check data type accuracy for the programmer.

5.5 Characteristics of a C Program


Middle level language.
Small size – has only 32 keywords
6
Extensive use of function calls- enables the end user to add their
own functions to the C library.
Supports loose typing – a character can be treated as an integer &
vice versa.
Structured language
Low level (Bit Wise) programming readily available
Pointer implementation - extensive use of pointers for memory,
array, structures and functions.
It has high-level constructs.
It can handle low-level activities.
It produces efficient programs.
It can be compiled on a variety of computers

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.

6.2 Files Used in a C Program


o Source File- This file contains the source code of the program. The file extension of
any c file is .c. The file contains C source code that defines the main function &
maybe other functions.
o Header File- A header file is a file with extension .h which contains the C function
declarations and macro definitions and to be shared between several source files.
o Object File- An object file is a file containing object code, with an extension .o,
meaning relocatable format machine code that is usually not directly executable.
Object files are produced by an assembler, compiler, or other language translator, and
used as input to the linker, which in turn typically generates an executable or library
by combining parts of object files.
o Executable File- The binary executable file is generated by the linker. The linker links
the various object files to produce a binary file that can be directly executed.

You might also like