1 / 53

Chapter 6: Low-Level Programming Languages

Chapter 6: Low-Level Programming Languages. In order to execute instructions on a CPU, those instructions must be in the particular binary format that was designed for that CPU. The set of instructions available for a particular CPU is known as its machine language .

tass
Download Presentation

Chapter 6: Low-Level Programming Languages

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 6: Low-Level Programming Languages In order to execute instructions on a CPU, those instructions must be in the particular binary format that was designed for that CPU. The set of instructions available for a particular CPU is known as its machine language. Sample Machine Instruction Format Op-Code Field (specifies the operation to be performed) Operand Field (gives further details pertinent to the operation) Chapter 6 Low-Level Programming Languages Page 51

  2. Simplified Machine Language Note that every instruction is sixteen bits (four hexadecimal digits) in length. Chapter 6 Low-Level Programming Languages Page 52

  3. Sample Program 205C Load register 0 with the integer 92 (hexadecimal 5C) 300E Store the contents of register 0 at main memory address 0E 205A Load register 0 with the integer 90 (hexadecimal 5A) 300F Store the contents of register 0 at main memory address 0F 110E Load register 1 with the bit pattern at main memory address 0E 120F Load register 2 with the bit pattern at main memory address 0F 5012 Add the contents of registers 1 & 2 and put the sum in register 0 300D Store the contents of register 0 at memory address 0D C000 Halt execution In an advanced language, like C++, this program would be: void main() { intX, Y, Z; X = 92; Y = 90; Z = X + Y; } Chapter 6 Low-Level Programming Languages Page 53

  4. Another Sample Program How would we code this pseudocode with our machine language? Procedure negative (x) If x < 0 Then return 1 Else return 0 We’ll assume that the value of xhas already been stored in main memory at address 5A, that the returned value (0 or 1) will be placed at address 5B, and that the program itself will be stored starting at address D0. Chapter 6 Low-Level Programming Languages Page 54

  5. Let’s take advantage of the fact that if an 8-bit two’s-complement number is ANDed with 10000000, the result is 00000000 if the number is positive, and 10000000 if the number is negative. Procedure negative (x) If x < 0 Then return 1 Else return 0 Chapter 6 Low-Level Programming Languages Page 55

  6. Assembly Language To make programming easier to handle, special languages (unique to the kinds of computers running them) have been developed. Programs written in such an assembly language are executed by first passing through a special program (called an assembler) that translates the assembly language code into machine language. • Example: • LOADVAR R1,X • LOADHEX R0,80 • AND R2,R0,R1 • JUMPEQ R0,R2,NEGATIVE • LOADHEX R3,00 • JUMPEQ R0,R0,STOREIT • NEGATIVE LOADHEX R3,01 • STOREIT STORE R3, RESULT • HALT Chapter 6 Low-Level Programming Languages Page 56

  7. Assembly Language Problems While assembly languages are easier to use than machine languages, they still share two big problems with machine languages. 1. Assembly languages are still machine-dependent. A program written in one machine’s assembly language cannot be executed on a computer with a different instruction set and register configuration. 2. Assembly language programming is still too nitpicky. Programmers are forced to concentrate on the tiny details necessary to choreograph the computer’s activity, instead of focusing on the overall solution of the problem being tackled. Chapter 6 Low-Level Programming Languages Page 57

  8. Chapter 7: Problem Solving and Algorithm Design In general, how are problems solved on a computer? Chapter 7 Problem Solving and Algorithm Design Page 58

  9. Algorithms An algorithm is an ordered set of unambiguous, executable steps that ultimately terminate if followed. Ambiguous: Not executable: • Lather Chapter 7 Problem Solving and Algorithm Design Page 59 No termination: • Rinse • Repeat

  10. Computer Algorithms In computer programming, an algorithm is the sequence of steps (i.e., the “recipe”) for accomplishing a task. • Every step in an algorithm has two basic components: • Semantics: The meaning of the step • Syntax: The format of the step Chapter 7 Problem Solving and Algorithm Design Page 60

  11. Pseudocode Pseudocode is an informal notation for expressing an algorithm. Procedure Sat1231A Set year to 2001 Set month to January Set day to first Saturday in January 2001 While (year < 2021) Do { Increment day by 7 If date is New Year’s Eve Then display year as having a Saturday New Year’s Eve If day > (number of days in month) Then { Adjust day by subtracting the number of days in month If month is December Then { Increment year by 1 Set month to January } Else Increment month by one } } Example: Which years in 2001-2020 have New Year’s Eve on Saturday? Chapter 7 Problem Solving and Algorithm Design Page 61

  12. Algorithm Alternatives It’s possible to devise many algorithms to solve the same problem. Procedure Sat1231B Set day_of_week to 12/31/2000 Set year to 2001 While (year < 2021) Do { If year is a leap year Then increment day_of_week by 2 Else increment day_of_week by 1 If day_of_week is Saturday Then display year as having a Saturday New Year’s Eve Increment year by 1 } Alternative pseudocode to determine which years in 2001-2020 have New Year’s Eve on Saturday. Both algorithms work, but which is better? Which is easier to code? Which runs more efficiently? Chapter 7 Problem Solving and Algorithm Design Page 62

  13. Program Modularity The software development process usually involves a team of developers, so the software is often designed as a hierarchical system of modules, which can be viewed as easily modified interdependent entities. Chapter 7 Problem Solving and Algorithm Design Page 63

  14. Advantages of Modular Programming Chapter 7 Problem Solving and Algorithm Design Page 64

  15. Top-Down Design One common approach for designing programs is the top-down methodology. • Design a high-level solution to the programming problem. Consider each complicated subproblem as a separate programming problem. Return to step #1 with each subproblem. • This approach lends itself to modularity. • However, it may impose an artificial hierarchy on the tasks being performed. Chapter 7 Problem Solving and Algorithm Design Page 65

  16. Bottom-Up Design A newer approach for designing programs is the bottom-up methodology. • Separate each major task in the overall problem into an individual programming problem. Develop cooperative programming solutions to the separate problems. • This approach produces reusable modules. • However, those modules may prove difficult to cobble together to solve bigger problems. Chapter 7 Problem Solving and Algorithm Design Page 66

  17. Chapter 8: Abstract Data Types and Algorithms Two keys to making computer software that works well: • Organize data so it can be accessed and processed efficiently. • Develop algorithms that take advantage of the strengths of the programming language and the hardware to accomplish what the program is attempting to do. Chapter 8 Abstract Data Types and Algorithms Page 67

  18. Iteration When an algorithm involves repetitive actions, iteration(i.e., looping) may be a practical approach. Pseudocode to implement the search for a specific name in an alphabetized phonebook: Procedure SeqSearch(phonebook, sought_name) Set test_name to first name in phonebook While (test_name is alphabetically before sought_name AND there are still more names in phonebook) Do Set test_name to the next name in phonebook If test_name is sought_name Then return the corresponding phone number Else return “Unlisted” message Notice that this algorithm always starts at the top of the phonebook list and checks each name against sought_name until it either locates it or (if it’s not in the phonebook) passes it. Chapter 8 Abstract Data Types and Algorithms Page 68

  19. Name Name Name Name Name Name Name Name Name Number Number Number Number Number Number Number Number Number Black, Sirius Chang, Cho Dumbledore, Albus Dursley, Dudley Filch, Argus Fudge, Cornelius Granger, Hermione Hagrid, Rubeus Lockhart, Gilderoy Longbottom, Neville Malfoy, Draco McGonagall, Minerva Pettigrew, Peter Pomfrey, Poppy Snape, Severus Trelawney, Sybill Weasley, Ron Wood, Oliver Black, Sirius Chang, Cho Dumbledore, Albus Dursley, Dudley Filch, Argus Fudge, Cornelius Granger, Hermione Hagrid, Rubeus Lockhart, Gilderoy Longbottom, Neville Malfoy, Draco McGonagall, Minerva Pettigrew, Peter Pomfrey, Poppy Snape, Severus Trelawney, Sybill Weasley, Ron Wood, Oliver Black, Sirius Chang, Cho Dumbledore, Albus Dursley, Dudley Filch, Argus Fudge, Cornelius Granger, Hermione Hagrid, Rubeus Lockhart, Gilderoy Longbottom, Neville Malfoy, Draco McGonagall, Minerva Pettigrew, Peter Pomfrey, Poppy Snape, Severus Trelawney, Sybill Weasley, Ron Wood, Oliver Black, Sirius Chang, Cho Dumbledore, Albus Dursley, Dudley Filch, Argus Fudge, Cornelius Granger, Hermione Hagrid, Rubeus Lockhart, Gilderoy Longbottom, Neville Malfoy, Draco McGonagall, Minerva Pettigrew, Peter Pomfrey, Poppy Snape, Severus Trelawney, Sybill Weasley, Ron Wood, Oliver Black, Sirius Chang, Cho Dumbledore, Albus Dursley, Dudley Filch, Argus Fudge, Cornelius Granger, Hermione Hagrid, Rubeus Lockhart, Gilderoy Longbottom, Neville Malfoy, Draco McGonagall, Minerva Pettigrew, Peter Pomfrey, Poppy Snape, Severus Trelawney, Sybill Weasley, Ron Wood, Oliver Black, Sirius Chang, Cho Dumbledore, Albus Dursley, Dudley Filch, Argus Fudge, Cornelius Granger, Hermione Hagrid, Rubeus Lockhart, Gilderoy Longbottom, Neville Malfoy, Draco McGonagall, Minerva Pettigrew, Peter Pomfrey, Poppy Snape, Severus Trelawney, Sybill Weasley, Ron Wood, Oliver Black, Sirius Chang, Cho Dumbledore, Albus Dursley, Dudley Filch, Argus Fudge, Cornelius Granger, Hermione Hagrid, Rubeus Lockhart, Gilderoy Longbottom, Neville Malfoy, Draco McGonagall, Minerva Pettigrew, Peter Pomfrey, Poppy Snape, Severus Trelawney, Sybill Weasley, Ron Wood, Oliver Black, Sirius Chang, Cho Dumbledore, Albus Dursley, Dudley Filch, Argus Fudge, Cornelius Granger, Hermione Hagrid, Rubeus Lockhart, Gilderoy Longbottom, Neville Malfoy, Draco McGonagall, Minerva Pettigrew, Peter Pomfrey, Poppy Snape, Severus Trelawney, Sybill Weasley, Ron Wood, Oliver Black, Sirius Chang, Cho Dumbledore, Albus Dursley, Dudley Filch, Argus Fudge, Cornelius Granger, Hermione Hagrid, Rubeus Lockhart, Gilderoy Longbottom, Neville Malfoy, Draco McGonagall, Minerva Pettigrew, Peter Pomfrey, Poppy Snape, Severus Trelawney, Sybill Weasley, Ron Wood, Oliver 555-7458 555-0131 555-3589 555-1119 555-3783 555-9927 555-2728 555-1317 555-1201 555-7936 555-7174 555-1659 555-2941 555-1503 555-8847 555-6296 555-5165 555-6793 555-7458 555-0131 555-3589 555-1119 555-3783 555-9927 555-2728 555-1317 555-1201 555-7936 555-7174 555-1659 555-2941 555-1503 555-8847 555-6296 555-5165 555-6793 555-7458 555-0131 555-3589 555-1119 555-3783 555-9927 555-2728 555-1317 555-1201 555-7936 555-7174 555-1659 555-2941 555-1503 555-8847 555-6296 555-5165 555-6793 555-7458 555-0131 555-3589 555-1119 555-3783 555-9927 555-2728 555-1317 555-1201 555-7936 555-7174 555-1659 555-2941 555-1503 555-8847 555-6296 555-5165 555-6793 555-7458 555-0131 555-3589 555-1119 555-3783 555-9927 555-2728 555-1317 555-1201 555-7936 555-7174 555-1659 555-2941 555-1503 555-8847 555-6296 555-5165 555-6793 555-7458 555-0131 555-3589 555-1119 555-3783 555-9927 555-2728 555-1317 555-1201 555-7936 555-7174 555-1659 555-2941 555-1503 555-8847 555-6296 555-5165 555-6793 555-7458 555-0131 555-3589 555-1119 555-3783 555-9927 555-2728 555-1317 555-1201 555-7936 555-7174 555-1659 555-2941 555-1503 555-8847 555-6296 555-5165 555-6793 555-7458 555-0131 555-3589 555-1119 555-3783 555-9927 555-2728 555-1317 555-1201 555-7936 555-7174 555-1659 555-2941 555-1503 555-8847 555-6296 555-5165 555-6793 555-7458 555-0131 555-3589 555-1119 555-3783 555-9927 555-2728 555-1317 555-1201 555-7936 555-7174 555-1659 555-2941 555-1503 555-8847 555-6296 555-5165 555-6793 Calling SeqSearch(phonebook, sought_name) where sought_name is “RubeusHagrid” and phonebook is the list below: test_nameis Sirius Black, so iterate again test_nameis Cho Chang, so iterate again test_nameis Albus Dumbledore, so iterate again test_nameis Dudley Dursley, so iterate again test_nameis Argus Filch, so iterate again test_nameis Cornelius Fudge, so iterate again test_nameis Hermione Granger, so iterate again test_nameis RubeusHagrid, so return 555-1317 Chapter 8 Abstract Data Types and Algorithms Page 69

  20. Recursion Another common approach in algorithms is to employ recursion (i.e., “divide and conquer”), which repeatedly reduces the size of a problem until it becomes manageable. Pseudocode to recursively take a base number to a specified power: Procedure Exponentiate(base, power) If base is 0 Then return 0 Else If power < 0 Then return Exponentiate(base, power+1)/base Else If power is 0 Then return 1 Else return base * Exponentiate(base, power-1) Notice that this algorithm returns 0 if the value of base is 0, 1 if the value of power is 0, base if the value of power is 1, 1/base if the value of power is -1, and so on. Chapter 8 Abstract Data Types and Algorithms Page 70

  21. A Recursive Search Algorithm Pseudocode to recursively implement the search for a specific name in an alphabetized phonebook: Procedure BinarySearch(phonebook, sought_name) Set test_name to the middle name in phonebook If test_name is sought_name Then return corresponding phone number Else If phonebook has only one remaining entry Then return “Unlisted” message If test_name is alphabetically before sought_name Then apply BinarySearch to the portion of phonebook after test_name Else apply BinarySearch to the portion of phonebook before test_name Notice that this algorithm starts at the middle of the phonebook list, and keeps splitting what’s left of the phonebook in half until it either locates sought_name or runs out of names to check. Chapter 8 Abstract Data Types and Algorithms Page 71

  22. Name Name Name Name Name Name Name Name Name Name Number Number Number Number Number Number Number Number Number Number Black, Sirius Chang, Cho Dumbledore, Albus Dursley, Dudley Filch, Argus Fudge, Cornelius Granger, Hermione Hagrid, Rubeus Lockhart, Gilderoy Longbottom, Neville Malfoy, Draco McGonagall, Minerva Pettigrew, Peter Pomfrey, Poppy Snape, Severus Trelawney, Sybill Weasley, Ron Wood, Oliver Black, Sirius Chang, Cho Dumbledore, Albus Dursley, Dudley Filch, Argus Fudge, Cornelius Granger, Hermione Hagrid, Rubeus Lockhart, Gilderoy Longbottom, Neville Malfoy, Draco McGonagall, Minerva Pettigrew, Peter Pomfrey, Poppy Snape, Severus Trelawney, Sybill Weasley, Ron Wood, Oliver Black, Sirius Chang, Cho Dumbledore, Albus Dursley, Dudley Filch, Argus Fudge, Cornelius Granger, Hermione Hagrid, Rubeus Lockhart, Gilderoy Longbottom, Neville Malfoy, Draco McGonagall, Minerva Pettigrew, Peter Pomfrey, Poppy Snape, Severus Trelawney, Sybill Weasley, Ron Wood, Oliver Black, Sirius Chang, Cho Dumbledore, Albus Dursley, Dudley Filch, Argus Fudge, Cornelius Granger, Hermione Hagrid, Rubeus Lockhart, Gilderoy Longbottom, Neville Malfoy, Draco McGonagall, Minerva Pettigrew, Peter Pomfrey, Poppy Snape, Severus Trelawney, Sybill Weasley, Ron Wood, Oliver Black, Sirius Chang, Cho Dumbledore, Albus Dursley, Dudley Filch, Argus Fudge, Cornelius Granger, Hermione Hagrid, Rubeus Lockhart, Gilderoy Longbottom, Neville Malfoy, Draco McGonagall, Minerva Pettigrew, Peter Pomfrey, Poppy Snape, Severus Trelawney, Sybill Weasley, Ron Wood, Oliver Black, Sirius Chang, Cho Dumbledore, Albus Dursley, Dudley Filch, Argus Fudge, Cornelius Granger, Hermione Hagrid, Rubeus Lockhart, Gilderoy Longbottom, Neville Malfoy, Draco McGonagall, Minerva Pettigrew, Peter Pomfrey, Poppy Snape, Severus Trelawney, Sybill Weasley, Ron Wood, Oliver Black, Sirius Chang, Cho Dumbledore, Albus Dursley, Dudley Filch, Argus Fudge, Cornelius Granger, Hermione Hagrid, Rubeus Lockhart, Gilderoy Longbottom, Neville Malfoy, Draco McGonagall, Minerva Pettigrew, Peter Pomfrey, Poppy Snape, Severus Trelawney, Sybill Weasley, Ron Wood, Oliver Black, Sirius Chang, Cho Dumbledore, Albus Dursley, Dudley Filch, Argus Fudge, Cornelius Granger, Hermione Hagrid, Rubeus Lockhart, Gilderoy Longbottom, Neville Malfoy, Draco McGonagall, Minerva Pettigrew, Peter Pomfrey, Poppy Snape, Severus Trelawney, Sybill Weasley, Ron Wood, Oliver Black, Sirius Chang, Cho Dumbledore, Albus Dursley, Dudley Filch, Argus Fudge, Cornelius Granger, Hermione Hagrid, Rubeus Lockhart, Gilderoy Longbottom, Neville Malfoy, Draco McGonagall, Minerva Pettigrew, Peter Pomfrey, Poppy Snape, Severus Trelawney, Sybill Weasley, Ron Wood, Oliver Black, Sirius Chang, Cho Dumbledore, Albus Dursley, Dudley Filch, Argus Fudge, Cornelius Granger, Hermione Hagrid, Rubeus Lockhart, Gilderoy Longbottom, Neville Malfoy, Draco McGonagall, Minerva Pettigrew, Peter Pomfrey, Poppy Snape, Severus Trelawney, Sybill Weasley, Ron Wood, Oliver 555-7458 555-0131 555-3589 555-1119 555-3783 555-9927 555-2728 555-1317 555-1201 555-7936 555-7174 555-1659 555-2941 555-1503 555-8847 555-6296 555-5165 555-6793 555-7458 555-0131 555-3589 555-1119 555-3783 555-9927 555-2728 555-1317 555-1201 555-7936 555-7174 555-1659 555-2941 555-1503 555-8847 555-6296 555-5165 555-6793 555-7458 555-0131 555-3589 555-1119 555-3783 555-9927 555-2728 555-1317 555-1201 555-7936 555-7174 555-1659 555-2941 555-1503 555-8847 555-6296 555-5165 555-6793 555-7458 555-0131 555-3589 555-1119 555-3783 555-9927 555-2728 555-1317 555-1201 555-7936 555-7174 555-1659 555-2941 555-1503 555-8847 555-6296 555-5165 555-6793 555-7458 555-0131 555-3589 555-1119 555-3783 555-9927 555-2728 555-1317 555-1201 555-7936 555-7174 555-1659 555-2941 555-1503 555-8847 555-6296 555-5165 555-6793 555-7458 555-0131 555-3589 555-1119 555-3783 555-9927 555-2728 555-1317 555-1201 555-7936 555-7174 555-1659 555-2941 555-1503 555-8847 555-6296 555-5165 555-6793 555-7458 555-0131 555-3589 555-1119 555-3783 555-9927 555-2728 555-1317 555-1201 555-7936 555-7174 555-1659 555-2941 555-1503 555-8847 555-6296 555-5165 555-6793 555-7458 555-0131 555-3589 555-1119 555-3783 555-9927 555-2728 555-1317 555-1201 555-7936 555-7174 555-1659 555-2941 555-1503 555-8847 555-6296 555-5165 555-6793 555-7458 555-0131 555-3589 555-1119 555-3783 555-9927 555-2728 555-1317 555-1201 555-7936 555-7174 555-1659 555-2941 555-1503 555-8847 555-6296 555-5165 555-6793 555-7458 555-0131 555-3589 555-1119 555-3783 555-9927 555-2728 555-1317 555-1201 555-7936 555-7174 555-1659 555-2941 555-1503 555-8847 555-6296 555-5165 555-6793 Calling BinarySearch(phonebook, sought_name) where sought_name is “RubeusHagrid” and phonebook is the list below: test_nameis Dudley Dursley, so iterate again test_nameis Cornelius Fudge, so iterate again test_nameis Hermione Granger, so iterate again test_nameis RubeusHagrid, so return 555-1317 test_nameis Gilderoy Lockhart, so iterate again Chapter 8 Abstract Data Types and Algorithms Page 72 Chapter 9 Abstract Data Types and Algorithms Page 77

  23. Data Structures When interrelated information is stored in a computer’s memory, it is usually convenient for the programmer (and for the computer’s memory management) to keep this data in a structured format. However, in the computer’s RAM, space for 100 integers has been allocated something like this: Data Structure #1: The Array An array is an indexed list of values of the same type. Example: intIQlist[100]; Conceptually, the array looks something like this: Chapter 8 Abstract Data Types and Algorithms Page 73

  24. Data Structure #2: The MultidimensionalArray A multidimensional array is an indexed table of values of the same type, using more than one dimension. Example: intGradeTable[3][5]; Conceptually, the array looks something like this: However, in the computer’s RAM, space for 15 integers has been allocated something like this: Chapter 8 Abstract Data Types and Algorithms Page 74

  25. Rather than reserving a contiguous block of memory to store a list, the linked list dynamically allocates memory as needed for list elements. Data Structure #3: The Linked List Example: struct node; typedef node *nodePtr; struct node { int value; nodePtr next; }; nodePtr List; However, in the computer’s RAM, space for 4 integers has been allocated something like this: Conceptually, the linked list looks something like this: Chapter 8 Abstract Data Types and Algorithms Page 75

  26. Relative Advantages of Arrays & Linked Lists Arrays Linked Lists • Require contiguous memory • Dynamically locate memory • Requires specific size • Has flexible size • Potentially wastes memory • Only uses allocated space • Potentially runs out of memory • Expands memory as needed • Insertion requires rearranging • Insertion requires slight relink • Deletion requires rearranging • Deletion requires slight relink • Indexing facilitates searching • One-by-one searching required • Binary search possible if sorted • Sequential search only • Straightforward to program • Tougher to conceptualize • Memory easily cleared after use • Complicated garbage collection Chapter 8 Abstract Data Types and Algorithms Page 76

  27. Comparison: Retrieving a List from a File Using an array Using a linked list void GetList(int List[50], int&ListSize) { ifstream file; char fileName[50]; intval; cout << "Enter the name " << "of the file: "; cin>> fileName; file.open(fileName); assert(!file.fail()); ListSize = 0; file >> val; while ((!file.eof()) && (ListSize < 50)) { List[ListSize] = val; ListSize++; file >> val; } file.close(); } void GetList(int List[50], int&ListSize) { ifstream file; char fileName[50]; intval; cout << "Enter the name " << "of the file: "; cin>> fileName; file.open(fileName); assert(!file.fail()); ListSize = 0; file >> val; while ((!file.eof()) && (ListSize < 50)) { List[ListSize] = val; ListSize++; file >> val; } file.close(); } void GetList(nodePtr &List) { ifstream file; char fileName[50]; intval; nodePtrptr; cout << "Enter the name " << “of the file: "; cin >> fileName; file.open(fileName); assert(!file.fail()); List = NULL; file >> val; while (!file.eof()) { ptr = new node; ptr->value = val; ptr->next = List; List = ptr; file >> val; } file.close(); } void GetList(nodePtr &List) { ifstream file; char fileName[50]; intval; nodePtrptr; cout << "Enter the name " << “of the file: "; cin >> fileName; file.open(fileName); assert(!file.fail()); List = NULL; file >> val; while (!file.eof()) { ptr = new node; ptr->value = val; ptr->next = List; List = ptr; file >> val; } file.close(); } Chapter 8 Abstract Data Types and Algorithms Page 77 Extra concern: Exceeding array’s size Extra concern: Allocating new memory

  28. Comparison: Sequential Search Using an array Using a linked list int Search(int List[50], intListSize, intsoughtVal) { int count; bool found = false; count = 0; while ((!found) && (count < 50)) { if (List[count] == soughtVal) found = true; else count++; } if (found) return List[count]; else return -1; } int Search(nodePtr List, intsoughtVal) { nodePtrcurrPtr; bool found = false; currPtr = List; while ((!found) && (currPtr != NULL)) { if (currPtr->value == soughtVal) found = true; else currPtr = currPtr->next; } if (found) return currPtr->value; else return -1 } Note again that the code is almost identical, but the array version is limited to lists of a certain size. If the list is too long, the array can’t hold it all; if it’s too short, several memory slots are wasted. Chapter 8 Abstract Data Types and Algorithms Page 78

  29. Sorting Algorithms Somewhat more complicated than searching an alphabetized list is the problem of alphabetizing such a list to begin with. Numerous sorting algorithms have been developed, each with its own advantages and disadvantages with respect to: • Speed with which it sorts a completely random list • Speed with which it sorts a nearly sorted list • Amount of memory required to implement it • Ease with which it can be coded Examination of three such algorithms follows, with each algorithm applied to the following list of 26 three-letter names: Moe Edy Zeb Ort Bob Wes Ann Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue Cub Ida Yul Ren Dan Lex Pez Hal Tia Chapter 8 Abstract Data Types and Algorithms Page 79

  30. Selection Sort Let k equal the size of your list Let i equal the index of the first element of your list Swap the smallest element in the last k elements with the ith element Decrease k by one Increase i by one If k is still larger than one, repeat, starting at step #3 Moe Edy Zeb Ort Bob Wes Ann Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue Cub Ida Yul Ren Dan Lex Pez Hal Tia Moe Edy Zeb Ort Bob Wes AnnUma Quo Kit Fly Vin Xon Gus Joe Nan Sue Cub Ida Yul Ren Dan Lex Pez Hal Tia Ann Edy Zeb Ort Bob Wes Moe Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue Cub Ida Yul Ren Dan Lex Pez Hal Tia AnnEdyZeb Ort BobWes Moe Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue Cub Ida Yul Ren Dan Lex Pez Hal Tia AnnBobZeb Ort Edy Wes Moe Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue Cub Ida Yul Ren Dan Lex Pez Hal Tia AnnBob Zeb Ort Edy Wes Moe Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue Cub Ida Yul Ren Dan Lex Pez Hal Tia AnnBobCub Ort Edy Wes Moe Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue Zeb Ida Yul Ren Dan Lex Pez Hal Tia AnnBobCubOrt Edy Wes Moe Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue Zeb Ida Yul Ren Dan Lex Pez Hal Tia AnnBobCubDan Edy Wes Moe Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue Zeb Ida Yul Ren Ort Lex Pez Hal Tia AnnBobCubDanEdy Wes Moe Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue Zeb Ida Yul Ren Ort Lex Pez Hal Tia AnnBobCubDanEdy Wes Moe Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue Zeb Ida Yul Ren Ort Lex Pez Hal Tia AnnBobCubDanEdyWes Moe Uma Quo Kit FlyVin Xon Gus Joe Nan Sue Zeb Ida Yul Ren Ort Lex Pez Hal Tia AnnBobCubDanEdyFly Moe Uma Quo Kit Wes Vin Xon Gus Joe Nan Sue Zeb Ida Yul Ren Ort Lex Pez Hal Tia  AnnBobCubDanEdyFly Gus Hal Ida Joe Kit Lex Moe Nan Ort Pez Quo Ren Sue Tia Uma Vin Wes Xon Yul Zeb Verdict: Easy to program, little memory waste, very inefficient Chapter 8 Abstract Data Types and Algorithms Page 80

  31. Bubble Sort Let k equal the size of your list Let i equal the index of the first element of your list Starting with the ith element of the list and moving down to the kth element, swap every consecutive pair of elements that is in the wrong order Decrease k by one Increase i by one If k is still larger than one, repeat, starting at step #3 Moe Edy Zeb Ort Bob Wes Ann Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue Cub Ida Yul Ren Dan Lex Pez Hal Tia Moe Edy Zeb Ort Bob Wes Ann Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue Cub Ida Yul Ren Dan Lex Pez Hal Tia Edy Moe Zeb Ort Bob Wes Ann Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue Cub Ida Yul Ren Dan Lex Pez Hal Tia Edy Moe Zeb Ort Bob Wes Ann Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue Cub Ida Yul Ren Dan Lex Pez Hal Tia Edy Moe Zeb Ort Bob Wes Ann Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue Cub Ida Yul Ren Dan Lex Pez Hal Tia Edy Moe Zeb Ort Bob Wes Ann Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue Cub Ida Yul Ren Dan Lex Pez Hal Tia Edy Moe Ort Zeb Bob Wes Ann Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue Cub Ida Yul Ren Dan Lex Pez Hal Tia  Edy Moe Ort Bob Wes Ann Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue Cub Ida Yul Ren Dan Lex Pez Hal Tia Zeb  Edy Moe Bob Ort Ann Uma Quo Kit Fly Vin Wes Gus Joe Nan Sue Cub Ida Xon Ren Dan Lex Pez Hal Tia YulZeb  AnnBobCubDanEdyFly Gus Hal Ida Joe Kit Lex Moe Nan Ort Pez Quo Ren Sue Tia Uma Vin Wes Xon Yul Zeb Verdict: Tougher to program, little memory waste, inefficient in general (but could easily be modified to terminate early if a swap-less pass occurs) Chapter 8 Abstract Data Types and Algorithms Page 81

  32. Quick Sort Let leftIndex be the index of the leftmost element of an unsorted portion of the list and rightIndex be the index of the rightmost element of that portion of the list Let pivot equal the value currently at index p of the list Moving in from the rightIndex element of the list, keep moving until a value less than pivot is found; set rightIndex to the index of that value and insert it at position leftIndex Moving in from the leftIndex element of the list, keep moving until a value greater than pivot is found; set leftIndex to the index of that value and insert it at position rightIndex If leftIndex doesn’t equal rightIndex, return to step #3; otherwise, insert pivot at index leftIndex and return to step #1, starting over with another unsorted portion of the list pivot: Moe Moe Edy Zeb Ort Bob Wes Ann Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue Cub Ida Yul Ren Dan Lex Pez Hal Tia Moe Edy Zeb Ort Bob Wes Ann Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue Cub Ida Yul Ren Dan Lex Pez HalTia Hal Edy Lex Dan Bob Wes Ann Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue Cub IdaYul Ren Wes Ort Pez Zeb Tia Hal Edy ZebOrt Bob Wes Ann Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue Cub Ida Yul Ren Dan Lex Pez HalTia Hal Edy ZebOrt Bob Wes Ann Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue Cub Ida Yul Ren Dan Lex Pez Zeb Tia Hal Edy ZebOrt Bob Wes Ann Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue Cub Ida Yul Ren Dan LexPez Zeb Tia Hal Edy Lex Ort Bob Wes Ann Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue Cub Ida Yul Ren Dan LexPez Zeb Tia Hal Edy Lex Dan Bob Wes Ann Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue Cub Ida Yul Ren Wes Ort Pez Zeb Tia Hal Edy Lex Ort Bob Wes Ann Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue Cub Ida Yul Ren Dan Ort Pez Zeb Tia Hal Edy Lex Ort Bob Wes Ann Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue Cub Ida Yul Ren DanOrt Pez Zeb Tia Hal Edy Lex Ort Bob Wes Ann Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue Cub Ida Yul Ren Dan LexPez Zeb Tia Hal Edy Lex Dan Bob Wes Ann Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue Cub Ida Yul Ren DanOrt Pez Zeb Tia Hal Edy Lex Dan Bob Wes Ann Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue Cub Ida Yul Ren DanOrt Pez Zeb Tia Moe Edy Zeb Ort Bob Wes Ann Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue Cub Ida Yul Ren Dan Lex Pez HalTia Hal Edy Lex Dan Bob Ida Ann Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue CubUma Yul Ren Wes Ort Pez Zeb Tia Hal Edy Zeb Ort Bob Wes Ann Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue Cub Ida Yul Ren Dan Lex Pez HalTia Hal Edy Lex Dan Bob Ida Ann Cub Joe Kit Fly Vin Xon Gus Vin Nan Sue Quo Uma Yul Ren Wes Ort Pez Zeb Tia Hal Edy Lex Dan Bob Ida Ann Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue Cub IdaYul Ren Wes Ort Pez Zeb Tia Hal Edy Lex Dan Bob Ida Ann Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue Cub Uma Yul Ren Wes Ort Pez Zeb Tia Hal Edy Lex Dan Bob Ida Ann Cub QuoKit Fly Vin Xon Gus Joe Nan Sue CubUma Yul Ren Wes Ort Pez Zeb Tia Hal Edy Lex Dan Bob Ida Ann Cub QuoKit Fly Vin Xon Gus Joe Nan Sue Quo Uma Yul Ren Wes Ort Pez Zeb Tia Hal Edy Lex Dan Bob Ida Ann Cub QuoKit Fly Vin Xon Gus JoeNan Sue Quo Uma Yul Ren Wes Ort Pez Zeb Tia Hal Edy Lex Dan Bob Ida Ann Cub Joe Kit Fly Vin Xon Gus JoeNan Sue Quo Uma Yul Ren Wes Ort Pez Zeb Tia Hal Edy Lex Dan Bob Ida Ann Cub Joe Kit Fly Vin Xon Gus JoeNan Sue Quo Uma Yul Ren Wes Ort Pez Zeb Tia Hal Edy Lex Dan Bob Ida Ann Cub Quo Kit Fly Vin Xon Gus Joe Nan Sue CubUma Yul Ren Wes Ort Pez Zeb Tia Hal Edy Lex Dan Bob Ida Ann Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue Cub IdaYul Ren Wes Ort Pez Zeb Tia Hal Edy Lex Dan Bob Ida Ann Cub Joe Kit Fly Gus Xon GusVin Nan Sue Quo Uma Yul Ren Wes Ort Pez Zeb Tia Hal Edy Lex Dan Bob Ida Ann Cub Joe Kit Fly Gus XonGusVin Nan Sue Quo Uma Yul Ren Wes Ort Pez Zeb Tia Hal Edy Lex Dan Bob Ida Ann Cub Joe Kit Fly Gus XonXon Vin Nan Sue Quo Uma Yul Ren Wes Ort Pez Zeb Tia Hal Edy Lex Dan Bob Ida Ann Cub Joe Kit Fly Gus Moe Xon Vin Nan Sue Quo Uma Yul Ren Wes Ort Pez Zeb Tia Hal Edy Lex Dan Bob Ida Ann Cub Joe Kit Fly Vin Xon GusVin Nan Sue Quo Uma Yul Ren Wes Ort Pez Zeb Tia  pivot: Hal Gus Edy Fly Dan Bob Cub Ann Hal Joe Kit Ida Lex Moe Xon Vin Nan Sue Quo Uma Yul Ren Wes Ort Pez Zeb Tia HalEdy Lex Dan Bob Ida Ann Cub Joe Kit Fly GusMoe Xon Vin Nan Sue Quo Uma Yul Ren Wes Ort Pez Zeb Tia  pivot: Xon Gus Edy Fly Dan Bob Cub Ann Hal Joe Kit Ida Lex Moe Xon Vin Nan Sue Quo Uma Yul Ren Wes Ort Pez Zeb Tia Gus Edy Fly Dan Bob Cub Ann Hal Joe Kit Ida Lex Moe Tia Vin Nan Sue Quo Uma Pez Ren Wes Ort Xon Zeb Yul  AnnBobCubDanEdyFly Gus Hal Ida Joe Kit Lex Moe Nan Ort Pez Quo Ren Sue Tia Uma Vin Wes Xon Yul Zeb Verdict: Much tougher to program, little memory waste, efficient in general (but very inefficient if the list is already almost sorted) Chapter 8 Abstract Data Types and Algorithms Page 82

  33. Data Structure #4: The Stack A stack is a data structure that manages a list of similar items in such a way that all insertions and deletions take place at one designated end of the list. In effect, one end of the list is considered the “top” of the stack, inserting into the list is considered “pushing” an item onto the top of the stack, and deleting from the list is considered “popping” off the top of the stack. Example: Chapter 8 Abstract Data Types and Algorithms Page 83

  34. Comparison: Stack Implementations Using an array Using a linked list void Push(int List[50], int &Top, int item) { if (Top < 49) { Top++; List[Top] = item; } } int Pop(int List[50], int &Top) { intval = -1; if (Top >= 0) { val = List[Top]; Top--; } return val; } void Push(nodePtr &List, int item) { nodePtrptr = new node; ptr->value = item; ptr->next = List; List = ptr; } int Pop(nodePtr &List) { intval = -1; if (nodePtr != NULL) { val = nodePtr->value; List = List->next; } return val; } Chapter 8 Abstract Data Types and Algorithms Page 84

  35. Example Stack Application Keeping track of function calls in a third-generation programming language. Main Program Subprogram A() Subprogram B() Subprogram C() • x = 0; • y = -2; • A(); • z = 6; • cout << x << y • << z << endl; • i = 10; • j = 46; • k = 31; • B(); • j = 50; • cout << i << j • << k << endl; • r = 400; • s = 542; • C(); • r = 710; • s = 365; • r = 927; • cout << r << s • << t << endl; • u = 15; • v = 57; • w = 34; • cout << u << v • << w << endl; • x = 0; • y = -2; • A(); • z = 6; • cout << x << y • << z << endl; • i = 10; • j = 46; • k = 31; • B(); • j = 50; • cout << i << j • << k << endl; • r = 400; • s = 542; • C(); • r = 710; • s = 365; • r = 927; • cout << r << s • << t << endl; When C finishes, the stack is popped and B resumes. When B finishes, the stack is popped and A resumes. When A finishes, the stack is popped and Main resumes and finishes. Chapter 8 Abstract Data Types and Algorithms Page 85

  36. Data Structure #5: The Queue A queue is a data structure that manages a list of similar items in such a way that all insertions take place at one end of the list, while all deletions take place at the other end. In effect, one end of the list is considered the “rear” of the queue, where new items enter; and the other end is considered the “front” of the queue, where old items are removed. Example: Chapter 8 Abstract Data Types and Algorithms Page 86

  37. Comparison: Queue Implementations Using an array Using a linked list void Insert(nodePtr &ListFront, nodePtr &ListRear, int item) { nodePtrptr = new node; ptr->value = item; ptr->next = NULL; if (ListFront == NULL) ListFront = ptr; else ListRear->next = ptr; ListRear = ptr; } int Remove(nodePtr &ListFront, nodePtr &ListRear) { intval = -1; if (ListFront != NULL) { val = ListFront->value; ListFront = ListFront->next; } if (ListFront == NULL) ListRear = NULL; return val; } void Insert(int List[50], int &Front, int &Rear, int item) { if (Front != (Rear+1)%50) { Rear = (Rear+1)%50; List[Rear] = item; if (Front == -1) Front = Rear; } } int Remove(int List[50], int &Front, int &Rear) { intval = -1; if (Front > -1) { val = List[Front]; if (Front == Rear) Front = Rear = -1; else Front = (Front+1)%50; } return val; } Chapter 8 Abstract Data Types and Algorithms Page 87

  38. Example Queue Application Keeping track of batch jobs as they arrive to be processed by a computer. CPU processing Job A Job A arrives and starts processing: Job B arrives: CPU processing Job A Jobs C & D arrive: CPU processing Job A Job A completes; Job B starts processing: CPU processing Job B Chapter 8 Abstract Data Types and Algorithms Page 88

  39. 8 3 14 1 5 10 19 7 12 16 23 17 Data Structure #6: The Binary Tree A binary tree is a hierarchical data structure that manages a collection of similar items in such a way that one item is designated as the “root” of the tree, and every other item is either the left or right “offspring” of some previously positioned item. Example Implementation: struct node; typedef node *nodePtr; struct node { int value; nodePtr left; nodePtr right; }; nodePtr Tree; • Example: Binary Insertion Tree • Each left offspring of a node has a value less than the node’s value • Each right offspring of a node has a value greater than or equal to the node’s value Chapter 8 Abstract Data Types and Algorithms Page 89

  40. Recursive Insertion into a Binary Insertion Tree 8 3 14 1 5 10 19 7 12 16 23 17 void Bin_Insert(nodePtr &Tree, int item) { if (Tree == NULL) { nodePtrptr = new node; ptr->value = item; ptr->left = NULL; ptr->right = NULL; } else if (item < Tree->value) Bin_Insert(Tree->left, item); else Bin_Insert(Tree->right, item); } Example: Where will a new node containing the integer 11 be inserted? 11 Chapter 8 Abstract Data Types and Algorithms Page 90

  41. Recursive Traversal of a Binary Insertion Tree 8 3 14 1 5 10 19 7 12 16 23 17 void Inorder(nodePtr Tree) { if (Tree != NULL) { Inorder(Tree->left); cout << Tree->value << endl; Inorder(Tree->right); } } 1 3 5 7 8 10 12 14 Example: Apply Inorder to this binary insertion tree: 16 17 19 23 Chapter 8 Abstract Data Types and Algorithms Page 91

  42. What Does This Function Do To A Binary Tree? 13 5 20 34 22 7 9 15 int Sumac(nodePtr Tree) { intleftbranch, rightbranch; if (Tree == NULL) return 0; else { leftbranch = Sumac(Tree->left); rightbranch = Sumac(Tree->right); return leftbranch + rightbranch + Tree->value; } } 125 51 61 34 22 0 31 Chapter 8 Abstract Data Types and Algorithms Page 92 9 0 0 15 0 0 0 0 0 0

  43. Chapter 9: High-Level Programming Languages Third-generation languages (e.g., BASIC, FORTRAN, COBOL, C) were developed as a solution to the assembly language problems. Third-generation languages are structured to avoid considering machine operations at all, instead concentrating on relatively straightforward instructions on how the data is being manipulated. Each type of computer that executes programs in a TGL has a special program (called a compiler) that translates the TGL code into the computer’s machine language. Example: • int negative (int x) • { • if (x < 0) • return 1; • else • return 0; • } Consequently, a TGL program written on one machine can be run on any other computer, as long as the computer has a compiler for that TGL! Chapter 9 High-Level Programming Languages Page 93

  44. Compilation Lexical Analysis Code Generation Parsing Object Program (in Machine Language) Source Program (in TGL) Lexical Analysis The compiler takes the TGL program (called the source program) and determines which strings of characters form separate items (e.g., “if (count > 100)” is split into “if”, “(”, “count”, “>”, “100”, and “)”), and all comments and white space (blanks, line feeds, etc.) are deleted. Parsing The compiler then analyzes the grammatical syntax of the program (e.g., “if”, “(”, “count”, “>”, “100”, “)” is determined to make sense, but a syntax error would be noticed in “if”, “count”, “>”, “100”, “)”.) Code Generation Once the program has been satisfactorily parsed, the compiler generates an equivalent program in machine language (called the object program). Chapter 9 High-Level Programming Languages Page 94

  45. Linking and Loading Object Program Load Module Executable Program Link Load Compile Since the individual portions of the TGL program are compiled as separate units (e.g., your program, a math library, a graphics library, etc.), the resulting machine code cannot be executed until all of the units are connected together as a single machine language program. Source Program Linking A TGL programmer usually relies on pre-compiled libraries of code (math functions, graphics routines, I/O operations, etc.) that are connected to the programmer’s code prior to execution by a linker program. Loading Finally, a special loader program places the resulting machine code in main memory, tying up all loose ends (e.g., setting the instruction addresses for JUMP instructions) so the code is ready for execution. Chapter 9 High-Level Programming Languages Page 95

  46. Standard Source Program Organization Declarative Statements Constant and variable values representing terms that will be manipulated as the program is executed. Imperative Statements The procedural specification of the algorithm itself. Source programs in most third-generation languages generally follow a standard pattern. void main() { const intmaxCount = 10; int count; int value; float sum = 0.0; cout << “Input values” << endl; count = 0; while (count < maxCount) { cin >> value; sum += value; count++; } cout << “Mean value: ” << sum/count << endl; } void main() { const intmaxCount = 10; int count; int value; float sum = 0.0; cout << “Input values” << endl; count = 0; while (count < maxCount) { cin >> value; sum += value; count++; } cout << “Mean value: ” << sum/count << endl; } void main() { const intmaxCount = 10; int count; int value; float sum = 0.0; cout << “Input values” << endl; count = 0; while (count < maxCount) { cin >> value; sum += value; count++; } cout << “Mean value: ” << sum/count << endl; } Chapter 9 High-Level Programming Languages Page 96

  47. Data Types Data types are used to specify how the bit patterns used to represent data should be interpreted by the program. Scalar: Single-valued data types (e.g., integer, floating-point, character, boolean) int count; float price; bool flag; Structured: Multiple-valued data types Built-In: Arrays, character strings float CaffeineOuncesPerDay[7]; char chosenCola[] = “Pepsi”; User-Defined: Specially constructed struct Student { char name[30]; intexamScore[5]; intquizScore[25]; intpaperScore[2]; char letterGrade; }; Student FALL111[25]; Chapter 9 High-Level Programming Languages Page 97

  48. Imperative Statements - Part One Assignment & I/O Assignment statements are used to assign a value to a variable. x = 127; count = count + 1; E = m * c * c; Input/output statements are used to retrieve external values (input) and to file away or print information (output). Enter user’s name: MOE cout << “Enter user’s name: ”; cin>> username; dataFile>> nextDataValue; if (nextDataValue > 0) positiveFile << nextDataValue; dataFile positiveFile 25 63 17 48 50 77 13 91 23 89 34 56 25 63 17 48 50 77 13 91 23 89 34 56 25 Chapter 9 High-Level Programming Languages Page 98

  49. Imperative Statements - Part Two Control Statements Conditional statements are used to enable alternative steps based on a condition. switch (AreaCode) { case 701: cout << “ND”; break; case 218: case 507: case 612: cout << “MN”; break; } if (total == 0) cout << “Possible Drop”; else cout << “Total: ” << total; Iterative statements are used to loop through a sequence of instructions. total = 0; for (i = 0; i <= 24; i++) { quizFile >> score[i]; total += score[i]; } while (flag == false) { cin >> newValue; if (newValue > 0) flag = true; } Chapter 9 High-Level Programming Languages Page 99

  50. Imperative Statements - Part Three Procedures & Functions Procedures and functions are used to conveniently write programs in a modular fashion. typedef int intList[100]; void getList(intList list) { int count; for (count = 0; count < 100; count++) cin >> list[count]; } int maximum(intList list) { int maxSoFar; int count; maxSoFar = list[0]; for (count = 1; count < 100; count++) if (list[count] > maxSoFar) maxSoFar = list[count]; return maxSoFar; } void main() { intList IQlist; intList SATlist; int maxIQ; int bestSAT; getList(IQlist); maxIQ = maximum(IQlist); getList(SATlist); bestSAT = maximum(SATlist); cout << “The highest IQ is ” << maxIQ << “ and the ” << “best SAT score is ” << bestSAT; } Chapter 9 High-Level Programming Languages Page 100

More Related