0% found this document useful (0 votes)
17 views13 pages

Lesson 1

Uploaded by

BRIAN
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)
17 views13 pages

Lesson 1

Uploaded by

BRIAN
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/ 13

STRUCTURED PROGRAMMING

WITH C

Page 1
TABLE OF CONTENT
1.0 Programming concepts ...................................................................... 5
1.1 Programming concepts ......................................................................................5
1.2 Generation of programming languages ....................................................................6
2.0 Programming approaches ................................................................... 8
2.1 Programming approaches .....................................................................................8
3.0 Program development ...................................................................... 10
3.1 Definition........................................................................................................ 10
3.2 Program specification ........................................................................................ 10
3.2 Program development cycle ................................................................................ 10
4.0 Program design ............................................................................... 13
4.1 Define program design ...................................................................................... 14
4.2 Design approaches ............................................................................................ 14
4.3 Design tools .................................................................................................... 15
5.0 Introduction to structured programming using c language ......................... 24
5.1 C concepts ...................................................................................................... 24
5.2 C programming environment .............................................................................. 25
5.3 C program format ............................................................................................. 26
6.0 Fundamentals of c programming ......................................................... 28
6.1 C fundamentals ................................................................................................ 28
6.2 Control structures ............................................................................................. 52
6.2 Concepts of sub programs .................................................................................. 69
Passing Arguments to main() ................................................................................... 72
7.0: Pointers and data structures............................................................... 82
7.1 Pointers .......................................................................................................... 82
7.2 Implementing pointers ....................................................................................... 83
7.3 Data structures ................................................................................................. 89
8.0: sorting and searching..................................................................... 100
8.1 Searching ...................................................................................................... 100
8.2 Sorting techniques. ......................................................................................... 102
9.0: File handling. .............................................................................. 112
9.1 File concepts.................................................................................................. 112
9.2 Types of files. ................................................................................................ 112
9.3 Writing, opening, appending and closing files ...................................................... 113
10: Program documentation .................................................................. 117
10.1 Uses of program documentation ....................................................................... 118
10.2 Types of Program Documentation .................................................................... 118
11.0: Emerging trends in programming ................................................... 119
11.1 Emerging trends in programming. .................................................................... 119
11.2 Challenges of Emerging Trends in Structured Programming .................................. 121
12.0 References ................................................................................. 123

Page 2
MODULE UNIT SUMMARY

SUB-MODULE UNIT CONTENT TOTAL (HRS)


THEO PRAC TOT
RY TICE AL
PROGRAMMING CONCEPTS  Programming concepts 10 2 12
 Generation of programming
languages

PROGRAMMING APPROACHES  Programming approaches 6 2 8


/paradigms

PROGRAM DEVELOPMENT  Program specification


 Program development cycle

PROGRAM DESIGN  Define program design 8 14 22


 Design approaches
 Design tools

INTRODUCTION TO  C concepts 4 4 8
STRUCTURED PROGRAMMING  C programming environment
USING C LANGUAGE  C program format

FUNDAMENTALS OF C  Control structures in C 22 40 62


PROGRAMMING  Concepts of sub program

POINTERS AND DATA  Description of pointers 14 14 28


STRUCTURES  Implementing pointers
 Data structures

SORTING AND SEARCHING  Searching Techniques 10 10 20


 Sorting Techniques

FILES  File concepts 4 6 10


 Types of files
 Writing, opening, appending and
closing files

PROGRAM DOCUMENTATION  Program documentation 4 6 10


definition
 Types of program
documentation

Page 3
EMERGING TRENDS IN  Identification of emerging 6 0 6
STRUCTURAL PROGRAMMING trends
 Challenges of emerging
trends
 Coping with the challenges
of emerging tends

Page 4
1.0 Programming concepts

1.1 Programming concepts

 Program -A computer program is a set of instructions that performs a specific task when
executed by a computer. A computer requires programs to function.
 Programming- is the process of creating a set of instructions that tell a computer how to
perform a task. Programming can be done using a variety of computer "languages," such as
SQL, Java, Python, and C++.
 Compiler - a program that converts instructions into a machine-code or lower-level form so that
they can be read and executed by a computer.
 A text editor is a computer program that lets a user enter, change, store, and usually print text .
 A linker or link editor is a computer utility program that takes one or more object files
generated by a compiler and combines them into a single executable file, library file, or another
'object' file.
 Loader: A program which loads the executable file to the primary memory of the machine.

Structured programming – Structured programming is a programming paradigm aimed at


improving the clarity, quality, and development time of a computer program by making extensive use
of the structured control flow constructs of selection (if/then/else) and repetition (while and for), block
structures, and subroutines
The main characteristics of Structured Programming are:
1. The code should be in modular nature.
2. There should be single entry and single exit for each module ( i.e. no unconditional gates).
3. At least one constructs each for sequence, condition and iteration.

 Computer hardware - refers to the physical parts of a computer and related devices. Internal
hardware devices include motherboards, hard drives, and RAM. External hardware devices
include monitors, keyboards, mice, printers, and scanners.
 The internal hardware parts of a computer are often referred to as components, while external
hardware devices are usually called peripherals. Together, they all fall under the category of
computer hardware. Software, on the other hand, consists of the programs and applications that
run on computers. Because software runs on computer hardware, software programs often have
system requirements that list the minimum hardware required for the software to run.

 Software - refers to the set of electronic program instructions or data a computer processor
reads in order to perform a task or operation. In contrast, the term 'hardware' refers to the
physical components that you can see and touch, such as the computer hard drive, mouse, and
keyboard. Software can be categorized according to what it is designed to accomplish. There are
two main types of software: systems software and application software.

Page 5
 Systems Software-System software is a type of computer program that is designed to run a
computer's hardware and application programs. If we think of the computersystem as a layered
model, the system software is the interface between the hardware and user applications.

 Applications Software-Application software, or simply applications, are often called
productivity programs or end-user programs because they enable the user to complete tasks,
such as creating documents, spreadsheets, databases and publications, doing online research,
sending email, designing graphics, running businesses, and even playing games! Application
software is specific to the task it is designed for and can be as simple as a calculator application
or as complex as a word processing application.

 Program-This is a complete set of step-by-step instructions that control and direct the computer
hardware in carrying out a given task. Tasks may vary from very simple e.g. computing
surface area to complex ones like statistical analysis.
Programs are usually written to solve user problems on a computer.

 Programming Language- A programming language is a vocabulary and set of grammatical


rules for instructing a computer or computing device to perform specific tasks. The term
programming language usually refers to high-level languages, such as BASIC, C, C++,
COBOL, Java, FORTRAN, Ada, and Pascal.
 Syntax- These are the rules of a language that govern the ways in which words, symbols,
expressions and statements may be formed and combined in that language.

1.2 Generation of programming languages

A program can be written in a variety of programming languages. The languages can broadly be
classified into two categories:

 Low-level language – which refers to the machine language and assembly language.
 High-Level languages: - which refers to languages such as COBOL, FORTRAN, BASIC

LOW LEVEL LANGUAGES

Machine programming Language

Machine code or object code, machine language is a collection of binary digits or bits that the
computer reads and interprets. Machine language is the only language a computer is capable of
understanding.

Advantages
 Program translation was fast because no conversion was required.
 The program could directly address and control the internal circuitry meaning that these
programs were more effective in hardware usage and control.
Disadvantages
 Writing programs was time consuming
 Tracing errors in a program was extremely difficult.

Page 6
 Difficult to learn and use.
 Program modification was cumbersome.
 They were machine dependent i.e. a program created for one type of machine would not work
on a different type of machine.
 To write an effective program the programmer had to have expert knowledge on the computer’s
internal workings.

Second generation /Assembly Language

This language was more user oriented than machine language. Instructions are represented using
mnemonic code and symbolic addresses. Words like add, sum etc could be used in programs. An
assembler translated these codes into machine language.

Advantages
 Much easier to learn compared to machine language.
 Coding took less time than coding using machine language.
 Error correction was less cumbersome.

Disadvantages
 Were also machine specific
 Execution took longer than machine language programs due to the translation process.

HIGH LEVEL LANGUAGES

These languages are user friendly and problem oriented compared to the low level languages.
Programs written in high level languages are shorter than the machine language programs.

Program instructions are written using familiar English-like statements and mathematical statements.

A single high-level language program is translated into multiple machine code instructions.

Advantages
 They are portable i.e. they can be used on more than one type of machine.
 Creating program and error correction takes less time.
 Are relatively easy to learn and use.
 The programmer does not have to be an expert on the internal workings of the machine.

Disadvantages
 Program execution takes more time due to the translation process.
 They do not address the internal circuitry of computers as effectively as the low level languages.
 A translated program will occupy more space.

High level languages can further be classified into:


 Procedural languages (Third Generation)
 Non-Procedural Languages (Fourth Generation Languages or 4GLs)

Page 7
Third generation/Procedural languages

They require the programmer to specify step-by-step how the computer will accomplish a specific task.
Program execution follows the exact sequence laid down by the programmer during coding. Examples
include FORTRAN, C, BASIC,

Fourth Generation Languages or 4GLs/Non-Procedural Languages

They allow the programmer to specify the desired result without having to specify the detailed
procedure needed to achieve the result.

They are more user oriented and allow programmers to develop programs with fewer commands
compared with 3rd generation languages. They are called non procedural because programmers can
write programs that need only tell the computer what they want done, not all the procedures of doing it.

4GL consists of:


 Report Generators: also called report writers. This is a program for end users that is used to
produce reports.
 Query Language: This is an easy to use language for retrieving data from a database
management system.
 Application Generators: This is a program’s tool that allows a person to give a detailed
explanation of what data to be processed. The software then generates codes needed to create a
program to perform the tasks.

Fifth-generation programming language

Fifth-generation languages are used mainly in artificial intelligence research. OPS5 and Mercury are
examples of fifth-generation languages. These types of languages were also built upon Lisp, many
originating on the Lisp machine, such as ICAD. Then, there are many frame languages, such as KL-
ONE

2.0 Programming approaches

Programming paradigms are a way to classify programming languages according to the style of
computer programming. Features of various programming languages determine which programming
approaches they belong to; as a result, some languages fall into only one approach, while others fall
into multiple paradigms. Some paradigms are concerned mainly with implications for the execution
model of the language, such as allowing side effects, or whether the sequence of operations is defined
by the execution model.

2.1 Programming approaches

Unstructured programming.

An unstructured program is a procedural program – the statements are executed in sequence as written.
But this type of programming uses the go to statement. A go to statement allows control to be passed to
any other place in the program. When a go to statement is executed, the sequence continues from the
target of the go to. Thus to understand how a program works, you have to pretend to execute it. This

Page 8
means that it is often difficult to understand the logic of such a program. Some program compilers
cross-index where a goto connects to, making it practical to rapidly navigate the source code. However,
it was a common practice in some programming languages to use a variable in association with where
the goto goes, making automated indexing impractical. There are similar problems in some structured
programming languages, such as how foreign language views are implemented, to permit many people
to view the same computer data, in their human language.

Structured programming

A procedural language is a type of computer programming language that specifies a series of well-
structured steps and procedures within its programming context to compose a program. It contains a
systematic order of statements, functions and commands to complete a computational task or program.

Event-driven programming
Is a programming approach in which the flow of the program is determined by events such as user
actions (mouse clicks, key presses), sensor outputs, or messages from other programs or threads. Event-
driven programming is the dominant paradigm used in graphical user interfaces and other applications
(e.g., JavaScript web applications) that are centered on performing certain actions in response to user
input. This is also true of programming for device drivers (e.g., P in USB device driver stacks)

Object Oriented Programming

Object-oriented programming (OOP) is a software programming model constructed around objects.


This model compartmentalizes data into objects (data fields) and describes object contents and behavior
through the declaration of classes (methods).
OOP features include the following:
 Encapsulation: This makes the program structure easier to manage because each object’s
implementation and state are hidden behind well-defined boundaries.
 Polymorphism: This means abstract entities are implemented in multiple ways.
 Inheritance: This refers to the hierarchical arrangement of implementation fragments.
Object-oriented programming allows for simplified programming. Its benefits include reusability,
refactoring, extensibility, maintenance and efficiency.

Visual Programming

In computing, a visual programming language (VPL) is any programming language that lets users
create programs by manipulating program elements graphically rather than by specifying them
textually. A VPL allows programming with visual expressions, spatial arrangements of text and graphic
symbols, used either as elements of syntax or secondary notation. For example, many VPLs (known as
dataflow or diagrammatic programming) are based on the idea of "boxes and arrows", where boxes or
other screen objects are treated as entities, connected by arrows, lines or arcs which represent relations.

Internet Based programming.

refers to the writing, markup and coding involved in Web development, which includes Web content,
Web client and server scripting and network security. The most common languages used for
Web programming are XML, HTML, JavaScript, Perl 5 and PHP.

Page 9
Factors to consider when choosing a programming language

 Popularity. You are more likely to find people to collaborate with if you use a popular language.
You are also more likely to find reference material and other help. Unfortunately, the most popular
language globally may not be a good match for your problem domain.
 Language-domain match. Choose one that matches your problem domain. You can do this by
looking at what other people in your field are using (after adjusting for popularity, so don't think the
match with Java is good simply because a lot of people are using Java) or by looking at some code
that solves problems you are likely to have and seeing how natural the mapping is.
 Availability of libraries. Some would argue that this is the same as the point above, but I don't
think so. If there's a library that solves your problem well, you'll put up with some ugly calling
conventions or hassle in the language.
 Efficiency. Languages aren't fast - compilers are efficient. Look at the efficiency of compilers or
interpreters for your language. Be aware that interpreted code will run an order of magnitude slower
than compiled code as a rule of thumb.
 Expressiveness. The number of lines of code you create per hour is not a strong function of
language, so favor languages that are expressive or powerful
 Project-size. Do you want to be programming in the large or programming in the small? Choose a
language that supports your use case.
 Tool support. Popularity usually buys tool support (and some languages are easier to write tools
for). If you are a tool-oriented user, choose a language with good tool support.

3.0 : Program development

3.1 Definition

Software development is the process of computer programming, documenting, testing, and bug fixing
involved in creating and maintaining a software product.
Software development is a process of writing and maintaining the source code, but in a broader sense, it
includes all that is involved between the conception of the desired software through to the final
manifestation of the software, sometimes in a planned and structured process. Therefore, software
development may include research, new development, prototyping, modification, reuse, re-engineering,
maintenance, or any other activities that result in software products.

3.2 Program specification


Precise statement of the effects the individual program is intended to achieve.

3.2 Program development cycle

Program Development Life Cycle (PDLC) is a systematic way of developing quality software. It
provides an organized plan for breaking down the task of program development into manageable
chunks, each of which must be successfully completed before moving on to the next phase.

The following steps are used in sequence for developing an efficient program:
 Specifying the problem statement
 Designing an algorithm

Page 10
 Coding
 Debugging
 Testing and Validating
 Documentation and Maintenance.
Specifying the Problem:
The Problem which has to be implemented into a program must be thoroughly understood
before the program is written. Problem must be analyzed to determine the input and output
requirements of the program. A problem is created with these specifications.

Designing an Algorithm:
With the problem statement obtained in the previous step, various methods available for
obtaining the required solution are analyzed and the best suitable method is designed into algorithm.
To improve clarity and understandability of the program flow charts are drawn using the
algorithms.

Coding:
The actual program is written in the required programming language with the help of
information depicted in flow charts and algorithms.

Debugging:
There is a possibility of occurrence of errors in programs. These errors must be removed to
ensure proper working of programs. Hence error check is made. This process is known as
“Debugging”.
Types of errors that may occur in the program are:

 Syntactic Errors: These errors occur due to the usage of wrong syntax for the statements.
Syntax means rules of writing the program.
Example: x=z*/b;
There is syntax error in this statement. The rules of binary operators state that there cannot be more
than one operator between two operands.
 Runtime Errors: These Errors are determined at the execution time of the program.
Example: Divide by zero
Range out of bounds
Square root of a negative number
 Logical Errors: These Errors occur due to incorrect usage of the instruction in the program.
These errors are neither displayed during compilation or execution nor cause any obstruction to
the program execution. They only cause incorrect outputs. Logical Errors are determined by
analyzing the outputs for different possible inputs that can be applied to the program. By this
way the program is validated.
Testing and Validating:
Testing and Validation is performed to check whether the program is producing correct results
or not for different values of input.

Documentation and Maintenance:


Documentation is the process of collecting, organizing and maintaining, in written the complete
information of the program for future references. Maintenance is the process of upgrading the program
according to the changing requirements.

Page 11
For writing up the instructions as a program in the way that a computer can understand, we use
programming languages.

 Problem Definition- In this phase, we define the problem statement and we decide the boundaries
of the problem. In this phase we need to understand the problem statement, what is our requirement,
what should be the output of the problem solution. These are defined in this first phase of the
program development life cycle.
 Problem Analysis- In phase 2, we determine the requirements like variables, functions, etc. to
solve the problem. That means we gather the required resources to solve the problem defined in the
problem definition phase. We also determine the bounds of the solution.
 Algorithm Development- During this phase, we develop a step by step procedure to solve the
problem using the specification given in the previous phase. This phase is very important for
program development. That means we write the solution in step by step statements.
 Coding & Documentation- This phase uses a programming language to write or implement actual
programming instructions for the steps defined in the previous phase. In this phase, we construct
actual program. That means we write the program to solve the given problem using programming
languages like C, C++, Java etc.
 Testing & Debugging- During this phase, we check whether the code written in previous step is
solving the specified problem or not. That means we test the program whether it is solving the
problem for various input data values or not. We also test that whether it is providing the desired
output or not.
 Maintenance- During this phase, the program is actively used by the users. If any enhancements
found in this phase, all the phases are to be repeated again to make the enhancements. That means
in this phase, the solution (program) is used by the end user. If the user encounters any problem or
wants any enhancement, then we need to repeat all the phases from the starting, so that the
encountered problem is solved or enhancement is added.

Page 12
Page 13

You might also like