AACS1074 Programming Concepts and Design IChapter 1: Introduction to C
Page 1
Chapter 1:
Introduction to C
AACS1074 Programming Concepts and Design IChapter 1: Introduction to C
Page 2
1.1 Introduction to Computers
Overview of computer system:
Computer System
SoftwareHardware
The physical equipments. E.g. keyboard, mouse, monitor, processor, memory, etc.
Collection of programs that tell hardware how to perform tasks.
AACS1074 Programming Concepts and Design IChapter 1: Introduction to C
Page 3
1.1 Introduction to Computers
2 categories of computer software:
Computer System
SoftwareHardware
SystemSoftware
ApplicationSoftware
Programs that manage computer resources.
Programs that help users to solve their problems.
AACS1074 Programming Concepts and Design IChapter 1: Introduction to C
Page 4
1.1 Introduction to Computers
System software and application software:
Computer System
SoftwareHardware
SystemSoftware
ApplicationSoftware
OperatingSystem
SystemSupportSoftware
SystemDevelopment
Software
General-PurposeSoftware
Application-SpecificSoftware
AACS1074 Programming Concepts and Design IChapter 1: Introduction to C
Page 5
Example of system software:
UNIX, Linux, MS Windows, Mac OS, etc.
Disk defragmenter, screen saver, antivirus tool, etc.
Programs that creates programs.
MS Visual C++ 6.0, MS Visual Studio 2008, Java NetBeans, etc.
OperatingSystem
SystemSupportSoftware
SystemDevelopment
Software
AACS1074 Programming Concepts and Design IChapter 1: Introduction to C
Page 6
Example of application software:
MS Word 2007, Windows Media Player, Window Live Messenger, etc.
LRT ticketing system, hotel reservation system, payroll system, etc.
General-PurposeSoftware
Application-SpecificSoftware
AACS1074 Programming Concepts and Design IChapter 1: Introduction to C
Page 7
1.2 Computer Languages
Program: A set of instructions that tell computer how to perform tasks.
We use a computer / programming language to write program.
Categories of computer languages:
Machine languages
Symbolic languages
High-level languages
AACS1074 Programming Concepts and Design IChapter 1: Introduction to C
Page 8
Evolution of computer languages:
AACS1074 Programming Concepts and Design IChapter 1: Introduction to C
Page 9
Program in machine language:
AACS1074 Programming Concepts and Design IChapter 1: Introduction to C
Page 10
Machine Language
Use binary codes (0s and 1s).
The only language computer can understand.
Platform-dependent.
Very hard to program.
100100100011010110101110
AACS1074 Programming Concepts and Design IChapter 1: Introduction to C
Page 11
Program in symbolic language:
AACS1074 Programming Concepts and Design IChapter 1: Introduction to C
Page 12
Symbolic Language
a.k.a. assembly language.
Use symbolic codes, mnemonics and meaningful abbreviations.
Platform-dependent.
Easier to program.
E.g. MASM 6.15, Turbo Assembler, etc.
AACS1074 Programming Concepts and Design IChapter 1: Introduction to C
Page 13
Wait a minute…
Since computers only understand machine language. Then, how computers recognize other computer languages such as symbolic language?
?
AACS1074 Programming Concepts and Design IChapter 1: Introduction to C
Page 14
Translate it!!!
Programmer writes a program using preferable programming language. Finally, translate it into machine language by using a translator and the program is ready to run!!!
SourceProgram Translator
100010111100110100110011
Machine Language
AACS1074 Programming Concepts and Design IChapter 1: Introduction to C
Page 15
Assembler
Symbolic language has to be translated to machine language before it can be run.
Translator: assembler.
Symbolic Language
Machine Language
Assembler
Source Program Translator 0s and 1s
AACS1074 Programming Concepts and Design IChapter 1: Introduction to C
Page 16
Example of high-level language:
AACS1074 Programming Concepts and Design IChapter 1: Introduction to C
Page 17
AACS1074 Programming Concepts and Design IChapter 1: Introduction to C
Page 18
High-Level Language
a.k.a procedural language.
Use English-like codes and mathematical operators.
Platform-independent.
Very easy to program.
E.g. C, Pascal, etc.
AACS1074 Programming Concepts and Design IChapter 1: Introduction to C
Page 19
Compiler / interpreter
High-level language has to be translated to machine language before it can be run.
Translator: compiler / interpreter.
High-Level Language
Machine Language
Compiler / Interpreter
Source Program Translator 0s and 1s
AACS1074 Programming Concepts and Design IChapter 1: Introduction to C
Page 20
The Compilation Process
Compiler converts source program into object program (machine language version).
If encounters error, program error listing is shown.
Object program is then loaded and run.
CompilerObject
ProgramResults
ProgramError
Listing
Data
SourceProgram
Machine LanguageTranslator
AACS1074 Programming Concepts and Design IChapter 1: Introduction to C
Page 21
1.3 Introduction to C
C is a high-level language.
For systems and applications programming.
Portable / platform-independent: run at other platforms with no or little alterations.
AACS1074 Programming Concepts and Design IChapter 1: Introduction to C
Page 22
Exercise 1.1
By following the instructions giving by your lecturer, write, compile and run your first C program – the Hello World program!
#include <stdio.h>
int main(void){ printf("Hello World!\n"); return 0;}
AACS1074 Programming Concepts and Design IChapter 1: Introduction to C
Page 23
Exercise 1.2
Create a new program that display the following lyrics on screen:
Welcome, welcome all of youGlad you are with usShake hands, no need to be blueWelcome to you!
AACS1074 Programming Concepts and Design IChapter 1: Introduction to C
Page 24
1.4 Program Development
To develop a program, programmer must complete the following steps:
Understand the problem
Develop a solution
Write the program
Test the program
AACS1074 Programming Concepts and Design IChapter 1: Introduction to C
Page 25
Take time to understand the problem and design the solution, before you code the actual program.
This reduce the chances of misunderstanding and errors. Hence, the final program can be implemented in a shorter time and with a better quality.
An old programming proverb:
Resist the temptation to code!!!
AACS1074 Programming Concepts and Design IChapter 1: Introduction to C
Page 26
Program Development
Understand the problem
Carefully study the user requirements.
Understand what he wants the program to do and what kind of output he wants to have.
Develop a solution
Understand the problem
Write the program
Test the program
AACS1074 Programming Concepts and Design IChapter 1: Introduction to C
Page 27
Program Development
Develop a solution
Design the logic of the program by using tools such as:
Structure chart
Pseudocode
Flowchart
Develop a solution
Understand the problem
Write the program
Test the program
We will talk about them later.
AACS1074 Programming Concepts and Design IChapter 1: Introduction to C
Page 28
Program Development
Write the program
Code the actual program by using the preferable programming language.
Develop a solution
Understand the problem
Write the program
Test the program
AACS1074 Programming Concepts and Design IChapter 1: Introduction to C
Page 29
Program Development
Test the program
Run and test the program to ensure it is free of logical errors.
Develop a solution
Understand the problem
Write the program
Test the program
AACS1074 Programming Concepts and Design IChapter 1: Introduction to C
Page 30
Example 1.1
You would like to install new floor covering in your home and would like to calculate the cost that would be involved. Your plan is as follows:
Only the living space will be carpeted. The garage and closets will not be considered.
The kitchen and bathrooms will be covered with linoleum. The rest of the house is to be carpeted.
AACS1074 Programming Concepts and Design IChapter 1: Introduction to C
Page 31
You need a computer program to compute the flooring cost. How should you approach this problem?
AACS1074 Programming Concepts and Design IChapter 1: Introduction to C
Page 32
A big programming problem.
Divide and Conquer!!!
AACS1074 Programming Concepts and Design IChapter 1: Introduction to C
Page 33
Break down the task into a few big subtasks.
Divide and Conquer!!!
AACS1074 Programming Concepts and Design IChapter 1: Introduction to C
Page 34
Decompose each big subtask into smaller subtasks.
Divide and Conquer!!!
AACS1074 Programming Concepts and Design IChapter 1: Introduction to C
Page 35
Further break down each smaller subtask into even smaller subtasks.
Divide and Conquer!!!
AACS1074 Programming Concepts and Design IChapter 1: Introduction to C
Page 36
Eventually the subtasks become so small that they are trivial to implement in C language.
Divide and Conquer!!!
AACS1074 Programming Concepts and Design IChapter 1: Introduction to C
Page 37
Divide and Conquer!!!
a.k.a. stepwise refinement or top-down design.
AACS1074 Programming Concepts and Design IChapter 1: Introduction to C
Page 38
Tool: Structure Chart
A hierarchy chart that shows the functional flow through the program.
Shows how the program is broken into logical steps. Each step will be a separate module.
AACS1074 Programming Concepts and Design IChapter 1: Introduction to C
Page 39
A structure chart:
AACS1074 Programming Concepts and Design IChapter 1: Introduction to C
Page 40
Tool: Pseudocode
Use English words to convey program logic.
Contains the logical steps / algorithms to accomplish the task.
AACS1074 Programming Concepts and Design IChapter 1: Introduction to C
Page 41
A pseudocode:
AACS1074 Programming Concepts and Design IChapter 1: Introduction to C
Page 42
Tool: Flowchart
Use standard graphical symbols to represent the logical flow of data through a program.
[ show flowchart ]
AACS1074 Programming Concepts and Design IChapter 1: Introduction to C
Page 43
Flowchart Symbols
Input/Output Processing Module
Decision FlowlinesTerminal
Off-page Connector
On-pageConnector
AACS1074 Programming Concepts and Design IChapter 1: Introduction to C
Page 44
Example 1.2
Problem: Calculate the sum of 2 numbers.
Hint: The general algorithm for a programming problem is normally:
Get the data
Perform the computations
Display the results
AACS1074 Programming Concepts and Design IChapter 1: Introduction to C
Page 45
Example 1.2: Structure chart
Sum Two Numbers
Get User Input
Calculate Sum
Display Sum
AACS1074 Programming Concepts and Design IChapter 1: Introduction to C
Page 46
Example 1.2: Pseudocode
START Prompt and read number1 Prompt and read number2 sum = number1 + number2 Display sumEND
AACS1074 Programming Concepts and Design IChapter 1: Introduction to C
Page 47
Example 1.2: Flowchart
START
END
Read number1
Read number2
sum =number1 + number2
Display sum
AACS1074 Programming Concepts and Design IChapter 1: Introduction to C
Page 48
Exercise 1.3
By using structure chart, pseudocode and flowchart, design the solution for a program that reads three (3) numbers from user, calculates their total and average, and displays the calculated total and average on the screen.