Notes1 - Chapter 1 Introduction to Computers and C++...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Chapter 1 Introduction to Computers and C++ Programming Goals: To introduce the fundamental hardware and software components of a computer system To describe the role of compilers in highlevel programming To examine the use of algorithms in program design To define the software life cycle To introduce the C++ programming language Computer Hardware Monitor Integrated Audio Card Integrated Network Card Cache Memory Graphics Connector Graphics Card Additional Card Slots Main Memory CPU CS 140 Chapter 1 CD-ROM Mouse Keyboard Hard Drive Extra Bay Floppy Drive Page 2 Computer Memory Random Access Memory: the electronic memory used to run programs The magnetic disks that store your operating system and other files Read Only Memory: the electronic memory containing the machine's boot process Portable magnetic memory, with very restrictive memory capacity Optical memory, with greater capacity, but limited writability CS 140 Chapter 1 Page 3 Computer Software Application Software that performs high level operations (computation, graphics, etc.) Operating System Software that relays messages between application and hardware Hardware Direct access to circuitry, disks, mouse, keyboard, monitor, etc. Chapter 1 "Hey, App! key `P'!" Specify resulting audio & video "Hey, OS! key `P'!" Contact sound & graphics graphics cards cards Key `P' struck CS 140 Send explosion sound to speaker and new pixel values to monitor Page 4 LowLevel Programming Languages A computer processor is not smart! Its vocabulary is limited to a simple "machine language" consisting of a small number of simple commands. Move this number over there! Move that number over here! Add this number to that number! CS 140 Chapter 1 Check to see if this number is zero! Page 5 Programming To get the computer to perform sophisticated operations, the programmer writes programs that tell the processor the sequence of primitive steps to take to get a result. 0010010101101010 1010101010101010 0001111110011100 1011110000011010 1001001011101010 0010100100001101 0110010101001101 0110010101001010 1001010010101011 0100101010001001 0101010101010101 0101001010101001 13 372.01946 Programming in machine language is a binary pain, so higher level languages have been developed to make the job of the programmer more efficient and more effective! Page 6 CS 140 Chapter 1 Compiling a HighLevel Program A program caller a "compiler" is used to translate your "source program" (in a language like C++) into an "object program" (in your system's machine language). #include <iostream> using namespace std; void main() { int x, y; cout << "Enter two integers: "; cin >> x >> y; if (x > y) cout << x << " is the largest!"; else cout << y << " is the largest!"; } LEXICAL ANALYSIS Split the source program into words like "void", "x", ">", and ";". PARSING Analyze the grammatical syntax of the source program (e.g., "if (x > y)" makes sense, but "if (x > ) y" doesn't). CODE GENERATION Generate an equivalent program in machine language. 110101000101100011000 010010110110100010101 011110010101011100000 010011100101011001110 101010111001010100101 010101000000110110111 011101010100111110101 010101001001001010000 010101010101000000101 111100101100001011101 010101010100010101111 110010100100100101000 Source Program CS 140 Chapter 1 Object Program Page 7 Linking and Loading After being compiled, the object program must be "linked" (i.e., connected to other compiled code from libraries, like math functions or input/output operators) and then "loaded" into main memory for execution. Source Program COMPILE Object Program LINK Compiled Library Programs CS 140 Chapter 1 Linked Program LOAD Page 8 Algorithms After defining a problem that the programmer wants the computer to solve, the programmer must first design an algorithm, a sequence of precise instructions that lead to a solution. Problem: Find the largest value in a list of numbers. Algorithm: 1) Retrieve the list of numbers. 2) Consider the first number the largest value so far. 3) Starting at the second number in the list, compare the number in the list to the largest value so far; if it's larger, then make it the largest value so far. 4) After examining all of the numbers, announce the largest value so far - it's the largest in the list!. CS 140 Chapter 1 47 28 56 30 61 19 ? ? ? ? ? LARGEST! Page 9 Another Algorithm Problem: Find the phone number of a specific person in an alphabetized phonebook. Algorithm: 1) Get the phonebook. 2) Crack what's left of the phonebook open to the middle page. 3) Check to see if the name you're seeking is on that page. If so, announce the phone number and you're done!. Otherwise, throw away the "impossible" half of the phonebook, and repeat the process, starting at step #2. 4) If the entire phonebook is ever thrown out, then the person is unlisted! CS 140 Chapter 1 Page 10 The Software Life Cycle Specification Clearly state the purpose of the software, including full details of the problem being solved. Maintenance Respond to "bugs" and "sugs", and determine when the software has become obsolete. Design Develop a solution to the problem, modularizing it and determining specific pre- and post-conditions. DOCUMENTATION!!! Testing Design test scenarios for individual modules, interaction between modules, and the entire program. Coding Program the modules using a bottomup approach (with dummy drivers) or a top-down approach (with stubs). CS 140 Chapter 1 Page 11 Introduction to C++ Ancient Languages Fortran Great for scientific computations COBOL Great for business file processing Very special purpose, not good in general Old Languages C General purpose language for UNIX systems Pascal General purpose language for PCs Emphasis upon procedures instead of data Modern Languages C++ Objectoriented version of C Java Objectorientation with networking emphasis Emphasis upon the objects being manipulated CS 140 Chapter 1 Page 12 A Sample C++ Program #include <iostream> #include <cmath> using namespace std; void main() { int nbr; double root; nbr = 2; root = sqrt(nbr); // This library facilitates input & output. // This library enables math functions, like sqrt. // // // // Every C++ program must have a "main" function. Opening brace to contain main's statements. Declare variable "nbr" to be an integer. Declare variable "root" to be a long real number. // Set value of nbr to be 2. // Calculate square root of nbr. // Output a message to the memory file // associated with the monitor (i.e., cout), // including nbr, root, and a skipped line. // Ask the user for a value for nbr. // Input from the file associated with the keyboard (cin). // Calculate the square root of nbr's new value. // Output a message to the cout file // concerning the values of nbr and root, // followed bt two skipped lines. cout << "The square root of " << nbr << " is " << root << endl << endl; cout << "Enter a number: "; cin >> nbr; root = sqrt(nbr); cout << "The square root of " << nbr << " is " << root << endl << endl << endl; return; } // Terminate the program's execution. // Closing brace to indicate end of main function. CS 140 Chapter 1 Page 13 ...
View Full Document

This note was uploaded on 08/26/2009 for the course CS 140 taught by Professor Staff during the Fall '08 term at Southern Illinois University Edwardsville.

Ask a homework question - tutors are online