Presentation is loading. Please wait.

Presentation is loading. Please wait.

EKT 120/4 C Programming (Additional note). Computer Fundamentals Computer is a machine that can receive, store, transform an output data of all kinds.

Similar presentations


Presentation on theme: "EKT 120/4 C Programming (Additional note). Computer Fundamentals Computer is a machine that can receive, store, transform an output data of all kinds."— Presentation transcript:

1 EKT 120/4 C Programming (Additional note)

2 Computer Fundamentals Computer is a machine that can receive, store, transform an output data of all kinds : numbers, text, images, graphics and sound. Device capable of performing computations and making logical decisions faster than human.

3 Computer Hardware Input Device Output Device Secondary Memory Control Unit Arithmetic and Logic Unit Register CPU Main Memory Computer system are divided into hardware and software. Hardware is the equipment used to perform the necessary computations and includes the Main memory, second memory (E.g. HDD,FDD), CPU, input devices and output devices.

4 Main Memory keeps information from the input unit also keeps processed information until it can be placed on output devices all programs must be loaded into main memory before they can be executed and all data must be brought into main memory before it can be manipulated.

5 Main Memory Main memory can be further classified into two types: Random Access Memory (RAM) Random Access Memory (RAM) information in RAM will be lost when the computer is turned-off. Read Only Memory (ROM) Read Only Memory (ROM) they have been set during the manufacturing process. ROM usually contains instructions and information considered to be fundamental to the computer.

6 Secondary Memory Main memory is only used during processing following certain instructions Permanent information is NOT stored in main memory but is stored in secondary memory

7 Central Processing Unit (CPU) CPU is the computer’s administrator Responsible for supervising the operation of the other section Consists of two functional units: A control unit A control unit - supervises all activities of the computer system An arithmetic-logic unit (ALU) An arithmetic-logic unit (ALU) - perform basic arithmetic operations and comparison operations - perform basic arithmetic operations and comparison operations

8 Input/Output Device Input device - feeds data and programs into computers E.g. keyboard, mouse, touch screen, scanners E.g. keyboard, mouse, touch screen, scanners Output device - display results produced by computer E.g. monitor, printer, speaker E.g. monitor, printer, speaker

9 Computer Software As a complement to hardware, computer system also needs software. Software consists of the programs that enable us to solve problems with a computer by providing it with lists of instructions to perform. Software are classified as: System software System software Application software Application software

10 Computer Software (cont’d) System software : manage the computer and its peripheral devices (hardware) E.g. Operating systems (OS) E.g. Operating systems (OS) Text editor Text editor Preprocessor Preprocessor Language translator Language translator Linker Linker Loader Loader

11 Computer Software (cont’d) Application software : performs specific tasks There are two types: Program to solve specific problems Program to solve specific problems Program written by user to solve specified problem Program written by user to solve specified problem E.g. word processor, desktop publishing software, spreadsheets, database, graphics, communication, program perform a specific task such as accounting, scientific, engineering, education etc

12 Computer programming “The computer program’s role in this technology is essential; without a list of instructions to follow, the computer is virtually useless.” “The computer program’s role in this technology is essential; without a list of instructions to follow, the computer is virtually useless.” Programming languages allow us to write those programs and thus communicate with computers. Programming languages allow us to write those programs and thus communicate with computers.

13 Programming Languages “Programmers write instructions in various programming languages, machines language is only language that a computer can understand. However, human hard to understand it.” Three categories Programming language Machine Language Machine Language Assembly Language Assembly Language High-Level Language High-Level Language

14 MachineAssemblyHigh-Level Binary number codes understood by a specific CPU. Machine-dependent programming language Mnemonic codes that correspond to machine language instructions. Machine-independent programming language that combines algebraic expressions and English symbols. Understood by computer-no translator needed. Hard to learn because it is written in 0’s and 1’s Human easier to understand and written Improve weaknesses in Machine Language and Assembly Executable without translator Needs language translator called Assembler to change Assembly Language to Machine Code for execution purpose Needs compiler : a system software that translate source program to object program (machine code) before executed Bunch of 0’s and 1’s Program written in machine language can be executed without being translated Strings of 0’s and 1’s are replaced into instructions which resembles English language to represent computer operation element still too long and not portable Portable Written in one instruction to carry out several instruction in machine level 000101101 = ADD LOAD A ADD B STORE C C = A + B

15 Language Application Area FOTRAN Scientific Programming COBOL Business data processing LISP Artificial Intelligence C Systems programming Prolog Artificial Intelligence Ada Real-time distributed system Smalltalk Graphical User Interface-OOP C++ Supports Objects and OOP High Level Languages (J.R.Hanly, E.B.Koffman : Problem solving and program design in C)

16 The lowest-level programming language. Machine languages are the only languages understood by computers. While easily understood by computers, machine languages are almost impossible for humans to use because they consist entirely of numbers.programming language languagescomputers ProgrammersProgrammers, therefore, use either a high-level programming language or an assembly language. An assembly language contains the same instructions as a machine language, but the instructions and variables have names instead of being just numbers.programming languageassembly language instructionsvariables names ProgramsPrograms written in high-level languages are translated into assembly language or machine language by a compiler. Assembly language programs are translated into machine language by a program called an assembler.high-level languagescompilerprogramsprogramassembler Every CPU has its own unique machine language. Programs must be rewritten or recompiled, therefore, to run on different types of computers.CPUcompiledrun

17 Programming -Specifying a detailed set of instructions telling a computer how to perform some tasks. Statements: are the "sentences" of the C language A function is a list of statements and local storage, which when given some "information", does some processing and returns a value. statements Execution of a C program always begins in main(). Statements are executed sequentially, beginning with the first. General form of a C program Introduction to C Programming Preprocessor directives Main function heading{ Declarations Executable statements} Preprocessor directives Main function heading{ Declarations Executable statements} #include int main(){ int pages; printf( "Enter number of copied pages\n" ); scanf("%d",&pages); return 0; } #include int main(){ int pages; printf( "Enter number of copied pages\n" ); scanf("%d",&pages); return 0; }

18 C Program - A collection of functions which when compiled, assembled, linked, loaded, and executed performs some task. functionscompiledassembledlinked loadedexecutedfunctionscompiledassembledlinked loadedexecuted Compile: Translate a program from source code to assembly code. sourceassemblysourceassemblyAssemble: Translate a program from assembly code to object code (sometimes these two steps are combined and called compiling). assemblyobjectassemblyobject Link: Pull together all of the functions needed for the program (both user defined objects and system libraries) and arrange that locations of functions and variables are known. This step creates the executable. objectssystem librariesexecutableobjectssystem librariesexecutableLoad: Move the executable into computer memory. executable Execute: RunRun the program Run

19 Algorithm and pseudo code To understand the problem and plan approach to solve the problem. A list of steps for solving a problem. Techniques to develop structured computer programs. Algorithm – a procedure for solving a problem in terms of the actions to be executed and the order in which these actions are to be executed. Algorithm – a procedure for solving a problem in terms of the actions to be executed and the order in which these actions are to be executed.

20 Algorithm and pseudo code Pseudo code : artificial and informal language that helps programmers develop algorithms. Similar to everyday English. Convenient and user-friendly. Not an actual computer program. Not executed on computers – only help the programmer before writing the program. Consists purely of characters. Carefully prepared pseudo code program – converted easily to corresponding C program. Consists only of action statements. Example : if student’s grade is greater than or equal to 30 Print “Pass” else Print “Fail”

21 Algorithms Flowchart: A graphical representation of an algorithm or a portion of it. Drawn using certain special purpose symbols (rectangular, diamonds, oval and small circles )

22 Algorithm-Basic symbols in a flowchart Start/ End Process Input/Output Decision Flow direction Connector

23 EXAMPLE Start read num1, num2 sum=num1+num2 print sum End Sum of two numbers operation

24 Control Structure All programs could be written in terms of three control structures: Sequence structure Selection structure Repetition structure Program control – specifying the order in which statements are to be executed in a computer program.

25 Exercises A photocopy center charges 6 cent per copied page. Design a program that can calculate the cost for any given number of copies.

26 Solution to exercise Step 1: Specify the problem requirements Price per page is 6 cents Given the number of copied pages The total cost. Step 2 : Analyze the problem Step 3 : Design the algorithm to solve problem Pseudo code : Read the number of copied pages Count the total cost total cost = copied pages x 0.06 total cost = copied pages x 0.06 Display / print number of copied pages Formula : Total cost is copied pages x 6 cents total cost

27 Solution to exercise (cont’d) Step 3: Design the algorithm to solve problem (cont’d) Flowchart start Scan number of pages total cost = pages x 0.06 cents Display / Print total cost End

28 Solution to exercise (cont’d) Step 3: Design the algorithm to solve problem (cont’d) Pseudo code C code Takes/read number of copied pages: cost = copied pages x 0.06 cost = copied pages x 0.06 Display/print the calculated cost Scanf (“%d”, &pages); total_cost = pages* 0.06; Printf(“%.2f”,total_cost;

29 Solution to exercise (cont’d) Step 4: Implement the algorithm /* Excercise 1 : Calculate and print the total cost */ #include #include /* function main begins program execution */ int main() { int pages; /* data type-integer declaration*/ int pages; /* data type-integer declaration*/ float total_cost; /* data type-float declaration*/ float total_cost; /* data type-float declaration*/ printf( "Enter number of copied pages\n" ); printf( "Enter number of copied pages\n" ); scanf( "%d",&pages); /* read number of copied pages */ scanf( "%d",&pages); /* read number of copied pages */ total_cost = pages*0.06; /* the formula*/ total_cost = pages*0.06; /* the formula*/ printf( "Total cost is = RM%.2f\n",total_cost); /*display/print the total cost*/ printf( "Total cost is = RM%.2f\n",total_cost); /*display/print the total cost*/ return 0; /* indicate that program ended successfully */ return 0; /* indicate that program ended successfully */ } /* end function main */ Step 5 : Test and verify the completed program Compile and run if compilation process is successful. Step 6: Maintain and update the program.


Download ppt "EKT 120/4 C Programming (Additional note). Computer Fundamentals Computer is a machine that can receive, store, transform an output data of all kinds."

Similar presentations


Ads by Google