150 likes | 662 Views
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 high-level programming. To examine the use of algorithms in program design.
E N D
Chapter 1Introduction 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 high-level programming • To examine the use of algorithms in program design • To define the software life cycle • To introduce the C++ programming language
Monitor CD-ROM Integrated Audio Card Mouse Integrated Network Card Cache Memory Keyboard Graphics Connector Graphics Card Hard Drive Additional Card Slots Extra Bay Main Memory Floppy Drive CPU Computer Hardware CS 140
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
Computer Software Application Software that performs high level operations (computation, graphics, etc.) Specify resulting audio & video “Hey, App! key ‘P’!” Operating System Software that relays messages between application and hardware Contact sound & graphics cards “Hey, OS! key ‘P’!” Hardware Direct access to circuitry, disks, mouse, keyboard, monitor, etc. Send explosion sound to speaker and new pixel values to monitor Key ‘P’ struck CS 140
Low-Level 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! Check to see if this number is zero! CS 140
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. 001001010110101010101010101010100001111110011100101111000001101010010010111010100010100100001101011001010100110101100101010010101001010010101011010010101000100101010101010101010101001010101001 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! CS 140
Compiling a High-Level 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). 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. #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!”; } 110101000101100011000010010110110100010101011110010101011100000010011100101011001110101010111001010100101010101000000110110111011101010100111110101010101001001001010000010101010101000000101111100101100001011101010101010100010101111110010100100100101000 Source Program Object Program COMPILATION CS 140
Compiled Library Programs 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 Object Program Linked Program COMPILE Executable Code LINK LOAD CS 140
47 28 56 30 61 19 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. 47 47 Problem: Find the largest value in a list of numbers. ? Algorithm: 1) Retrieve the list of numbers. ? 56 56 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. ? LARGEST! 61 61 4) After examining all of the numbers, announce the largest value so far - it’s the largest in the list!. ? CS 140
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
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 bottom-up approach (with dummy drivers) or a top-down approach (with stubs). CS 140
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++ - Object-oriented version of C Java - Object-orientation with networking emphasis Emphasis upon the objects being manipulated CS 140
A Sample C++ Program #include <iostream> // This library facilitates input & output. #include <cmath> // This library enables math functions, like sqrt. using namespace std; void main() // Every C++ program must have a "main" function. { // Opening brace to contain main's statements. int nbr; // Declare variable "nbr" to be an integer. double root; // Declare variable "root" to be a long real number. nbr = 2; // Set value of nbr to be 2. root = sqrt(nbr); // Calculate square root of nbr. cout << "The square root of " // Output a message to the memory file << nbr << " is " << root // associated with the monitor (i.e., cout), << endl << endl; // including nbr, root, and a skipped line. cout << "Enter a number: "; // Ask the user for a value for nbr. cin >> nbr; // Input from the file associated with the keyboard (cin). root = sqrt(nbr); // Calculate the square root of nbr's new value. cout << "The square root of " // Output a message to the cout file << nbr << " is " << root // concerning the values of nbr and root, << endl << endl << endl; // followed bt two skipped lines. return; // Terminate the program's execution. } // Closing brace to indicate end of main function. CS 140