ce 2207 numerical analysis and computer programming...numerical analysis and computer programming...

35
PINGKI DATTA, LECTURER, DEPARTMENT OF CIVIL ENGINEERING, KUET. 1 CE 2207 Numerical Analysis and Computer Programming

Upload: others

Post on 21-Mar-2021

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CE 2207 Numerical Analysis and Computer Programming...Numerical Analysis and Computer Programming PINGKI DATTA, LECTURER, DEPARTMENT OF CIVIL ENGINEERING, KUET. 2 Course outline: Computer

PINGKI DATTA, LECTURER, DEPARTMENT OF CIVIL ENGINEERING, KUET.

1

CE 2207 Numerical Analysis and

Computer Programming

Page 2: CE 2207 Numerical Analysis and Computer Programming...Numerical Analysis and Computer Programming PINGKI DATTA, LECTURER, DEPARTMENT OF CIVIL ENGINEERING, KUET. 2 Course outline: Computer

PINGKI DATTA, LECTURER, DEPARTMENT OF CIVIL ENGINEERING, KUET.

2

Course outline:Computer Programming: Introduction to Computer Programming Study of C language Computer Applications in Civil Engineering ProblemsReferences:• Programming in ANSI C

………..E. Balagurusamy• The C Programming Language 2nd Edition

………..Brian W. Kernighan, Dennis M. Ritchie

Page 3: CE 2207 Numerical Analysis and Computer Programming...Numerical Analysis and Computer Programming PINGKI DATTA, LECTURER, DEPARTMENT OF CIVIL ENGINEERING, KUET. 2 Course outline: Computer

PINGKI DATTA, LECTURER, DEPARTMENT OF CIVIL ENGINEERING, KUET.

3

LECTURE-01Introduction to

Computer Programming

Page 4: CE 2207 Numerical Analysis and Computer Programming...Numerical Analysis and Computer Programming PINGKI DATTA, LECTURER, DEPARTMENT OF CIVIL ENGINEERING, KUET. 2 Course outline: Computer

PINGKI DATTA, LECTURER, DEPARTMENT OF CIVIL ENGINEERING, KUET.

4

What is Computer programming?Computer programming is a process that leads

from an original formulation of a computingproblem to executable computer programs.

The purpose of programming is to find a sequenceof instructions that will automate performing aspecific task or solving a given problem.

Important tasks in computer programming includetesting, debugging, and maintaining the sourcecode, implementation of the build system, andmanagement of derived artifacts such as machinecode of computer programs.

Page 5: CE 2207 Numerical Analysis and Computer Programming...Numerical Analysis and Computer Programming PINGKI DATTA, LECTURER, DEPARTMENT OF CIVIL ENGINEERING, KUET. 2 Course outline: Computer

PINGKI DATTA, LECTURER, DEPARTMENT OF CIVIL ENGINEERING, KUET.

5

• DebuggingDebugging is a very important task in the softwaredevelopment process since having defects in aprogram can have significant consequences for itsusers. Some languages are more prone to somekinds of faults because their specification does notrequire compilers to perform as much checking asother languages.

Page 6: CE 2207 Numerical Analysis and Computer Programming...Numerical Analysis and Computer Programming PINGKI DATTA, LECTURER, DEPARTMENT OF CIVIL ENGINEERING, KUET. 2 Course outline: Computer

PINGKI DATTA, LECTURER, DEPARTMENT OF CIVIL ENGINEERING, KUET.

6

• AlgorithmAn algorithm is a well-defined procedure that allows acomputer to solve a problem. Another way todescribe an algorithm is a sequence of unambiguousinstructions.

• FlowchartA flowchart is a type of diagram that represents analgorithm, workflow or process, showing the steps asboxes of various kinds, and their order by connectingthem with arrows. This diagrammatic representationillustrates a solution model to a given problem.

Page 7: CE 2207 Numerical Analysis and Computer Programming...Numerical Analysis and Computer Programming PINGKI DATTA, LECTURER, DEPARTMENT OF CIVIL ENGINEERING, KUET. 2 Course outline: Computer

PINGKI DATTA, LECTURER, DEPARTMENT OF CIVIL ENGINEERING, KUET.

7

Languages are a means of communication.Normally people interact with each otherthrough a language. On the same pattern,communication with computers is carried outthrough a language. This language is understoodboth by the user and the machine. Just as everylanguage like English, has its own grammaticalrules; every computer language is also boundedby rules known as syntax of that language. Theuser is bound by that syntax whilecommunicating with the computer system.

Page 8: CE 2207 Numerical Analysis and Computer Programming...Numerical Analysis and Computer Programming PINGKI DATTA, LECTURER, DEPARTMENT OF CIVIL ENGINEERING, KUET. 2 Course outline: Computer

PINGKI DATTA, LECTURER, DEPARTMENT OF CIVIL ENGINEERING, KUET.

8

Languages form an approximate spectrum from"low-level" to "high-level"; "low-level" languagesare typically more machine-oriented and faster toexecute, whereas "high-level" languages are moreabstract and easier to use but execute lessquickly. It is usually easier to code in "high-level"languages than in "low-level" ones.

Page 9: CE 2207 Numerical Analysis and Computer Programming...Numerical Analysis and Computer Programming PINGKI DATTA, LECTURER, DEPARTMENT OF CIVIL ENGINEERING, KUET. 2 Course outline: Computer

PINGKI DATTA, LECTURER, DEPARTMENT OF CIVIL ENGINEERING, KUET.

9

Computer languages are broadlyclassified as follows:Low Level Language: The term low level highlightsthe fact that it is closer to a language which themachine understands.The low level languages are classified as:Machine Language: This is the language (in theform of 0’s and 1’s, called binary numbers)understood directly by the computer. It is machinedependent. It is difficult to learn and even moredifficult to write programs.

Page 10: CE 2207 Numerical Analysis and Computer Programming...Numerical Analysis and Computer Programming PINGKI DATTA, LECTURER, DEPARTMENT OF CIVIL ENGINEERING, KUET. 2 Course outline: Computer

PINGKI DATTA, LECTURER, DEPARTMENT OF CIVIL ENGINEERING, KUET.

10

Assembly Language:This is the language where the machine codescomprising of 0’sand 1’s are substituted by symboliccodes to improve their understanding. It is the firststep to improve programming structure. It is simplerand less time consuming than machine levelprogramming, it is easier to locate and correcterrors in assembly language than in machinelanguage programs. It is also machine dependent.Programmers must have knowledge of the machineon which the program will run.

Page 11: CE 2207 Numerical Analysis and Computer Programming...Numerical Analysis and Computer Programming PINGKI DATTA, LECTURER, DEPARTMENT OF CIVIL ENGINEERING, KUET. 2 Course outline: Computer

PINGKI DATTA, LECTURER, DEPARTMENT OF CIVIL ENGINEERING, KUET.

11

High Level Language:A high-level programming language is a programminglanguage with strong abstraction from the details ofthe computer. In comparison to low-level programminglanguages, it may use natural language elements, beeasier to use, or may automate (or even hide entirely)significant areas of computing systems (e.g. memorymanagement), making the process of developing aprogram simpler and more understandable relative to alower-level language.

Page 12: CE 2207 Numerical Analysis and Computer Programming...Numerical Analysis and Computer Programming PINGKI DATTA, LECTURER, DEPARTMENT OF CIVIL ENGINEERING, KUET. 2 Course outline: Computer

PINGKI DATTA, LECTURER, DEPARTMENT OF CIVIL ENGINEERING, KUET.

12

Examples of High level languages:BASIC (Beginners All Purpose Symbolic Instruction Code): Itis widely used, easy to learn general purpose language.Mainly used in microcomputers in earlier days.COBOL (Common Business Oriented language):A standardized language used for commercial applications.FORTRAN (Formula Translation): Developed for solvingmathematical and scientific problems. One of the mostpopular languages among scientific community.C: Structured Programming Language used for all purposesuch as scientific application, commercial application,developing games etc.C++: Popular object oriented programming language, used forgeneral purpose.

Page 13: CE 2207 Numerical Analysis and Computer Programming...Numerical Analysis and Computer Programming PINGKI DATTA, LECTURER, DEPARTMENT OF CIVIL ENGINEERING, KUET. 2 Course outline: Computer

PINGKI DATTA, LECTURER, DEPARTMENT OF CIVIL ENGINEERING, KUET.

13

PROGRAMMING LANGUAGE TRANSLATORS:-To make the machine understand the instructionsprovided by both high level and low levellanguages, programming language instructors areused. They transform the instruction prepared byprogrammers into a form which can be interpreted& executed by the computer.

Page 14: CE 2207 Numerical Analysis and Computer Programming...Numerical Analysis and Computer Programming PINGKI DATTA, LECTURER, DEPARTMENT OF CIVIL ENGINEERING, KUET. 2 Course outline: Computer

PINGKI DATTA, LECTURER, DEPARTMENT OF CIVIL ENGINEERING, KUET.

14

Following are the various tools toachieve this purpose:-

Compiler:The software that reads a program written inhigh level language and translates it into anequivalent program in machine language iscalled as compiler. The program written by theprogrammer in high level language is calledsource program or source code and the programgenerated by the compiler after translation iscalled as object program.

Page 15: CE 2207 Numerical Analysis and Computer Programming...Numerical Analysis and Computer Programming PINGKI DATTA, LECTURER, DEPARTMENT OF CIVIL ENGINEERING, KUET. 2 Course outline: Computer

PINGKI DATTA, LECTURER, DEPARTMENT OF CIVIL ENGINEERING, KUET.

15

Interpreter:It also executes instructions written in a high levellanguage. Both complier & interpreter have thesame goal i.e. to convert high level language intobinary instructions, but their method of executionis different. The complier converts the entiresource code into machine level program, whilethe interpreter takes 1 statement, translates it,executes it & then again takes the next statement.

Page 16: CE 2207 Numerical Analysis and Computer Programming...Numerical Analysis and Computer Programming PINGKI DATTA, LECTURER, DEPARTMENT OF CIVIL ENGINEERING, KUET. 2 Course outline: Computer

PINGKI DATTA, LECTURER, DEPARTMENT OF CIVIL ENGINEERING, KUET.

16

Assembler:The software that reads a program written inassembly language and translates it into anequivalent program in machine language is calledas assembler.

Linker:A linker or link editor is a computer program thattakes one or more object files generated by acompiler and combines them into a singleexecutable file, library file, or another object file.

Page 17: CE 2207 Numerical Analysis and Computer Programming...Numerical Analysis and Computer Programming PINGKI DATTA, LECTURER, DEPARTMENT OF CIVIL ENGINEERING, KUET. 2 Course outline: Computer

PINGKI DATTA, LECTURER, DEPARTMENT OF CIVIL ENGINEERING, KUET.

17

Integrated Development Environments(IDE)

The process of editing, compiling, running, anddebugging programs is often managed by asingle integrated application known as anIntegrated Development Environment, or IDE.Under Windows, Microsoft Visual Studio is agood example of a popular IDE.

Page 18: CE 2207 Numerical Analysis and Computer Programming...Numerical Analysis and Computer Programming PINGKI DATTA, LECTURER, DEPARTMENT OF CIVIL ENGINEERING, KUET. 2 Course outline: Computer

PINGKI DATTA, LECTURER, DEPARTMENT OF CIVIL ENGINEERING, KUET.

18

• The C programming language is a structureoriented programming language, developed atBell Laboratories in 1972 by Dennis Ritchie.

• C programming language features werederived from an earlier language called “B”(Basic Combined Programming Language –BCPL)

• C language was invented for implementingUNIX operating system.

History of C:

Page 19: CE 2207 Numerical Analysis and Computer Programming...Numerical Analysis and Computer Programming PINGKI DATTA, LECTURER, DEPARTMENT OF CIVIL ENGINEERING, KUET. 2 Course outline: Computer

PINGKI DATTA, LECTURER, DEPARTMENT OF CIVIL ENGINEERING, KUET.

19

• In 1978, Dennis Ritchie and Brian Kernighan publishedthe first edition “The C Programming Language” and iscommonly known as K&R C.

• In 1983, the American National Standards Institute(ANSI) established a committee to provide a modern,comprehensive definition of C. The resultingdefinition, the ANSI standard, or “ANSI C”, wascompleted late 1988.

• Many of C’s ideas & principles were derived from theearlier language B, thereby naming this new language“C”.

History of C (Cont.):

Page 20: CE 2207 Numerical Analysis and Computer Programming...Numerical Analysis and Computer Programming PINGKI DATTA, LECTURER, DEPARTMENT OF CIVIL ENGINEERING, KUET. 2 Course outline: Computer

PINGKI DATTA, LECTURER, DEPARTMENT OF CIVIL ENGINEERING, KUET.

20

Page 21: CE 2207 Numerical Analysis and Computer Programming...Numerical Analysis and Computer Programming PINGKI DATTA, LECTURER, DEPARTMENT OF CIVIL ENGINEERING, KUET. 2 Course outline: Computer

PINGKI DATTA, LECTURER, DEPARTMENT OF CIVIL ENGINEERING, KUET.

21

Basic Components of C :i. Preprocessor Commandsii. Functionsiii. Variablesiv. Statements & Expressionsv. Comments

C has now become a widely used professionallanguage for various reasons:• Easy to learn• Structured language• It produces efficient programs• It can handle low-level activities• It can be compiled on a variety of computer platforms

Page 22: CE 2207 Numerical Analysis and Computer Programming...Numerical Analysis and Computer Programming PINGKI DATTA, LECTURER, DEPARTMENT OF CIVIL ENGINEERING, KUET. 2 Course outline: Computer

PINGKI DATTA, LECTURER, DEPARTMENT OF CIVIL ENGINEERING, KUET.

22

Why is C PopularØ It is reliable, simple and easy to use.Ø C is a small, block-structured programming language.Ø C is a portable language, which means that Cprograms written on one system can be run on othersystems with little or no modification.Ø C has one of the largest assortments of operators,such as those used for calculations and datacomparisons.Ø Although the programmer has more freedom withdata storage, the languages do not check data typeaccuracy for the programmer.

Page 23: CE 2207 Numerical Analysis and Computer Programming...Numerical Analysis and Computer Programming PINGKI DATTA, LECTURER, DEPARTMENT OF CIVIL ENGINEERING, KUET. 2 Course outline: Computer

PINGKI DATTA, LECTURER, DEPARTMENT OF CIVIL ENGINEERING, KUET.

23

Why should we use C?C was initially used for system development work. Someexamples of the use of C might be:Operating Systems Language Compilers Assemblers Text Editors Print SpoolersNetwork DriversModern ProgramsDatabases Language Interpreters

Page 24: CE 2207 Numerical Analysis and Computer Programming...Numerical Analysis and Computer Programming PINGKI DATTA, LECTURER, DEPARTMENT OF CIVIL ENGINEERING, KUET. 2 Course outline: Computer

PINGKI DATTA, LECTURER, DEPARTMENT OF CIVIL ENGINEERING, KUET.

24

Characteristics of C Language:Ø Middle level language.Ø Small size – has only 32 keywordsØ Extensive use of function calls- enables theend user to add their own functions to the Clibrary.Ø Supports loose typing – a character can betreated as an integer & vice versa.Ø Structured languageØ It can be compiled on a variety of computers.

Page 25: CE 2207 Numerical Analysis and Computer Programming...Numerical Analysis and Computer Programming PINGKI DATTA, LECTURER, DEPARTMENT OF CIVIL ENGINEERING, KUET. 2 Course outline: Computer

PINGKI DATTA, LECTURER, DEPARTMENT OF CIVIL ENGINEERING, KUET.

25

Characteristics of C Language:(Cont.):Ø Low level (Bit Wise) programming readily availableØ Pointer implementation - extensive use of pointersfor 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.

Page 26: CE 2207 Numerical Analysis and Computer Programming...Numerical Analysis and Computer Programming PINGKI DATTA, LECTURER, DEPARTMENT OF CIVIL ENGINEERING, KUET. 2 Course outline: Computer

PINGKI DATTA, LECTURER, DEPARTMENT OF CIVIL ENGINEERING, KUET.

26

Basic Structure of a C ProgramDocumentation sectionLink SectionDefinition SectionGlobal Declaration Sectionmain () Function Section{

Declaration partExecutable part

}Subprogram section

Function 1……………...………………………………Function n

Page 27: CE 2207 Numerical Analysis and Computer Programming...Numerical Analysis and Computer Programming PINGKI DATTA, LECTURER, DEPARTMENT OF CIVIL ENGINEERING, KUET. 2 Course outline: Computer

PINGKI DATTA, LECTURER, DEPARTMENT OF CIVIL ENGINEERING, KUET.

27

Documentation section It consists of comment lines giving the name of the

program, the author and other details. It starts with the character /* and ends with */. Comment lines are not an executable statement.

Link section The link section provides instruction to the compiler to

link functions from the system library. Library functions are grouped category-wise and stored

in different files known as header files. The preprocessor directive # include as follow:

# include<file name>

Page 28: CE 2207 Numerical Analysis and Computer Programming...Numerical Analysis and Computer Programming PINGKI DATTA, LECTURER, DEPARTMENT OF CIVIL ENGINEERING, KUET. 2 Course outline: Computer

PINGKI DATTA, LECTURER, DEPARTMENT OF CIVIL ENGINEERING, KUET.

28

Definition SectionIt defines all the symbolic constants.symbolic constants are defined with #define instruction.#define is a pre-processor compiler directive not a statement.

Therefore #define lines should not ends with semicolon (;)symbolic constants are mainly defined with uppercase letters#define instructions are mainly placed before the main() function for example #define PI 3.1416

Global Declaration SectionThe variables that are used in more than one function is called

global variablesThis section declares all the user defined functionsFor example, definition of integer and real numbers are declared

in this section.

Page 29: CE 2207 Numerical Analysis and Computer Programming...Numerical Analysis and Computer Programming PINGKI DATTA, LECTURER, DEPARTMENT OF CIVIL ENGINEERING, KUET. 2 Course outline: Computer

PINGKI DATTA, LECTURER, DEPARTMENT OF CIVIL ENGINEERING, KUET.

29

main ( ) functionIt is a part of every C programThe parenthesis after main indicates that main is a program

building block called a functionC programs are composed of one or many functions like it but

main is a mustThis sections contains two parts, declaration and executable part.

The declaration parts declares all types of variables are defined inexecutable.

All the statements in declaration and executable parts are endswith semicolon (;)

Notes:• All commands in C must be lowercase (small letter).• C has a free-form line structure. End of each statement must be marked with

a semicolon (;). Multiple statements can be on the same line. White space is ignored. The C program starting point is identified by the word main ().

Page 30: CE 2207 Numerical Analysis and Computer Programming...Numerical Analysis and Computer Programming PINGKI DATTA, LECTURER, DEPARTMENT OF CIVIL ENGINEERING, KUET. 2 Course outline: Computer

PINGKI DATTA, LECTURER, DEPARTMENT OF CIVIL ENGINEERING, KUET.

30

FILES USED IN A C PROGRAMSource File- This file contains the source code of the

program. The file extension of any c file is c. The filecontains C source code that defines the main function& maybe other functions.

Header Files Header files contain definitions offunctions and variables which can be incorporatedinto any C program by using the pre-processor#include statement.

This is done at the beginning of the C source file. Forexample, to use the function printf() in a program, theline #include <stdio.h> should be at the beginning ofthe source file.

Page 31: CE 2207 Numerical Analysis and Computer Programming...Numerical Analysis and Computer Programming PINGKI DATTA, LECTURER, DEPARTMENT OF CIVIL ENGINEERING, KUET. 2 Course outline: Computer

PINGKI DATTA, LECTURER, DEPARTMENT OF CIVIL ENGINEERING, KUET.

31

All header files have the extension .h andgenerally reside in the /user/includesubdirectory.#include <string.h>#include <math.h>#include <mylib.h>The use of angle brackets <> informs thecompiler to search the compiler’s includedirectories for the specified file. The use of thedouble quotes " " around the filename informsthe compiler to start the search in the currentdirectory for the specified file.

Page 32: CE 2207 Numerical Analysis and Computer Programming...Numerical Analysis and Computer Programming PINGKI DATTA, LECTURER, DEPARTMENT OF CIVIL ENGINEERING, KUET. 2 Course outline: Computer

PINGKI DATTA, LECTURER, DEPARTMENT OF CIVIL ENGINEERING, KUET.

32

• Object File- An object file is a file containing

object code, with an extension .o, meaning re-locatable format machine code that is usually notdirectly executable. Object files are produced by anassembler, compiler, or other language translator,and used as input to the linker, which in turn typicallygenerates an executable or library by combiningparts of object files.

• Executable File- The binary executable file is

generated by the linker. The linker links the variousobject files to produce a binary file that can bedirectly executed.

Page 33: CE 2207 Numerical Analysis and Computer Programming...Numerical Analysis and Computer Programming PINGKI DATTA, LECTURER, DEPARTMENT OF CIVIL ENGINEERING, KUET. 2 Course outline: Computer

PINGKI DATTA, LECTURER, DEPARTMENT OF CIVIL ENGINEERING, KUET.

33

Compilation & Execution of a C Program

Page 34: CE 2207 Numerical Analysis and Computer Programming...Numerical Analysis and Computer Programming PINGKI DATTA, LECTURER, DEPARTMENT OF CIVIL ENGINEERING, KUET. 2 Course outline: Computer

PINGKI DATTA, LECTURER, DEPARTMENT OF CIVIL ENGINEERING, KUET.

34

/*PROGRAM:02*//*DEVELOP A PROGRAM TO WRITE KUET*/#include<stdio.h>#include<conio.h>main(){printf("OUTPUT:\n");printf("KUET");printf("\n Roll:\n Section: A\nGroup:");getch();}

OUTPUT:KUETRoll:Section: AGroup:

Page 35: CE 2207 Numerical Analysis and Computer Programming...Numerical Analysis and Computer Programming PINGKI DATTA, LECTURER, DEPARTMENT OF CIVIL ENGINEERING, KUET. 2 Course outline: Computer

PINGKI DATTA, LECTURER, DEPARTMENT OF CIVIL ENGINEERING, KUET.

35