Download C Program - UniMAP Portal

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Abstraction (computer science) wikipedia , lookup

Programming language wikipedia , lookup

Algorithm wikipedia , lookup

Program optimization wikipedia , lookup

Structured programming wikipedia , lookup

Compiler wikipedia , lookup

Go (programming language) wikipedia , lookup

History of compiler construction wikipedia , lookup

Parallel computing wikipedia , lookup

C Sharp (programming language) wikipedia , lookup

ILLIAC IV wikipedia , lookup

Interpreter (computing) wikipedia , lookup

Assembly language wikipedia , lookup

Transcript
EKT 120/4
C Programming
(Additional note)
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.
Computer Hardware
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.
CPU
Control Unit
Input Device
Arithmetic and
Logic Unit
Register
Main Memory
Secondary Memory
Output Device
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.
Main Memory
Main memory can be further classified into
two types:

Random Access Memory (RAM)
information in RAM will be lost when the computer
is turned-off.

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.
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
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
- supervises all activities of the computer system
An arithmetic-logic unit (ALU)
- perform basic arithmetic operations and
comparison operations
Input/Output Device
Input device - feeds data and programs
into computers

E.g. keyboard, mouse, touch screen,
scanners
Output device - display results produced
by computer

E.g. monitor, printer, speaker
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
Application software
Computer Software (cont’d)
System software : manage the computer and its
peripheral devices (hardware)






E.g. Operating systems (OS)
Text editor
Preprocessor
Language translator
Linker
Loader
Computer Software (cont’d)
Application software : performs specific tasks
There are two types:


Program to solve specific problems
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
Computer programming
“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
“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
Assembly Language
High-Level Language
Machine
Assembly
High-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
ADD
STORE
A
B
C
C=A+B
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)
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.
Programmers, 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.
Programs 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.
Every CPU has its own unique machine language. Programs must be
rewritten or recompiled, therefore, to run on different types of computers.
Introduction to C Programming
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.
Execution of a C program always begins in main(). Statements are
executed sequentially, beginning with the first.
General form of a C program
Preprocessor
directives
Main function
heading
{
Declarations
Executable
#include <stdio.h>
int main()
{
int pages;
printf( "Enter number of copied pages\n" );
scanf("%d",&pages);
return 0;
}
C Program - A collection of functions which when compiled, assembled, linked,
loaded, and executed performs some task.
Compile:
Translate a program from source code
to assembly code.
Assemble:
Translate a program from assembly
code to object code (sometimes these
two steps are combined and called
compiling).
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.
Load:
Move the executable into computer
memory.
Execute:
Run the program
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 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”
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 )
Algorithm-Basic symbols in a
flowchart
Start/ End
Flow
direction
Process
Connector
Input/Output
Decision
EXAMPLE
Start
read num1, num2
sum=num1+num2
print sum
End
Sum of two numbers operation
Control Structure
Program control – specifying the order in
which statements are to be executed in a
computer program.
All programs could be written in terms of three control
structures:
Sequence structure
Selection structure
Repetition structure
Exercises
A photocopy center charges 6 cent per copied
page. Design a program that can calculate the
cost for any given number of copies.
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
number of
copied
pages
Formula :
Total cost is copied
pages x 6 cents
total cost
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
Display / print
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
Solution to exercise (cont’d)
Step 3: Design the algorithm to solve problem (cont’d)
Pseudo code
Pseudo code
C code
Takes/read number of copied pages: Scanf (“%d”, &pages);
cost = copied pages x 0.06
total_cost = pages* 0.06;
Display/print the calculated cost
Printf(“%.2f”,total_cost;
Solution to exercise (cont’d)
Step 4: Implement the algorithm
/*
Excercise 1 : Calculate and print the total cost */
#include <stdio.h>
/* function main begins program execution */
int main()
{
int pages;
/* data type-integer declaration*/
float total_cost;
/* data type-float declaration*/
printf( "Enter number of copied pages\n" );
scanf( "%d",&pages); /* read number of copied pages */
total_cost = pages*0.06; /* the formula*/
printf( "Total cost is = RM%.2f\n",total_cost); /*display/print the total
cost*/
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.