Download presentation
Presentation is loading. Please wait.
Published byDylan Norman Modified over 8 years ago
2
(Courtesy: Dr. Amitabha Chakrabarty) Lesson - 2 Number System & Program Design CSE 101
3
2 This Lesson Includes Following section Number SystemProgram Design & AlgorithmFlow Chart
4
3 Number System How Computers Represent Data Binary Numbers The Binary Number System Bits and Bytes Text Codes Binary Number Computer processing is performed by transistors, which are switches with only two possible states: on and off. All computer data is converted to a series of binary numbers– 1 and 0. For example, you see a sentence as a collection of letters, but the computer sees each letter as a collection of 1s and 0s. If a transistor is assigned a value of 1, it is on. If it has a value of 0, it is off. A computer's transistors can be switched on and off millions of times each second.
5
4 Number System The Binary Number System To convert data into strings of numbers, computers use the binary number system. Humans use the decimal system (“deci” stands for “ten”). Elementory storage units inside computer are electronic switches. Each switch holds one of two states: on (1) or off (0). We use a bit (binary digit), 0 or 1, to represent the state. ON OFF 0 (00) 1 (01) 2 (10) 3 (11) The binary number system works the same way as the decimal system, but has only two available symbols (0 and 1) rather than ten (0, 1, 2, 3, 4, 5, 6, 7, 8, and 9).
6
5 Number System Bits and Bytes A single unit of data is called a bit, having a value of 1 or 0. Computers work with collections of bits, grouping them to represent larger pieces of data, such as letters of the alphabet. Eight bits make up one byte. A byte is the amount of memory needed to store one alphanumeric character. With one byte, the computer can represent one of 256 different symbols or characters. 1 0 1 1 0 0 1 01 0 0 1 0 0 1 01 0 0 1 0 0 1 11 1 1 1
7
6 Number System Text Codes A text code is a system that uses binary numbers (1s and 0s) to represent characters understood by humans (letters and numerals). An early text code system, called EBCDIC (Extended Binary Coded Decimal Interchange Code), uses eight-bit codes, but is used primarily in older mainframe systems. In the most common text-code set, ASCII (American Standard Code for Information Interchange), each character consists of eight bits (one byte) of data. ASCII is used in nearly all personal computers. In the Unicode text-code set, each character consists of 16 bits (two bytes) of data CodeCharacter 00110000 0 00110001 1 00110010 2 00110011 3 00110100 4 00110101 5 01000001 A 01000010 B 01000011 C 01000100 D 01000101 E
8
7 Number System In general, N bits can represent 2 N different values. For M values, bits are needed. 1 bit represents up to 2 values (0 or 1) 2 bits rep. up to 4 values (00, 01, 10 or 11) 3 bits rep. up to 8 values (000, 001, 010. …, 110, 111) 4 bits rep. up to 16 values (0000, 0001, 0010, …, 1111) 32 values requires 5 bits 64 values requires 6 bits 1024 values requires 10 bits 40 values requires 6 bits 100 values requires 7 bits Decimal number system, symbols = { 0, 1, 2, 3, …, 9 } Position is important Example:(7594) 10 = (7x10 3 ) + (5x10 2 ) + (9x10 1 ) + (4x10 0 ) In general, (a n a n-1 … a 0 ) 10 = (a n x 10 n ) + (a n-1 x 10 n-1 ) + … + (a 0 x 10 0 ) (2.75) 10 = (2 x 10 0 ) + (7 x 10 -1 ) + (5 x 10 -2 ) In general, (a n a n-1 … a 0. f 1 f 2 … f m ) 10 = (a n x 10 n ) + (a n-1 x10 n-1 ) + … + (a 0 x 10 0 ) + (f 1 x 10 -1 ) + (f 2 x 10 -2 ) + … + (f m x 10 -m )
9
8 Other Number System Binary (base 2): weights in powers-of-2. Binary digits (bits): 0,1. Octal (base 8): weights in powers-of-8. Octal digits: 0,1,2,3,4,5,6,7 Hexadecimal (base 16): weights in powers-of-16. Hexadecimal digits: 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F BinaryOctalDecimalHexadecimal 0000 000 0001111 0010222 0011333 0100444 0101555 0110666 0111777 1000 1088 10011199 10101210A 10111311B 11001412C 11011513D 11101614E 11111715F
10
9 Number System – Base–R to Decimal Conversion ( 1101.101) 2 = 12 3 + 12 2 + 12 0 + 12 -1 + 02 -2 +12 -3 = 8 + 4 + 1 + 0.5 + 0.125 = (13.625) 10 (572.6) 8 = 58 2 + 78 1 + 28 0 + 68 -1 = 320 + 56 + 2 + 0.75 = (378.75) 10 (2A.8) 16 = 216 1 + 1016 0 + 816 -1 = 32 + 10 + 0.5 = (42.5) 10 (341.24) 5 = 35 2 + 45 1 + 15 0 + 25 -1 + 45 -2 = 75 + 20 + 1 + 0.4 + 0.16 = (96.56) 10
11
10 Number System – Decimal to Binary Conversion Method 1: Sum-of-Weights Method Method 2: Repeated Division-by-2 Method (for whole numbers) Repeated Multiplication-by-2 Method (for fractions) Sum-of-Weights Method Determine the set of binary weights whose sum is equal to the decimal number. (9) 10 = 8 + 1 = 2 3 + 2 0 = (1001) 2 (18) 10 = 16 + 2 = 2 4 + 2 1 = (10010) 2 (58) 10 = 32 + 16 + 8 + 2 = 2 5 + 2 4 + 2 3 + 2 1 = (111010) 2 (0.625) 10 = 0.5 + 0.125 = 2 -1 + 2 -3 = (0.101) 2
12
11 Number System – Decimal to Binary Conversion To convert a whole number to binary, use successive division by 2 until the quotient is 0. The remainders form the answer, with the first remainder as the least significant bit (LSB) and the last as the most significant bit (MSB). (43) 10 = (101011) 2 Repeated Multiplication-by-2 Method (for fractions) Repeated Division-by-2 Method (for whole number) To convert decimal fractions to binary, repeated multiplication by 2 is used, until the fractional product is 0 (or until the desired number of decimal places). The carried digits, or carries, produce the answer, with the first carry as the MSB, and the last as the LSB. (0.3125) 10 = (.0101) 2
13
12 Number System - Conversion between Decimal to other Base Decimal to base-R whole numbers: repeated division-by-R fractions: repeated multiplication-by-R In general, conversion between bases can be done via decimal: Base-2Base-3 Base-4DecimalBase-4 … ….Base-R
14
13 Number System - Conversion between Decimal to other Base Octal and Hexadecimal Numbers The conversion of binary, octal and hexadecimal plays an important part in digital computers. Each octal digit corresponds to three digits and each hexadecimal digit corresponds to four binary digits. The conversion from binary to octal is easily accomplished by partitioning the binary number into groups of three each, starting from the binary point and proceeding to the left and to the right. Conversion from binary to hxadecimal is similar. Conversion from the octal or hexadecimal to binary is done by procedure reverse to the above. Binary Octal: Partition in groups of 3 (10 111 011 001. 101 110) 2 = (2731.56) 8 Octal Binary: reverse (2731.56) 8 = (10 111 011 001. 101 110) 2 Binary Hexadecimal: Partition in groups of 4 (101 1101 1001. 1011 1000) 2 = (5D9.B8) 16 Hexadecimal Binary: reverse (5D9.B8) 16 = (101 1101 1001. 1011 1000) 2 Binary-Octal/Hexadecimal Conversion
15
14 Program Design What is a computer program? Simply put, a computer program is a set of detailed directions telling the computer exactly what to do, one step at a time. A program can be as short as one line of code, or as long as several millions lines of code. Typically, a program is stored as a collection of files. Some common file types used in programs are: Executable (.EXE) files actually send commands to the processor. Dynamic Link Library (.DLL) files are partial.EXE files. Initialization (.INI) files contain configuration information for a program. Help (.HLP) files contain information for the user.
16
15 Program Design The Evolution of Programming Languages To build programs, people use languages that are similar to human language. The results are translated into machine code, which computers understand Programming has changed a lot since the first computers were created. They can be categorized based on how close to normal speech they are, and thus how far from the computer's internal language. 1st GL Machine Language (1945) 2nd GL Assembly Language (1950) 3rd GL High Level Language (1960) 4th GL Very High Level Language (1970) 5th GL Natural Language (1980)
17
Assembly Language An assembly language is a low-level programming language for a computer, in which there is a very strong correspondence between the language and the architecture's machine code instructions. Each assembly language is specific to a particular computer architecture, in contrast to most high-level programming languages, which are generally portable across multiple architectures, but require interpreting or compiling.low-level programming languagecomputerarchitecture'smachine code instructionshigh-level programming languagesportableinterpreting compiling
18
High Level Language high-level programming language is a programming language with strong abstraction (technique for managing complexity of computer systems) from the details of the computer. In comparison to low-level programming languages, it may use natural language elements, be easier to use, or may automate significant areas of computing systems, making the process of developing a program simpler and more understandable relative to a lower-level language programming languageabstractioncomputerlow-level programming languagesnatural language
19
Very High Level Language very high-level programming language (VHLL) is a programming language with a very high level of abstraction, used primarily as a professional programmer productivity toolprogramming languageabstraction
20
19 Program Design - Algorithm An algorithm is a sequence of finite instructions, often used for calculation and data processing. Start with a real-world problem that needs to be solved. Convert the real-world problem into a computational problem Develop an algorithm and use it to solve the computational problem An algorithm is a precise list of instructions that determine what operations to perform on what pieces of data, in what order. Lets see How to Cook?
21
20 Program Design - Pseudocode Pseudocode Generic way of describing an algorithm, without use of any specific programming language. It Helps programmers to plan an algorithm. This is not an actual programming language, but may borrow syntax from popular programming languages. Example Problem: Calculate the bill when someone buys a specific number of some item: Pseudocode: PROMT for number of items being purchased READ number of items being purchased PROMPT for price per item READ price per item CALCULATE subtotal CALCULATE tax CALCULATE total DISPLAY total
22
21 Program Design - Pseudocode Example Problem: Calculate two childrens’ allowances, based upon 75 cents per year old. Known ValuesRate = 75 cents per year InputsAges of children CalculationsAllowance = Age x Rate OutputsAllowances for each child Pseudo Code Algorithm: PROMPT for Age of Child1 READ Age of Child1 PROMPT for Age of Child2 READ Age of Child2 CALCULATE Allowance for Child1 = Age of Child1 x Rate CALCULATE Allowance for Child2 = Age of Child2 x Rate DISPLAY Allowance for Child1 DISPLAY Allowance for Child2
23
22 Program Design - Pseudocode Sequential: With sequential program statements, you execute one statement after another (like steps in a list) After step X is completed, step X+1 will be performed, until the last statement has been executed. Every step in the list will be performed. Each step in the list will be performed once and only once.
24
23 Program Design - Pseudocode Non - Sequential: Programs that execute only sequential program statements are pretty simplistic. Most programs need more flexibility in the order of statement execution. The order of statement execution is called the flow of control. Control statements allow the execution of statements based upon decisions.
25
24 Program Design - Pseudocode Conditional statements: Decide whether or not to execute a particular statement or statements Also called the selection statements or decision statements. Pseudo code Example: IF Hours Worked over 40 THEN DISPLAY overtime pay ELSE DISPLAY regular pay Loop statements: Repeatedly execute the same statement(s) for a certain number of times or until a test condition is satisfied. Pseudo code Example: WHILE Population UNDER Limit DO COMPUTE Population = Population + Births – Deaths
26
25 Flow Chart - Flow Control Design Tool Flowchart - a graphical way of writing algorithms Rectangle is used for calculations Parallelogram is used for input and output Circle is used as connector Diamond is used as decision Symbols are connected by arrows to represent the order of the operations
27
26 Flow Chart – Flow Control Symbol Process Alternet Process Decision Data Predefined Process Internal Storage Terminator Document Manual Input Manual Operation Connector Display Stored Data Extract - Marge
28
27 Flow Chart – Flow Control Symbol Every solution starts somewhere and terminates somewhere Every flowchart must have one start symbol and one end symbol Start and end symbols are ovals A start symbol denotes the start of the algorithm An end symbol indicates the algorithm has terminated Start Stop Solution
29
28 Flow Chart – Flow Control Symbol Build an application that asks User to input two numbers. Then Calculates the sum of the two numbers Outputs the sum. Input first_number Input second_number Sum = first_number + second_number Output Sum Stop
30
29 Flow Chart – Flow Control Symbol Allow1 = Age1 x Rate input Age1 start Print Allow1 stop prompt Age1 Allow2 = Age2 x Rate Print Allow2 input Age2 prompt Age2 Previous Pseudocode Algorithm: 1.READ Age of Child1 2.PROMPT for Age of Child1 3.READ Age of Child2 4.PROMPT for Age of Child2 5.CALCULATE Allowance for Child1 = Age of Child1 x Rate 6.CALCULATE Allowance for Child2 = Age of Child2 x Rate 7.DISPLAY Allowance for Child1 8.DISPLAY Allowance for Child2 Calculate two children‘s allowances, based upon 75 cents per year old.
31
30 Flow Chart – Flow Control Symbol Start Input first_number Input second_number Stop Is first_number greater than second_number? Output first_number is bigger Output second_number is bigger YESNO Take 2 number from user and find out which one is a bigger number
32
31 Flow Chart – Flow Control Symbol Start Input second_number Stop Is first_number greater than second_number? Output Sum & first_number is bigger Output Sum & second_number is bigger TRUEFALSE Sum = first_number + second_number Input first_number
33
32 Flow Chart – Flow Control Symbol Lets think about this … Write a program to output the sum of 1 to 10. That is, 1 + 2 + 3 + 4 + 5 + 6 +7 +8 +9 +10 Start Sum =0 Number = 1 Sum = Sum + Number Is Number < 10? Number = Number +1 True Output Sum Stop False
34
33 Flow Chart – Flow Control Symbol Previous Pseudocode Algorithm: –PROMPT for Age –READ Age –IF Age less than 10 –THEN CALCULATE Allowance = Age x YoungRate –ELSE CALCULATE Allowance = Age x OlderRate –DISPLAY Allowance Allow = Age x YoungRate start Print Allow stop prompt Age Allow = Age x OlderRate Age < 10 FALSETRUE input Age Calculate one child’s allowance, based upon 75 cents per year old if s/he is under 10, and $1 per year if 10 or over.
35
34 Flow Chart – Flow Control Symbol Previous Pseudo code Algorithm: –Set KidsPaid to 0 –Set Total to 0 –WHILE KidsPaid < 3 DO –PROMPT for Age –READ Age –CALCULATE Allowance = Age x Rate –ADD Allowance to Total –INCREMENT KidsPaid –DISPLAY Total start Display Total stop Read Age Calc Allowance KidsPaid < 3 FALSETRUE KidsPaid = 0 Total = 0 Add Allowance to Total Increment KidsPaid Prompt Age Calculate the total allowance paid to each of three children at $1 per year old.
36
35 Flow Chart – Flow Control Symbol 1. Start 2. Input N 3. Set Count = 1 4. If Count <= N 4a. Yes go to Step 5 4b. No, go to Step 7 5. Print Count 6. Count = Count +2, Go to Step 4 7. End Write pseudo code and draw flowchart for printing Sequence:1,3,5,7,…. N Start Input N I = 1 When I<=N Output I I = I+ 2 End
37
36 Flow Chart – Flow Control Symbol 1. Start 2. Input N 3. Set Count = 2 4. If Count <= N 4a. Yes go to Step 5 4b. No, go to Step 7 5. Print Count 6. Count = Count +2, Go to Step 4 7. End Write pseudo code and draw flowchart for printing Sequence:2,4,6,8,…. N Start Input N I = 2 When I<=N Output I I = I+ 2 End
38
37 Flow Chart – Flow Control Symbol 1. Start 2. Input N 3. Set Count = 1 4. If Count <= N 4a. Yes go to Step 5 4b. No, go to Step 7 5. Print Count, Count*(-1) 6. Count = Count +1, Go to Step 4 7. End Write pseudo code and draw flowchart for printing Sequence:1,-1,2,-2,3,-3,…. N,-N Start Input N I = 1 When I<=N Output I, I*-1 I = I+ 1 End
39
38 Flow Chart – Flow Control Symbol 1. Start 2. Input N 3. Set Count = 1 4. If Count <= N 4a. Yes go to Step 5 4b. No, go to Step 7 5. Print Count/Count*2 6. Count = Count +1, Go to Step 4 7. End Write pseudo code and draw flowchart for printing Sequence:1/2,2/4,3/6,4/8, N/2N Start Input N I = 1 When I<=N Output I/I*2 I = I+ 1 End
40
39 Flow Chart – Flow Control Symbol 1. Start 2. Input N 3. Set Count = 1, S=1 4. If Count <= N 4a. Yes go to Step 5 4b. No, go to Step 7 5. Print Count*S 6. Count = Count +2, S=S*(-1), Go to Step 4 7. End Write pseudo code and draw flowchart for printing Sequence:1,-3,5,-7,…..n Start Input N I = 1, S=1 When I<=N Output I*S I = I+ 2, S=S*(-1) End
41
40 Flow Chart – Flow Control Symbol 1. Start 2. Input N 3. Set Count = 2, S=1 4. If Count <= N 4a. Yes go to Step 5 4b. No, go to Step 7 5. Print Count*S 6. Count = Count +2, S=S*(-1), Go to Step 4 7. End Write pseudo code and draw flowchart for printing Sequence:2,-4,6,-8,…..n Start Input N I = 2, S=1 When I<=N Output I*S I = I+ 2, S=S*(-1) End
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.