Sie sind auf Seite 1von 9

Introduction to Computer Programming

© copyright Saharudin Haron, UTM

Introduction to
Computer Programming
Chapter 1: Introduction to Programming

Saharudin Haron
Introduction to Computer Programming
© copyright Saharudin Haron, UTM

Chapter Objectives
It is expected that students have the ability to:

• Familiar with the important of computer


programming
• Identify appropriate control structures for
solving problems

Page 2 - 2
Introduction to Computer Programming
© copyright Saharudin Haron, UTM

Why Learn Programming


 Work 10 times faster than manual calculation
 Analyze the background of the problem
 Simulate and approximate process behavior
 Solve complex problem
 Discover more creative solutions

Page 2 - 3
Introduction to Computer Programming
© copyright Saharudin Haron, UTM

CASE STUDY: Bungee jumper velocity


The fall velocity of a bungee jumper can be
predicted by following differential equation:
𝑑𝑣 𝐶𝑑
=g- 𝑣
𝑑𝑡 𝑚

The equation can be solved numerically


using Euler’s method:
𝑑𝑣𝑖
𝑣𝑖+1 = 𝑣𝑖 + ∆𝑡
𝑑𝑡
Predict the velocity at t = 12s using a time
step of ∆t = 0.5s and initial t = 0.
Page 2 - 4
Introduction to Computer Programming
© copyright Saharudin Haron, UTM

What is a Program
 A computer program is a series of instructions which enables the
computer to perform a designated task.
 The program told the computer what to do in precise, step-by-step
detail.
 These steps might include obtaining data, arithmetic operations
(additional, subtraction, multiplication, division, etc.), data storage,
and information output.
 The computer will perform these tasks as instructed, even if they
don't always make sense.
 Consequently, it is the programmer who must develop a solution
to the problem.

Page 2 - 5
Introduction to Computer Programming
© copyright Saharudin Haron, UTM

What is a Program
A computer program is written in many programming languages
such as:
 FORTRAN (Formula Translating System) - Originally developed by
IBM in the 1950s for scientific and engineering applications.
 BASIC (Beginner's All-purpose Symbolic Instruction Code) -
designed by John G. Kemeny and Thomas E. Kurtz in 1964 at Dartmouth
College in New Hampshire. They wanted to enable students in fields other than
science and mathematics to use computers
 C - originally developed by Dennis Ritchie between 1969 and 1973 at AT&T
Bell Labs and used to re-implement the Unix operating system.
 C++ - an object-oriented programming language based on C. The first edition
was released in 1985.
 MATLAB (matrix laboratory) - intended primarily for numerical
computing in 1984. Now it is widely used in academic and research institutions
as well as industrial enterprises.

Page 2 - 6
Introduction to Computer Programming
© copyright Saharudin Haron, UTM

What is a Program
Example of FORTRAN program:
! Simple Fortran Program
program dowhile
integer :: x, i, N
real :: BETA
x=0
i=1
N = 10
BETA = 2.5
do while (i <= N)
x = (x + (i*2)) - ((i-BETA)/2)
write (*,*) "i = ", i, " x = ", x
i=i+2
end do
end program dowhile

Page 2 - 7
Introduction to Computer Programming
© copyright Saharudin Haron, UTM

What is a Program
Example of BASIC program:
10 INPUT "What is your name: ", U$
The resulting dialog might resemble:
20 PRINT "Hello "; U$ What is your name: Mike
30 INPUT "How many stars do you want: ", N
Hello Mike
40 S$ = ""
50 FOR I = 1 TO N
How many stars do you want: 7
60 S$ = S$ + "*" *******
70 NEXT I Do you want more stars? yes
80 PRINT S$ How many stars do you want: 3
90 INPUT "Do you want more stars? ", A$ ***
100 IF LEN(A$) = 0 THEN GOTO 90 Do you want more stars? no
110 A$ = LEFT$(A$, 1) Goodbye Mike
120 IF A$ = "Y" OR A$ = "y" THEN GOTO 30
130 PRINT "Goodbye "; U$
140 END

Page 2 - 8
Introduction to Computer Programming
© copyright Saharudin Haron, UTM

What is a Program
Example of C program:
#include <stdio.h>
#include <stdlib.h>
#include "pipe.h" The resulting dialog might resemble:
int main()
{ Friction Factor for smooth pipe.
char try;
double Nre, f; Enter the Reynolds Number ( >2100 ): 2000
do{
system("cls");; The friction factor is 0.004064
printf("Friction Factor for smooth pipe.\n");
printf("\nEnter the Reynolds Number ( >2100 ): "); Do you want to try again? (Y/N)
scanf("%lf",&Nre);
f=friction(Nre);
printf("\nThe friction factor is %lf\n",f);
printf("\nDo you want to try again? (Y/N)\n");
try=getch();
}while(try=='y'||try=='Y');
return 0;
}

Page 2 - 9

Das könnte Ihnen auch gefallen