Programming Methodologies, Lecture notes of Programming Methodologies

Lecture notes for university students

Typology: Lecture notes

2018/2019
On special offer
30 Points
Discount

Limited-time offer


Uploaded on 11/22/2019

peter-limo
peter-limo 🇰🇪

3

(2)

8 documents

1 / 152

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
P.O. Box 342-01000 Thika
Web: www.mku.ac.ke
DEPARTMENT OF INFORMATION
TECHNOLOGY
BACHELOR OF BUSINESS INFORMATION TECHNOLOGY (BBIT)
COURSE CODE: BIT 2201
COURSE TITLE: COMPUTER PROGRAMMING METHODOLOGY
(INTRO. TO C PROGRAMMING)
INSTRUCTIONAL MANUAL FOR BBIT DISTANCE LEARNING
PREPARED BY MERCY MAINA
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54
pf55
pf56
pf57
pf58
pf59
pf5a
pf5b
pf5c
pf5d
pf5e
pf5f
pf60
pf61
pf62
pf63
pf64
Discount

On special offer

Partial preview of the text

Download Programming Methodologies and more Lecture notes Programming Methodologies in PDF only on Docsity!

P.O. Box 342-01000 Thika

Email: [email protected]

Web: www.mku.ac.ke

DEPARTMENT OF INFORMATION

TECHNOLOGY

BACHELOR OF BUSINESS INFORMATION TECHNOLOGY (BBIT)

COURSE CODE: BIT 2201

COURSE TITLE: COMPUTER PROGRAMMING METHODOLOGY

(INTRO. TO C PROGRAMMING)

INSTRUCTIONAL MANUAL FOR BBIT – DISTANCE LEARNING

PREPARED BY MERCY MAINA

TABLE OF CONTENT

  • BIT 2201: Computer Programming Methodology Page
    • CHAPTER ONE Page
    • Overview of Computer Programming Methodology
      • 1.1 What is Computer Programming Methodology
      • 1.2. Problem Solving with Computer;
      • 1.2.1. Problem Algorithm
      • 1.3. Flow Charts
      • 1.3.1. Elements of a Flowchart.
      • 1.4. Pseudo code
      • 1.5. Program Control Structures
      • 1.6. Programming Methods
      • 1.6.1 Top-down and Bottom-up methodology..........................................................................................
      • 1.6.2. Structured programming Method....................................................................................................
      • 1.7. Programming Aims
      • 1.8. Programming Paradigms....................................................................................................................
      • 1.8.1. Imperative programming;
      • 1.8.2. Object-oriented (OO) programming:
      • 1.8.3 Functional Programming:
      • 1.8.4. Logic Programming
      • Chapter Review Questions........................................................................................................................
    • CHAPTER TWO
    • Programming Languages
      • 2.1. What is a Programming Language
      • 2.2. Types of Programming Languages
      • 2.2.1. Machine language
  • BIT 2201: Computer Programming Methodology Page - 2.2.2. Assembly Language - 2.2.3. High level language - 2.3. High Level language Translation - 2.3.1. Compiler - 2.3.2. Interpreter: - 2.4. Computer Program Compilation Process........................................................................................... - 2.5. Evaluating Languages - 2.6. The Programming Language Generations - Chapter Review Questions........................................................................................................................
    • CHAPTER THREE
    • Introduction to C Programming
      • 3.2. Characteristics of C............................................................................................................................
      • 3.3. Executing a C program
      • 3.4 Compiling a C Program
      • 3.5. C Program File naming convention
      • 3.6. Errors
      • 3.6.1. Syntax Error
      • 3.6.2. Logical or Intention Error
      • 3.7. C Libraries
      • 3.8. C Program Structure
      • 3.9 C Program format
      • 1.9.1. Documentation Section
      • 1.9.2. Link Section
      • 1.9.3. Definition Section
      • 1.9.4. Declaration Section
  • BIT 2201: Computer Programming Methodology Page - 3.9.5 main() Function Section.................................................................................................................. - 3.9.6 Sub-program section - 3.10. Breaking out early............................................................................................................................ - Chapter Review Questions........................................................................................................................
    • CHAPTER FOUR
    • C Programming: - Constants, Variables and Data Types
      • 4.1. C Program Character set
      • 4.2. C Program Tokens
      • 4.4.1. Integer Constants
      • 4.4.2. Real Constants
      • 4.4.3. Single Character Constants
      • 4.4.4. String Constants
      • 4.4.5. Backslash Character constants
      • 4.5 Variables
      • 4.6. Data types
      • 4.6.1. Primary data types
      • 4.6.2. Integer data types
      • 4.6.3. Floating point types
      • 4.6.4. Void types
      • 4.6.5. Character type
      • 4.7. Declaring variables
      • 4.7.1. User defined Type Declaration
      • 4.8. Declaration of Storage Class..............................................................................................................
      • 4.9. Assigning Values to Variables
      • 4.9.1. Using the „=‟ operator
  • BIT 2201: Computer Programming Methodology Page - 4.9.2. Reading data from the keyboard (scanf()) - 4.10. Defining Symbolic Constants
    • CHAPTER FIVE
    • C- Programming:- Operators and Expressions
      • 5.1. Overview.......................................................................................................................................
      • 5.2. Arithmetic Operators
      • 5.3. Relational Operators and Logical operators
      • 5.3.1. Relative precedence of the Relational Operators
      • 5.4 Assignment operators
      • 5.5. Increment and Decrement Operators
      • 5.6. Conditional Operators
      • 5.7. Special Operators
      • 5.8. Arithmetic Expressions
      • 5.9. Precedence of Arithmetic Operators
      • Chapter review questions
    • CHAPTER SIX
    • C Programming:- Managing Input and Output Operations
      • 6.1. Introduction...................................................................................................................................
      • 6.2. Reading a Character.
      • 6.2.1. Character test functions.
      • 6.3. Writing a character........................................................................................................................
      • 6.4. Formatted Input............................................................................................................................
      • 6.4.1. Inputting Integer Numbers
      • 6.4.2. Inputting Real (floating point numbers)
      • 6.5. Inputting Character Strings
  • BIT 2201: Computer Programming Methodology Page - 6.6. Reading mixed Data types - 6.7. Detecting Errors in Input - 6.8. Commonly used Scanf formats - 6.9. Formatted Out put - 6.9.1. Formating Integer and Real numbers output - 6.10. Commonly used Printf Formats - 6.11. Enhancing readability of Output. - Chapter Review Questions........................................................................................................................
    • CHAPTER SEVEN
    • C Programming:- Decision Making and Branching
      • 7.1. Introduction...................................................................................................................................
      • 7.2. Decision making with if statement
      • 7.2.1. Simple if statement
      • 7.2.2. The If …… Else Statement
      • 7.2.3. Nesting if …. Else Statements
      • 7.2.4. The Else If Ladder
      • 7.3. Rules for indentation in the if…. else statements
      • 7.4. Switch Statement
      • 7.4.1. Rules for Switch........................................................................................................................
      • Chapter Review questions
    • CHAPTER EIGHT
    • C Programming:- Decision Making and Looping
      • 8.1 Introduction.........................................................................................................................................
      • 8.2. Classification of Loop Control Structures;
      • 8.3. The While Statement
  • BIT 2201: Computer Programming Methodology Page - 8.4. The Do Statement - 8.5. The For Statement......................................................................................................................... - 8.6. Nesting of for loops - 8.7. Selecting a Loop - 8.8. Jumping out of a Loop - 8.9 Structured programming - Chapter Review Questions........................................................................................................................
    • CHAPTER NINE..........................................................................................................................................
    • C- Programming: Arrays
      • 9.1 Introduction.........................................................................................................................................
      • 9.2. Data structures in C............................................................................................................................
      • 9.3. One-Dimensional Arrays
      • 9.4. Declaration of One-Dimensional Arrays.
      • 9.5. Initialization of One-Dimensional Arrays.
      • 9.5.1. Compile time initialization.
      • 9.5.2. Run Time Initialization
      • 9.6. Searching and Sorting using Arrays
      • 9.7. Two – Dimensional Arrays
      • 9.7. Initializing Two-Dimension Arrays
      • 9.8. Character Arrays and Strings
      • 9.9. Declaring and Initializing String Valuables.
      • 9.10. Terminating with Null Character.
      • 9.11. Reading Strings from Terminals
      • 9.13. Writing Strings to Screen.
      • 9.14. String Handling Functions
  • BIT 2201: Computer Programming Methodology Page - 9.14.1. Strupr() function - 9.14.2. Strcat() function - 9.14.3. Strcmp() Function - 9.14.4. Strcpy() Function - Chapter Review Questions......................................................................................................................
    • CHAPTER TEN
    • C- Programming:- User Defined Functions
      • 10.1. Introduction....................................................................................................................................
      • 10.2. Need for user-defined functions
      • 10.3. Modular programming;
      • 10.4. A Multi-Function Program.
      • 10.5. Elements of User-Defined Functions
      • 10.6. Definition of Functions
      • 10.6.1 Function header............................................................................................................................
      • 10.6.2. Function Body
      • 10.7. Return values and their types
      • 10.8. Function call
      • 10.9. Function Declaration......................................................................................................................
      • 10.10. Categories of Functions
      • 10.11. Nesting of Functions
      • 10.12. Recursion
      • Chapter Review Questions......................................................................................................................
    • CHAPTER ELEVEN..................................................................................................................................
    • C- Programming: Structures and Pointers
      • 11.2. Arrays vs Structures
  • BIT 2201: Computer Programming Methodology Page - 11.3. Declaring Structure Variables - 11.4. Accessing Structure Members - 11.5 Structure initialization..................................................................................................................... - 11.7. Pointers - 11.7.1. Accessing the Address of a Variable - 11.7.2. Declaring Pointer Variables. - 11.7.3. Pointer declaration styles - 11.7.4. Initialization of Pointer variables - 11.7.5. Accessing a Variable through its pointer - 11.8. Pointers and Arrays........................................................................................................................ - Chapter Review Questions......................................................................................................................
    • CHAPTER TWELVE
    • C- Programming: Managing Files in C.......................................................................................................
      • 12.1. Introduction....................................................................................................................................
      • 12.2. Defining and Opening aFile.
      • 12.3. Closing a File
      • 12.4. Input / Output Operations on Files
      • 12.5. The getw and putw Functions
      • 12.6. The fprintf and fscanf Functions
      • 12.7. Error Handling during I/O Operations
      • Chapter Review Questions......................................................................................................................
    • Sample Examination Papers

BIT 2201: Computer Programming Methodology Page 11

or doing integrals in a non-numerical fashion is relatively limited. The Algorithm can also be represented using Pseudo-code Step 3. Code the Program: The steps in an algorithm, translated into a series of instructions to the computer, comprise the computer program. There are many languages in which computer programs can be coded, each with its own syntax, vocabulary, and special features. Step 4. Debug the Program: Most programs of any length don't work properly the first time they are run and must therefore be debugged." Often, during the debugging phase, errors and ambiguities in the original statement of the problem reveal themselves, calling for basic revisions in the solution algorithm. Step 5. Run the Program: After the program has been fully debugged you run it, possibly using many sets of input data. This step may take anywhere from a few seconds to many hours depending on the complexity of the problem and the speed of the computer. Step 6. Analyze the Results: Often the output from a computer program requires considerable further analysis. In some cases, even though the program worked perfectly, you may find that you solved the “wrong" problem. There is an acronym well known to computer users: GIGO, which stands for “garbage in, garbage out."

1.2.1. Problem Algorithm An Algorithm is a logical sequence of discrete steps that describe a complete solution to a given problem in a finite amount of time independently of the software or hardware of the computer. It is the set of rules that define how a particular problem can be solved in finite number of steps. Algorithms are very essential as they instructs the computer what specific steps it needs to perform to carry out a particular task or solve a problem. Every algorithm should have the following five characteristics: Input, Output, Definiteness, Effectiveness and Termination. An Algorithm has the following properties;  It must be precise and unambiguous  It must give the correct solution in all cases  It must eventually end.

Efficiency and Analysis of the Algorithm The efficiency of an Algorithm means how fast it can produce the correct results for the given problem. The Algorithm efficiency depends upon its time complexity and space complexity. The complexity of an algorithm is a function that provides the running time and space for data, depending on the size provided by us. Two important factors for judging the complexity of an Algorithm are; space complexity which refers to the amount of memory required by the algorithm for it execution and generation of the final output and time Complexity which refers to the amount of computer time required by an algorithm for its execution, which includes both the compile time and run time. The compile time of an algorithm does not depend on the instance characteristics of the algorithm. The run time of an algorithm is estimated by

BIT 2201: Computer Programming Methodology Page 12

determining the number of various operation, such as addition, subtraction, multiplication, division, load and store executed by it.

The analysis of an algorithm determines the amount of resources, such as time and space required by it for its execution. Generally, the algorithms are formulated to work with the inputs or arbitrary length. Algorithm analysis provides theoretical estimates required by an algorithm to solve a problem. The steps of an Algorithm, they can be presented using Flow charts and pseudo-codes.

1.3. Flow Charts A flow chart is a traditional means of showing in diagrammatic form, the sequence of steps to be undertaken in solving a problem. Flowcharts or flow diagrams are important tools in writing a computer program. A flowchart allows you to plan the sequence of steps in a program before writing it. The flowchart serves as a visual representation which many programmers find indispensable in planning any program of at least moderate complexity.

1.3.1. Elements of a Flowchart. A flowchart consists of a set of boxes, the shapes of which indicate specific operations. The separate boxes are connected with arrows to show the sequences in which the various operations are performed. We use these standard symbols:

Shape Name Description Rectangle Process symbol: Used to represent any kind of processing activity. Details are written in the box

Diamond: The decision Symbol: Used where a decision has to be made in selecting the subsequent path to be followed.

Used to show the flow/ path of ma sequence of symbols.  Vertical line without arrow head are assumes t flow top to bottom.  Horizontal lines without arrow heads are assumed to flow left to right.  Every operation box must have at least one incoming or outgoing arrow. Any arrow leaving a decision box must be labeled with the decision result which will cause that path to be followed.

Calculate wages

Is the transaction a credit?

BIT 2201: Computer Programming Methodology Page 14

Pseudo code assumes that programming procedures no matter how complex may be reduced to a combination of controlled sequences, selection, or repetition of basic operations. This gives rise to the control structures found in pseudo-code.

1.5. Program Control Structures Control Structure Pseudo code Flow Chart Sequence: In the absence of selection, or repetition, program statements are executed in the sequence in which the appear in the program

1 st^ Instruction

2 nd^ Instruction

3 rd^ Instruction

Selection Part of decision making and allows alternative actions to be taken according to the conditions that exist at particular stages in program execution

IF

condition THEN actions ELSE actions ENDIF Or

CASE

a). Actions b). Actions c). Actions d). Actions ENDCASE

Repetition also called “looping” There are many programming problems in which the same sequence of statements needs to be performed again and again for a definite or indefinite number of times

WHILE

condition DO Actions ENDWHILE

REPEAT

actions UNTIL condition

No Yes

No

Yes

a b c

No

BIT 2201: Computer Programming Methodology Page 15

1.6. Programming Methods 1.6.1 Top-down and Bottom-up methodology A top-down approach (is also known as step-wise design) is essentially the breaking down of a system to gain insight into its compositional sub-systems. In a top-down approach an overview of the system is formulated, specifying but not detailing any first-level subsystems. Each subsystem is then refined in yet greater detail, sometimes in many additional subsystem levels, until the entire specification is reduced to base elements. A top-down model is often specified with the assistance of "black boxes", these make it easier to manipulate. However, black boxes may fail to elucidate elementary mechanisms or be detailed enough to realistically validate the model.

Top - down-design starts with a description of the overall system and usually consists of a hierarchical structure which contains more detailed descriptions of the system at each lower level. The lower level design details continue until further subdivision is no longer possible, i.e., until the system is described in terms of its "atomic" parts. This method involves a hierarchical or tree-like structure for a system as illustrated by the following diagram:

At the top level, we have that part of the system which deals with the overall system; a kind of system overview or main top-level module.

Top down programming method process

  1. Define exactly what data the program will get and what it has to do with them.
  2. If the task is simple enough, write the program code.
  3. Otherwise, split the task into smaller parts and define exactly the duty of each part and interface to the rest of the program.
  4. Repeat the steps 1–4 separately for each subtask.

Advantages of the Top-Down Design Method

  • It is easier to comprehend the solution of a smaller and less complicated problem than to grasp the solution of a large and complex problem. Separating the low level work from the higher level

BIT 2201: Computer Programming Methodology Page 17

1.6.2. Structured programming Method Structured programming is a special type of procedural programming. It provides additional tools to manage the problems that larger programs were creating. Structured programming requires that programmers break program structure into small pieces of code that are easily understood. It also frowns upon the use of global variables and instead uses variables local to each subroutine.

Structured programming was developed during the 1950s after Edgar Dijkstra‟s insightful comments into the harmful nature of the GO TO statement. Dijkstra and others subsequently created a set of acceptable structures in programming that would enable development without GO TO statements. These structures produced programs that were easier to read by humans, easier to debug and easier to test. These structures have become some of the founding principles of modern programming methods.

Although the principles of structured programming have had a profound effect on the programming world, it was not until the 1970s that an actual language was created for teaching structured programming. Pascal was developed especially for this purpose, though it is much derided as a toy language, and appears to have never been used in commercial development. It appears that existing languages such as COBOL and FORTRAN were changed to accommodate Dijkstra‟s structures, or that programming included these structures through more indirect methods.

Later generation languages such as C are fully-fledged structured programming languages; these are from the third generation and procedural, in that they are both written and executed step-by-step. C, in its turn, has formed the foundation for the object-oriented language C++.

The three structures allowed in structured programming are sequence , selection , and iteration. Structures are also thought of in terms of substitution and combination, i.e. structures can be substituted or combined with other structures as long as the result equals a sequential structure. Structured programming also pays attention to design and testing with emphasis on a top-down approach. The top-down approach uses modularity as a means to ensure that the program is both legible and manageable, and also that these modules can be tested as they are developed. This is beneficial as it ensures that all modules should be tested and that bugs can be found in the modules that have most recently been added or altered.

Structured programming also places emphasis on program documentation, which can be in the form of a chart or the structured coding/listing. This documentation allows for subsequent updating of modules, making these modules easier to locate in the program. Modularity also ensures greater opportunity for re- use of modules during development.

BIT 2201: Computer Programming Methodology Page 18

1.7. Programming Aims Good programming principles and practice aim at producing a program with the following characteristics;

  • Reliability: the program can be depended upon always to do what it is supposed to do
  • Maintainability: the program will be easy to change or modify when the need arises
  • Portability: the program will be transferable to a different computer with a minimum modification.
  • Readability: the program will be easy for a programmer to read and understand.
  • Performance: the program causes the tasks to be done quickly and efficiently.
  • Storage saving: the program is not allowed to be unnecessarily long

1.8. Programming Paradigms A programming paradigm is a pattern of problem solving thought that underlies a particular genre of programs and languages. Four distinct and fundamental programming paradigms have evolved over the last three decades;

  • Imperative programming;
  • Object-oriented programming
  • Functional programming
  • Logic Programming;

1.8.1. Imperative programming; The oldest and the most well-developed, it emerged with the first computers in the 1940s and its elements directly mirror the architectural characteristics of modern computers as well. The program and its variables are stored together and the program contains a series of commands that perform calculations, assign values to variables, retrieve input, produce out, or redirect control elsewhere in the series.

Procedural abstraction is an essential building block for imperative programming as are assignments, loops, sequences, conditional statements and exception handling. Imperative languages also support variable declaration and expressions. The predominant imperative programming languages include Cobol, Fortran, C Ada and Perl.

Commands are normally executed in the order they appear in the memory, while conditional and unconditional branching statements can interrupt this normal flow of execution. Originally the commands included assignment statements, conditional statements and branching statements. The assignments statements provided the ability to dynamically update the value stored in the memory location, while conditional and branching statements could be combined to allow a set of statements to be either skipped or repeatedly executed. The main features of Imperative programming includes;

  • Control structures;
  • Input/output