0% found this document useful (0 votes)
36 views

Computer Programming For Engineers: 1. Introduction To C Programming

This document provides an overview of a computer programming course for engineers. It outlines topics that will be covered, including an introduction to C programming, the history of C, problem solving and software development processes, algorithms, variables and assignment statements, and statements versus expressions. Students will learn how to convert algorithms for programming applications, identify variables and reserved words, and use different operator types in implementations.

Uploaded by

abdul ramdzi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

Computer Programming For Engineers: 1. Introduction To C Programming

This document provides an overview of a computer programming course for engineers. It outlines topics that will be covered, including an introduction to C programming, the history of C, problem solving and software development processes, algorithms, variables and assignment statements, and statements versus expressions. Students will learn how to convert algorithms for programming applications, identify variables and reserved words, and use different operator types in implementations.

Uploaded by

abdul ramdzi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

NCB20103

COMPUTER PROGRAMMING FOR ENGINEERS

1. INTRODUCTION TO C PROGRAMMING
Student Learning Outcomes
 Upon completion of this lesson, students will be able to:

Convert algorithm for programming application by applying three


phases in software development process

Identify variables and reserve words

Use different types of operators in programming implementation

Show the differences between statements and expressions

2
Topics
1. Computer Programming Language
2. History of C
3. Problem solution and software development
4. Algorithm
5. Variables and assignment statements
6. Statements and expression

3
Topics
1. Computer Programming Language
2. History of C
3. Problem solution and software development
4. Algorithm
5. Variables and assignment statements
6. Statements and expression

4
1. Computer Programming Language
Three (3) types of programming languages

1. Machine code or machine languages


• A sequence of 0’s and 1’s giving machine specific
instructions
• Every CPU has its own machine code/instruction
set.
• Machine code is machine dependent.
• Example: 00011001
• Instruction to add integer values in register “hl” and “de”
and the sum in “hl”

2. Assembly language
• Using meaningful symbols to represent machine
code.
• Usually defined by microprocessor’s manufacturer.
• Example: add hl,de

Assembler: Assembly code  machine code


Disassembler: machine code  assembly code 5
1. Computer Programming Language

3. High-level languages

Similar to everyday English and use mathematical


notations
(processed by compilers or interpreters)
Example of a C statement:
a = a + 8;

6
Topics
1. Computer Programming Language
2. History of C
3. Problem solution and software development
4. Algorithm
5. Variables and assignment statements
6. Statements and expression

7
2. History of C
C
Invented by Ritchie based on B, a simplified version of BCPL.
Used to develop Unix operating system and Unix commands
Most system software such as OS are written in C or C++
Replacement for assembly language for hardware interface.
By late 1970's C had evolved to “K & R C“

C Standards
1st C standard created in 1989 by ANSI, ratified by ISO in 1990.
It is called C89. Some call it C90.
2nd C standard was ratified in 1999, called C99.
Numerical extensions such as complex numbers, variable length
arrays, and IEEE floating-point arithmetic are major enhancement in
C99. C99 will be pointed out whenever features in C99 only are
presented.

8
Topics
1. Computer Programming Language
2. History of C
3. Problem solution and software development
4. Algorithm
5. Variables and assignment statements
6. Statements and expression

9
3. Problem solution and software
development
Problem solution (problem solving)
Problem-solving consists of using generic or ad hoc
methods, in an orderly manner, for finding solutions to any
given problems.

Software development
A process in the development of software product

10
3. Problem solution and software
development
 Software development – 3 phases
i. Development and design
• Analyze the problem
 Input, output, formula
• Develop solution (Algorithm)
 Pseudocode, flowchart
• Code the solution (coding)
 Syntax error, logic error, run-time error
• Test and correct the program

ii. Documentation
iii. Maintenance

11
3. Problem solution and software
development

Programming process

12
3. Problem solution and software
development

Programming shortcut?

13
3. Problem solution and software
development
Example:
Create a program to calculate and print out the total
numbers of two integers.

Solution:
Analyze the problem
 Input: two integer (num1 and num2)
 Output: total of two integers (total)
 Formula: total = num1 + num2

14
Topics
1. Computer Programming Language
2. History of C
3. Problem solution and software development
4. Algorithm
5. Variables and assignment statements
6. Statements and expression

15
4. Algorithm
 Algorithms as path through “Problem spaces”

a precise specification of a behavior


Algorithm:
intended to solve a well-defined problem

Coding: the translation of an algorithm into a


particular programming language

Program: a step-by-step execution plan that a


computer can perform as a sequence of
simple steps or instructions
16
4. Algorithm
Algorithm
Step-by-step sequence of instruction that described
how data is to be processed to produced desired
outputs

How to describe algorithm?


By two ways:
1. Pseudocode
2. Flowchart
17
4. Algorithm
4.1 Pseudocode
To describe algorithms in a English-like phrases that
used to write computer programs

Example: Area of a circle


Write a pseudocode to find
the area of a circle using Pseudocode
the following formula: •Input the value of radius
Area = pi x radius2
•Set value of PI.
• Prompts the user to enter
a value for radius and •Calculate area = pi x radius2
reads it. Display the result •Print area
on the screen.
18
4. Algorithm
4.2 Flowchart
A flowchart is a diagram that depicts the “flow” of a
program

Uses simple geometric symbols and arrows to define


relationships

19
4. Algorithm
4.2 Flowchart

 Common flowchart symbol:

20
Topics
1. Computer Programming Language
2. History of C
3. Problem solution and software development
4. Algorithm
5. Variables and assignment statements
6. Statements and expression

21
5. Variables and assignment statements
5.1 Operators
 Operator - A symbol that represents a specific action
 Assignment operator: ‘=‘
 Arithmetic operators:
i. + Addition
ii. - Subtraction
iii. * Multiplication
iv. / Division
v. % Modulus
 Relational operators:
i. < Less than comparison
ii. <= Less or equal comparison
iii. == Equal comparison
iv. > Greater than comparison
v. >= Greater or equal comparison
vi. != Not equal comparison

22
5. Variables and assignment statements
5.1 Operators
 Logical operators:
i. ! Logical negation
ii. && Logical AND
iii. || Logical OR

 Increment operator: ‘++’


 Add 1 to its operand

 Decrement operator: ‘--’


 Subtract 1 from its operand

23
5. Variables and assignment statements
5.1 Operators
Operations Associativity
::
() [] left to right
 Precedence and Function_name() right to left

associativity of . -> left to right


‘ ! ` ++ -- + - * right to left
operators &(type) sizeof
* / % .* ./ left to right
 The list of operators + - left to right
are shown on right. << >> left to right
Operators at the < <= > >= left to right

higher level has == != left to right


& left to right
precedence over ^ left to right
operators at the | left to right

lower level. && left to right


^^ left to right
|| left to right
?: right to left
= += -= *= /= %= |= right to left
<<= >>=
, left to right

24
5. Variables and assignment statements
5.2 variables
A variable is a location in memory that can be referred
to by an identifier and in which a data value that can be
changed is stored

A value that can be changed, depending on conditions


or on information passed to the program

25
5. Variables and assignment statements
5.2 variables
• Variable names in C
 May only consist of letters, digits, and underscores
 May be as long as you like, but only the first 31
characters are significant
 May not begin with a number
 Case sensitive: A ≠ a
 May not be a C reserved word (keyword)

26
5. Variables and assignment statements
5.2 variables
• Reserved words in c
auto break int long
case char register return
const continue short signed
default do sizeof static
double else struct switch
enum extern typedef union
float for unsigned void
goto if volatile while
27
5. Variables and assignment statements
5.2 variables
Naming convention
• C programmers generally agree on the following
conventions for naming variables.
 Begin variable names with lowercase letters
 Use meaningful identifiers
 Separate “words” within identifiers with underscores or mixed
upper and lower case.
 Examples: surfaceArea surface_Area surface_area
 Be consistent!

28
Topics
1. Computer Programming Language
2. History of C
3. Problem solution and software development
4. Algorithm
5. Variables and assignment statements
6. Statements and expression

29
6. Statements and expression
Statements
A statement is a block of code that does something.

Example:
o An assignment statement assigns a value to a variable.
o A for statement performs a loop

30
6. Statements and expression
Expression
Any legal combination of symbols that represents a value.

Example:
o X > 10
o Y+5

31
6. Statements and expression
Statement vs Expression
Every C program comprises of Statements.
Program executes statement by statement.
Each statement generally, comprises of some expression.

There are three (3) different classes of statements in C:


i. expression statements,
ii. compound statements,
iii. control statements.

32
33

You might also like