process of programming: step 2
TRANSCRIPT
![Page 1: Process of Programming: Step 2](https://reader030.vdocuments.site/reader030/viewer/2022012102/6169f5c811a7b741a34d46b2/html5/thumbnails/1.jpg)
Process of Programming: Step 2Obtain a logical solution to your
problem.A logical solution is a finite and clear
step-by-step procedure to solve your problem. Also called an Algorithm (or recipe).
We can visualize this using a Flowchart. Very important step in the programming
process.
Welcome Esc101, Programming 1
![Page 2: Process of Programming: Step 2](https://reader030.vdocuments.site/reader030/viewer/2022012102/6169f5c811a7b741a34d46b2/html5/thumbnails/2.jpg)
Welcome Esc101, Programming
Friendship Algorithm/Flowchart
2
Source: The Big Bang Theory
![Page 3: Process of Programming: Step 2](https://reader030.vdocuments.site/reader030/viewer/2022012102/6169f5c811a7b741a34d46b2/html5/thumbnails/3.jpg)
Flowchart to depict algorithm Start
Sum = 0
N < 1?
End
I = 1 Sum = Sum + I I = I + 1 I <= N ?
Yes
NoYes
No
For calculating 1+2+…+N
Too Detailed? Computers need precise instructions
Input N
Print Sum
![Page 4: Process of Programming: Step 2](https://reader030.vdocuments.site/reader030/viewer/2022012102/6169f5c811a7b741a34d46b2/html5/thumbnails/4.jpg)
Algorithms in real-life
Any step-by-step guide. e.g. Assembly instructions for a make-it-yourself kit.
Welcome Esc101, Programming 4
http://www.gocomics.com/calvinandhobbes/2009/06/02
![Page 5: Process of Programming: Step 2](https://reader030.vdocuments.site/reader030/viewer/2022012102/6169f5c811a7b741a34d46b2/html5/thumbnails/5.jpg)
At least for this course…
Welcome Esc101, Programming 5
What is “NOT” a computer
![Page 6: Process of Programming: Step 2](https://reader030.vdocuments.site/reader030/viewer/2022012102/6169f5c811a7b741a34d46b2/html5/thumbnails/6.jpg)
Welcome Esc101, Programming 6
Some famous Computers
IBM Deep Blue
![Page 7: Process of Programming: Step 2](https://reader030.vdocuments.site/reader030/viewer/2022012102/6169f5c811a7b741a34d46b2/html5/thumbnails/7.jpg)
Welcome Esc101, Programming 7
Some famous Computers
IBM Watson
![Page 8: Process of Programming: Step 2](https://reader030.vdocuments.site/reader030/viewer/2022012102/6169f5c811a7b741a34d46b2/html5/thumbnails/8.jpg)
Welcome Esc101, Programming 8
Some famous Computers
IBM Watson
![Page 9: Process of Programming: Step 2](https://reader030.vdocuments.site/reader030/viewer/2022012102/6169f5c811a7b741a34d46b2/html5/thumbnails/9.jpg)
ESC101: Introduction to Computing
Overview of Programming
7/31/2015 Esc101, Programming 9
![Page 10: Process of Programming: Step 2](https://reader030.vdocuments.site/reader030/viewer/2022012102/6169f5c811a7b741a34d46b2/html5/thumbnails/10.jpg)
GCD
An algorithm to find the greatest common divisor of two positive integers m and n, m ≥ n.A naïve solution – Described informally as follows. 1. Take the smaller number n.2. For each number k, n ≥k≥1, in
descending order, do the following.a) If k divides m and n, then k is the gcd
of m and n
7/31/2015Esc101,
Programming10
![Page 11: Process of Programming: Step 2](https://reader030.vdocuments.site/reader030/viewer/2022012102/6169f5c811a7b741a34d46b2/html5/thumbnails/11.jpg)
GCD
This will compute gcd correctly, but is VERY slow (think about large numbers m and n=m-1).There is a faster way…
7/31/2015Esc101,
Programming11
![Page 12: Process of Programming: Step 2](https://reader030.vdocuments.site/reader030/viewer/2022012102/6169f5c811a7b741a34d46b2/html5/thumbnails/12.jpg)
GCD Algorithm - IntuitionTo find gcd of 8 and 6. • Consider rods of length 8 and 6.• Measure the longer with the
shorter. • Take the remainder if any. • Repeat the process until the longer
can be exactly measured as an integer multiple of the shorter.
7/31/2015Esc101,
Programming12
![Page 13: Process of Programming: Step 2](https://reader030.vdocuments.site/reader030/viewer/2022012102/6169f5c811a7b741a34d46b2/html5/thumbnails/13.jpg)
GCD Algorithm - Intuition
Remainder
8
6
2
Gcd(8, 6) = 2.
7/31/2015Esc101,
Programming13
![Page 14: Process of Programming: Step 2](https://reader030.vdocuments.site/reader030/viewer/2022012102/6169f5c811a7b741a34d46b2/html5/thumbnails/14.jpg)
GCD Algorithm - Intuition102
21
18
3
102 mod 21 = 18
21 mod 18 = 3
Gcd (102, 21) = 3
7/31/2015Esc101,
Programming14
![Page 15: Process of Programming: Step 2](https://reader030.vdocuments.site/reader030/viewer/2022012102/6169f5c811a7b741a34d46b2/html5/thumbnails/15.jpg)
Euclid’s method for gcd
• Suppose a > b. Then the gcd of a and b is the same as the gcd of b and the remainder of a when divided by b.
gcd(a,b) = gcd (b, a % b)Proof:Exercise
7/31/2015Esc101,
Programming15
![Page 16: Process of Programming: Step 2](https://reader030.vdocuments.site/reader030/viewer/2022012102/6169f5c811a7b741a34d46b2/html5/thumbnails/16.jpg)
7/31/2015
GCD Algorithm
Esc101, Programming16
![Page 17: Process of Programming: Step 2](https://reader030.vdocuments.site/reader030/viewer/2022012102/6169f5c811a7b741a34d46b2/html5/thumbnails/17.jpg)
Overview of Programming
Using C
7/31/2015 17Esc101, Programming
![Page 18: Process of Programming: Step 2](https://reader030.vdocuments.site/reader030/viewer/2022012102/6169f5c811a7b741a34d46b2/html5/thumbnails/18.jpg)
The Programming Cycle1. Write your program or edit (i.e., change or modify) your
program.2. Compile your program. If compilation fails, return to editing
step. 3. Run your program on an input. If output is not correct, return
to editing step. a. Repeat step 3 for other inputs, if any.
Write/Edit RunCompile Compilation Errors ?
NO
OK?
YES
NOA simple development cycle of a program
YESMoreInputs
YES!!DONEInputs
Exhausted7/31/2015 Esc101, Programming 18
![Page 19: Process of Programming: Step 2](https://reader030.vdocuments.site/reader030/viewer/2022012102/6169f5c811a7b741a34d46b2/html5/thumbnails/19.jpg)
IDE for Edit-Compile-Run cycle
In this course, you will be using an Integrated Development Environment (IDE). IDE will be available through your browser.
First login to the system. Type in your program in the editor of the IDE. Use the compile button to compile. Run button to run.
The labs in the first week will introduce you to the system in more detail.
7/31/2015 Esc101, Programming 19
![Page 20: Process of Programming: Step 2](https://reader030.vdocuments.site/reader030/viewer/2022012102/6169f5c811a7b741a34d46b2/html5/thumbnails/20.jpg)
http
://xk
cd.c
om/3
03/
7/31/2015 Esc101, Programming 20
![Page 21: Process of Programming: Step 2](https://reader030.vdocuments.site/reader030/viewer/2022012102/6169f5c811a7b741a34d46b2/html5/thumbnails/21.jpg)
Why program in high level languages like C
Writing programs in machine language is long, tedious and error-prone.
They are also not portable—meaning program written for one machine may not work on another machine.
Compilers work as a bridge. Take as input a C program and produce an equivalent
machine program.
Compiler for C for a given target machine
C program EquivalentMachine Program on target machine
7/31/2015 Esc101, Programming 21
![Page 22: Process of Programming: Step 2](https://reader030.vdocuments.site/reader030/viewer/2022012102/6169f5c811a7b741a34d46b2/html5/thumbnails/22.jpg)
Simple! Program
Today we will see some of the simplest C programs.
# include <stdio.h>int main () {
printf(“Welcome to ESC101”);return 0;
}
The program prints the message “Welcome to ESC101”
7/31/2015 Esc101, Programming 22
![Page 23: Process of Programming: Step 2](https://reader030.vdocuments.site/reader030/viewer/2022012102/6169f5c811a7b741a34d46b2/html5/thumbnails/23.jpg)
main() is a function. All C programs start by executing from the first statement of the main function.
# include <stdio.h>
int main () {
printf(“Welcome to ESC101”);return 0;
}
Program components1. This tells the C compilerto include the standard input output library.
2. Include this line routinelyas the first line of your C file.
printf is the function called to output from a C program. To print a string, enclose it in “ ” and it gets printed. For now, do not try to print “ itself.
printf(“Welcome to ESC101”); is a statement in C. Statements in C end in semicolon ;
“return” returns the control to the caller (program finishes in this case.)
7/31/2015 Esc101, Programming 23