Professional Documents
Culture Documents
sg/~cs1010/
Week1 - 2
Computing Fundamentals
Monitor and speaker (output) Houses processor, memory, buses, etc.
Software (1/4)
Program
Sequence of instruction that tells a computer what to do Performing the instruction sequence Language for writing instructions to a computer Machine language or object code Assembly language High-level
Execution
Programming language
Major flavors
Week1 - 4
Software (2/4)
Program
Sequence of instruction that tells a computer what to do Performing the instruction sequence Language for writing instructions to a computer Machine language or object code Program to which computer can Assembly language respond directly. Each instruction High-level
Execution
Programming language
Major flavors
Example: 0001001101101110
CS1010 (AY2013/4 Semester 1)
Week1 - 5
Software (3/4)
Program
Sequence of instruction that tells a computer what to do Performing the instruction sequence Language for writing instructions to a computer Machine language or object code Assembly language Symbolic language High-level for coding machine
Execution
Programming language
Major flavors
language instructions.
Example: ADD A, B, C
Week1 - 6
Software (4/4)
Program
Sequence of instruction that tells a computer what to do Performing the instruction sequence Language for writing instructions to a computer Machine language or object code Assembly language Detailed knowledge of the machine is not required. Uses a vocabulary High-level Examples: Java, C, C++, Prolog, Scheme.
Execution
Programming language
Major flavors
Week1 - 7
Translation
High-level language programs (source codes) must be translated into machine code for execution Translator
Accepts a program written in a source language and translates it to a program in a target language
Compiler
Interpreter
Week1 - 8
produces
Source code
welcome.c
Executable code
Compiler
eg: gcc welcome.c
produces
a.out
Execute
eg: a.out
Output
produces
Week1 - 9
We will do more programming next week. You will need your UNIX account user-name and password. To create SoC UNIX account: https://mysoc.nus.edu.sg/~newacct We will do a quick demonstration now.
Week1 - 10
Week1 - 11
5. Once you log in successfully into your UNIX account, you will see this screen (actual display may vary).
Week1 - 12
Introductory Workshop
Meant for students who did not attend the UNIX workshop conducted by CompClub. Optional for those who have attended the UNIX workshop conducted by CompClub. 16 August is meant for students in all sectional groups except SG31 and SG32. 20 August is meant for students in SG31 and SG32.
Design
Implementation
Testing
Week1 - 14
Week1 - 15
What is the unknown? What are the data? What is the condition? Is it possible to satisfy the condition? Is the condition sufficient to determine the unknown? Draw a figure. Introduce suitable notation.
Week1 - 16
Have you seen the problem before? Do you know a related problem? Look at the unknown. Think of a problem having the same or similar unknown. Split the problem into smaller sub-problems. If you cant solve it, solve a more general version, or a special case, or part of it.
Week1 - 17
Carry out your plan of the solution. Check each step. Can you see clearly that the step is correct? Can you prove that it is correct?
Week1 - 18
Can you check the result? Can you derive the result differently? Can you use the result, or the method, for some other problem?
Week1 - 19
An algorithm is a well-defined computational procedure consisting of a set of instructions, that takes some value or set of values, as input, and produces some value or set of values, as output.
Input
Algorithm
Output
Week1 - 20
Algorithm
Each step of an algorithm must be exact. An algorithm must terminate. An algorithm must be effective. An algorithm must be general.
Exact
Terminate
Effective
General
Week1 - 21
Pseudo-code
sum count 0 max 0
// sum = sum of numbers // count = how many numbers are entered? // max to hold the largest value eventually
for each num entered, count count + 1 sum sum + num The need to indent. if num > max then max num ave sum / count print max, ave Are there any errors in this algorithm?
Week1 - 22
Flowchart
start
Decision box
Yes
ave sum/count
Yes
max num
end
Week1 - 23
Control structures
Sequence Branching Loop
(selection)
(repetition)
Week1 - 24
Data Representation
1 byte = 8 bits We will not deal with bit level and bit operations
Integers: int, short, long Real numbers: float, double Characters: char
A variable holds some data and it belongs to a data type and occupies some memory space, shown as a box in the following slides.
Week1 - 25
Sequence
Variables used:
num2 num3
enter values for num1, num2, num3 ave ( num1 + num2 + num3 ) / 3 print ave
ave
Variables used:
num2 total num3
enter values for num1, num2, num3 total ( num1 + num2 + num3 ) ave total / 3 print ave
ave
Week1 - 26
Sequence
Week1 - 27
Selection
final1
final2
// Transfer values in final1, final2 back to num1, num2 num1 final1 num2 final2
// Display sorted integers print num1, num2
CS1010 (AY2013/4 Semester 1)
Week1 - 28
Selection
temp
Week1 - 29
Selection
Week1_prog2.c // This program arranges 2 integers in ascending order #include <stdio.h> int main(void) { int num1, num2, temp; printf("Enter 2 integers: "); scanf("%d %d", &num1, &num2); if (num2 < num1) { temp = num1; num1 = num2; num2 = temp; } printf("Sorted: num1 = %d, num2 = %d\n", num1, num2); return 0; }
CS1010 (AY2013/4 Semester 1)
Week1 - 30
Repetition
Example 3: Find the sum of positive integers up to n (assuming that n is a positive integer).
Variables used:
n
count
ans
Week1 - 31
Repetition
Week1_prog3.c // Computes sum of positive integers up to n #include <stdio.h> int main(void) { int n; // upper limit int count=1, ans=0; // initialisation printf("Enter n: "); scanf("%d", &n); while (count <= n) { ans += count; count++; } printf("Sum = %d\n", ans);
return 0;
}
CS1010 (AY2013/4 Semester 1)
Week1 - 32
Euclidean algorithm
1. Let A and B be integers with A > B 0. 2. If B = 0, then the GCD is A and algorithm ends. 3. Otherwise, find q and r such that A = q.B + r where 0 r < B
Week1 - 33
From the examples, we see that in general an algorithm comprises three steps:
Input (read data) Compute (process input data to generate some answers) Output (display answers)
Week1 - 34
Example: Given a value in miles, convert it into kilometres Recall Phase 1 of How To Solve It: Understanding the problem.
Week1 - 35
We can actually stop here, if we are clear about how to do each step. If not, we need to refine the step. For instance, step 2, how do we convert miles to kilometres
1. Read in distance (m) in miles 2. Convert the distance to kilometres 2.1 calculate k = m * 1.609 3. Print the distance (k) in kilometres
Week1 - 36
Week1 - 37
2a
Week1 - 38
Pythagoras theorem:
Area of circle
Week1 - 39
Given these coin denominations: 1, 5, 10, 20, 50, and $1, find the smallest number of coins needed for a given amount. You do not need to list out what coins are used.
Example 1: For 375 cents, 6 coins are needed. Example 2: For 543 cents, 10 coins are needed.
Week1 - 40
Week1 - 41
Example: Given a positive integer n, how do you sum up all its individual digits?
Week1 - 42
The earlier examples show that we can discuss problems and their solutions (algorithms) without writing out the codes. A sample program development process:
Understanding the problem (if in doubt, ask questions!): 5 minutes Writing the algorithm: 30 minutes Testing the algorithm: 20 minutes Writing the program: 20 minutes Testing and debugging the program: 30 minutes to 3 hours or more
For more complex problems, time spent in thinking about the algorithm could far exceed time spent in writing the program. The more time you invest in writing a good algorithm, the more time you will save in debugging your program.
Week1 - 43
Week1 - 44
Problem-solving
Algorithms
Announcements/Things-to-do
Reminders
Discussion classes start in week 3 Check module website and IVLE forums regularly Read IVLE forum about the Intro workshop (only for students who did not attend the one conducted by CompClub; optional if you have attended that) and sign up for it Read CS1010 Student Handbook Revise Chapter 1 Programming Fundamentals Read Chapters 2 and 3 Learn vim and practise using it Create your UNIX account (if you have not done so) and bring along your UNIX password next week
Week1 - 46
End of File