Lecture1 Programming
Lecture1 Programming
Lecture1 Programming
FUNDAMENTAL
BERC1313 Programming Fundamental
Lecture (1BERE/1BERZ): Friday, 8am – 10am, DK 3 KDK
Lab / Tutorial:
1BERE (s1/1): Monday, 2pm – 5pm, MSK
1 BERE (s1/2): Tuesday, 2pm – 5pm, MSK
1 BERZ (s1/1): Thursday, 2pm – 5pm, MPK
1 BERZ (s1/2): Tuesday, 8am – 11am, MPK
Assessment:
Lab Assessment – 30%
Lab Test – 10%
Mid Term Test – 20%
Final Exam – 40%
2
Chapter 1:
Introduction to Computers &
the Programming Language
3
Objectives
You will learn about:
Overview of computers
Computer hardware & software
Computer language
Evolution of programming languages
Discover what a compiler does
Examine a basic C program
Explore how a C program is processed
4
What is a computer?
• A general purpose machine commonly consisting
of digital circuitry that accepts inputs; stores,
manipulates, and generates outputs (example:
numbers, graphics, voice, video, etc); in
accordance to instructions called a program.
5
How does it looks like?
6
7
Computer components
Refers to the physical parts
Hardware of the computer that have
mass.
Can be touched.
Computer
8
Computer hardware
9
Hardware Inside the cpu
10
Computer software Software that manages the system
resources and control the
operations of the hardware.
System Normally supplied by the
manufacturer of the computer.
System programs take control of the
computer, such as an Operating
Computer System.
Software Example: Windows 7,8,10,11.
11
Hardware Inside the cpu
• CPU (Central Processing Unit) - performs most of the calculations
which enable a computer to function, and is sometimes referred to as
the "brain" of the computer. It is usually cooled by a heat sink and
fan.
• Chipset - mediates communication between the CPU and the other
components of the system, including main memory.
• RAM (Random-access Memory) - stores resident part of the current
running OS and all running processes. It is volatile, so main memory is
erased when program terminates or computer is turned off
• ROM (Read-only Memory) - stores the initial program that runs when
the computer is powered on or otherwise begins execution
(Bootstrapping also known as "booting" or "booting up").
• HDD (Hard Disk Drive) - for medium-term storage of data. It is non-
volatile, so data is retained even when program is not running or
computer is turned off.
• USB flash drive - a flash non-volatile memory data storage device
integrated with a USB interface, typically small, lightweight,
removable, and rewritable.
12
13
History Of Computer Languages:
14
Machine language
15
Assembly language
LOAD rate
MULT hour
STOR salary
16
High-level language
17
Example 1
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("Hello world!\n");
return 0;
}
Sample Run:
Hello world!
18
Example 2
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("Computers are dumb.\nBut programmers make
them seem smart.\n\n");
printf("The sum of 2 plus 3 is %d\n", 2 + 3);
return 0;
}
Sample Run:
Computers are dumb.
But programmers make them seem smart.
The sum of 2 plus 3 is 5
19
Integrated Development Environments
(IDE)
• An integrated development environment, or IDE,
combine all the tools needed to write, compile,
and debug a program into a single software
application.
• Examples are Microsoft Visual C++, Borland C++
Builder, CodeBlocks, etc.
20
Integrated Development Environments
(IDE)
21
Why Program?
• Computer – programmable machine designed to
follow instructions.
• Program – instructions in computer memory to
make it do something.
• Programmer – person who writes instructions
(programs) to make computer perform a task.
22
BERC 1313
PROGRAMMING FUNDAMENTAL
System Development
The critical process determines the overall quality
and success of the program.
If any program is designed carefully using good
structured development techniques, the program will
be efficient, error-free and easy to maintain.
Most programming projects are built using system
development life cycle.
One of the popular development life cycle is known
as the waterfall model.
Waterfall model
Program Development
• A multi-step process that requires you understand the
problem, design a solution, write the program, and test the
program.
• Output
– the distance in kilometers
(K)
Step 3: write the program
// written by: John, 1 BETC
/* Problem Solving Exercise:
Introduction to C */
#include <stdio.h>
#include <stdlib.h>
int main()
{
float distanceMiles;
float distanceKm;
return 0;
}
Step 4: test
• Output of the program:
\n or endl New line Takes the cursor to the beginning of the next line
\v Vertical Tab Takes the cursor to the next tab stop vertically.
• Types of constant
– Integer constant
– Float constant
• Numbers with decimal part
– Character constant
• Enclosed between two single quotes (‘)
– String constant
• A sequence of zero or more characters enclosed in double
quotes (“)
– Symbolic constant
• Defined constant and memory constant
Constants
• Three different ways
– Literal constants
• An unnamed constant used to specify data
• If the data cannot be changed, we can simply code the data
value itself in a statement
• E.g
• The way define work is that the expressions that follows the
name (.0825) replaces the name wherever it is found in the
program (like search and replace command)
Constants
– Memory constant
• C provides the capability to define a named constant where we
can add the type qualifier, const before the definition
• Type qualifier indicates that data cannot be changed and to fix
the contents of the memory location
• E.g
const float pi = 3.1416;
Variables
• Named memory locations that have a type, such as integer
or character and a size which is inherited from their type
• Once defined, variables are used to hold the data that are
required by the program from its operation
Variables
Variables
• Eg:
– int count = 0;
– int count , sum = 0; // Only sum is initialize.
– int count=0 , sum = 0; OR int count =0; int sum = 0;
Punctuator
Use for completing program structure
Including [ ] ( ) { } , ; : * # symbols
Operator
C uses a set of built in operators ( Eg : +, -, *,
/).
There are several classes of operators :
Arithmetic, relational, logical and assignment.
Common Programming Errors
Categories
missing semicolon,
illegal operation,
undeclared program follows a
such as dividing by
variables, missing faulty algorithm
zero.
braces
Common Programming Errors
• Errors in programs can be categorized into 1)
syntax errors, 2) logic errors, 3) run-time errors
• Syntax errors:
– When your code violate some or more grammar rules
of C.
– Detected by compiler as it attempts to translate your
program.
– Compiler cant translate your program and will not be
executed.
– Example : missing semicolon, undeclared variables,
missing braces etc.
Common Programming Errors
• Logic errors:
– Occur when a program follows a faulty algorithm.
– Logic errors usually do not cause run time errors
and do not display error messages.
– They are very difficult to detect.
– The only sign of a logic error may be incorrect
program output.
– You can detect logic errors by testing the program
thoroughly, comparing its output to calculated
results.
Common Programming Errors
• Run-time errors:
– Detected and displayed by the computer during
the execution of a program.
– Occurs when the program directs the computer to
perform an illegal operation, such as dividing by
zero.
– The computer will stop executing your program
and will display a diagnostic message that
indicates the line where the error was detected.