Chapt. 0 - 1 Computer Programming For Engineers - Ceng 206
Chapt. 0 - 1 Computer Programming For Engineers - Ceng 206
PROGRAMMING FOR
ENGINEERS – CENG 205
Tweneboah-Koduah Samuel
? MEng. - ICE BSc. - TE
D o l p h i n z 05/07/2024
Te l e n e t w o r k s C o m p a n y CENG 205 1
QUOTE
This mind is not an empty vessel to be filled
but a powerhouse to be ignited
– Plutarch
Create a portable
electronic toilet
Central Processing
Mass Unit (CPU) Communication
storage Device
soft Hard
ware ware
Clock
Step 4: Data is processed by the ALU
Algorith
Problem Language
m
Voltage
Machine
in
code
circuitry
05/07/2024 CENG 205 19
SOFTWARE DESIGN
Flowchart/algorithms
Algorithms – an algorithm is any well-
defined computational procedure that takes
some value, or set of values, as input and
produces some value, or set of values as
output
It’s the first step of the solution process of
any problem after analysis.
Flowchart is graphical representation of
algorithms – sequence of events to occur in
order to accomplish a given task.
05/07/2024 CENG 205 20
ALGORITHMS AND DATA
STRUCTURES
Sorting by insertion-
example
Input: sequence of numbers
Output: A permutation
(reordering) of the input
such that
Pseudocode
Pseudocode
34
FLOWCHART
35
LOOPING FLOW CHART
a
05/07/2024 CENG 205 39
CLASS AVERAGE
ALGORITHM
Problem: Calculate and report the grade-point
average for a class
Discussion: The average grade equals the sum of
all grades divided by the number of students
Output: Average grade
Input: Student grades
Processing: Find the sum of the grades; count the
number of students; calculate average
40
Determine the average grade of a class.
Pseudocode:
Initialize Counter and Sum to 0
Do While there are more data
Get the next Grade
Add the Grade to the Sum
Increment the Counter
Loop
Computer Average = Sum / Counter
Display Average
05/07/2024 CENG 205 41
FLOWCHART
Pseudocode:
Initialize Counter and Sum to 0
Do While there are more data
Get the next Grade
Add the Grade to the Sum
Increment the Counter
Loop
Computer Average = Sum / Counter
Display Average
42
SELF CHECK
Write an algorithm and draw a flowchart that will
calculate the roots of a quadratic equation
ax bx c 0
2
and
Step 1: Start
Step 2: Input NAME, OVERTIME, ABSENT
Step 3: if (OVERTIME–(2/3)*ABSENT > 40) then
PAYMENT 50
else if (OVERTIME–(2/3)*ABSENT > 30) then
PAYMENT 40
else if (OVERTIME–(2/3)*ABSENT > 20) then
PAYMENT 30
else if (OVERTIME–(2/3)*ABSENT > 10) then
PAYMENT 20
else
PAYMENT 10
endif
Step 4: Print “Bonus for”, NAME “is $”, PAYMENT
Step 5: End
SELF CHECK
Flowchart: Draw the flowchart of the above algorithm?
ADVANTAGES OF USING
FLOWCHART
Communication: Flowcharts are better way of communicating
the logic of a system to all concerned.
Effective analysis: With the help of flowchart, problem can be
analysed in more effective way.
Proper documentation: Program flowcharts serve as a good
program documentation, which is needed for various purposes.
Efficient Coding: The flowcharts act as a guide or
blueprint during the systems analysis and program development
phase.
Proper Debugging: The flowchart helps in debugging process.
Efficient Program Maintenance: The maintenance of operating
program becomes easy with the help of flowchart. It helps the
programmer to put efforts more efficiently on that part.
05/07/2024 CENG 205 48
LIMITATION OF
FLOWCHART
Complex logic: Sometimes, the program logic
is quite complicated. In that case, flowchart
becomes complex and clumsy.
Alterations and Modifications: If alterations are
required the flowchart may require re-drawing
completely.
Reproduction: As the flowchart symbols cannot
be typed, reproduction of flowchart becomes a
problem.
The essentials of what is done can easily be lost in
the technical details of how it is done.
05/07/2024 CENG 205 49
The syntax of C pretty-well guarantees you will easily understand
other languages that came afterwards like C++, Java, Javascript,
and C#.NET
05/07/2024 CENG 205 50
COURTESY OF
CONFUCIUS
Learning by doing is probably the
most effective way to maximize
retention.
http://www.tutorialspoint.com/
cprogramming/index.htm
53
PROGRAMMING LANGUAGE CLASSIFICATION
High-level languages: Closer to human
language, E.g. C, C++, Java, Visual Basic
Easier to intuitively program
Not CPU specific, portable with the right compiler
More than 1,000 available
Assembly languages: Closer to what
the computer processes
CPU specific, faster than high-level languages
More tedious to program
Machine language: The sequence of 0 and
1 processed by the CPU
05/07/2024 CENG 205 54
HUMAN VS. PROGRAMMING LANGUAGES
Human Languages Programming Languages
Learn grammar rules, E.g.,
Learn grammar rules, E.g., The printf(“Hello World\
book has arrived” vs. “Book n”); vs.
arriving has” printf(Hello world)
Learn to compose meaningful Learn to compose meaningful
sentences E.g., “The table is sentences x = 0; y =
having a fever” Syntactically 10/x;
correct but not meaningful
Learn how to use
Learn how to use words/functions
words/sentences programs/applications in a
paragraphs/chapters in a concise and functional way
concise and functional way
Difference: No flexibility
Difference: Flexibility in
expression
05/07/2024 CENG 205 55
PROCESS FOR BUILDING AND EXECUTING A PROGRAM
Compil
Edit Link Load Execute
e
Data structures to
hold data
Instructions for
manipulating the
data
int main(void)
{
printf(“Hello Class, this is my first C program\n”);
return(0);
}
#include
Gives the program access to header files of standard
libraries
Example standard library header files: stdio.h,
math.h, string.h, etc.
Gives the program access to user-defined libraries as
well#include <header_file.h> /* for standard
Syntax:
A library
libraries */ is a collection of object files that
perform certain functions
#include “user_file.h” /* for user-defined
Sometimes
libraries */ explicit linking may be needed (e.g.,
math.h)
Example: #include <stdio.h> /* Most programs include
this */
05/07/2024 CENG 205 82
DATA STRUCTURES -
CONSTANTS
Many times a specific value is used multiple times throughout
the program
Can be pre-defined in a macro
syntax: #define macro_name value
A = 10,
05/07/2024 CENG 205
B91 = 20
RELATIONAL OPERATORS CONT’D
Output:
value of c : 30 declaration
value of f :
23.333334
Extern variable is a
global variable
accessible to a
function below it
05/07/2024 CENG 205 93
DISPLAYING OUTPUT
Output operation: data displayed on a screen or stored on/in a file
Syntax: printf(“format string”, variable list);
Examples
printf(“%d\t%d\n”, x, y);
printf(“%3d %6d\n”, x, y);
printf(“%.0f %6.1f\n”, x, y);
Argumen Operation
t
%d print as a decimal number
%3d print as a decimal number in three digit format
%6.1f print as a floating point number with six digits
(including decimal point) and one decimal digit
%.2f print as a floating point number with two decimal
digits
%c
05/07/2024 print as a character CENG 205 94
DISPLAYING OUTPUT
Output operation: data displayed on a screen or stored on/in a file
#include<stdio.h>
// Example of printing variables with the printf
function
int main(void)
{
int x; // declaration of an integer
float y; // declaration of a float
char c;
x = 125; // initialization
y = 3.14159; // initialization
c = 'a'; // initialization
05/07/2024 CENG 205 98
PRINTING EXAMPLE – CONT’D
printf("Printing an integer number:%d\n", x); //
integer
printf("Printing a float number:%f\n",y); // real
printf("Printing a character:%c\n",c); // char
printf("You can also print message prompts\n");
printf("Integer:%d\tFloat:%.1f\n",x,y); // printing
tabs
printf("Two decimals:%4.2f\nThree decimals:
%6.3f\n",y,y);
// decimals
return(0);
}
I have
questions…
05/07/2024
! CENG 205 101