Chapter 1 Introduction to Computers and C Programming

  • Slides: 12
Download presentation
Chapter 1 Introduction to Computers and C++ Programming Goals: • To introduce the fundamental

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 • To define the software life cycle • To introduce the C++ programming language

Computer Hardware CS 140 Chapter 1 Page 2

Computer Hardware CS 140 Chapter 1 Page 2

Computer Software Application Software that performs high level “Hey, App! operations (computation, graphics, etc.

Computer Software Application Software that performs high level “Hey, App! operations (computation, graphics, etc. ) ke y ‘ P ’ ! ” Operating System Software that relays messages between application and hardware “ Hey , O S ! ke y ‘ P ’ ! ” Key ‘P’ struck CS 140 Hardware Direct access to circuitry, disks, mouse, keyboard, monitor, etc. Chapter 1 S p ec i f y r e s ul t i n g a ud i o & v i d eo C o n ta c t s o un d & g r a p h ic s c a rds Send explosion sound to speaker and new pixel values to monitor Page 3

Low-Level Programming Languages A computer processor is not smart! Its vocabulary is limited to

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! CS 140 Check to see if this number is zero! Chapter 1 Page 4

Programming To get the computer to perform sophisticated operations, the programmer writes programs that

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. 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! 0010010101010 00011111100 1011110000011010 1001001011101010 0010100100001101 01100101010010101011 010010101001 01010101001 CS 140 Chapter 1 Page 5

Compiling a High-Level Program A program caller a “compiler” is used to translate your

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). #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). Source Program CS 140 CODE GENERATION Generate an equivalent program in machine language. 1101010001011000 010010110110100010101 01111001011100000 010011100101011001110 10101011100101010010101000000110110111010011111010101001010000 010101000000101 111100101100001011101 01010100010101111 1100100100101000 Object Program Chapter 1 Page 6

Linking and Loading After being compiled, the object program must be “linked” (i. e.

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 7

Algorithms After defining a problem that the programmer wants the computer to solve, the

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 8

Another Algorithm Problem: Find the phone number of a specific person in an alphabetized

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 9

The Software Life Cycle Specification Clearly state the purpose of the software, including full

The Software Life Cycle Specification Clearly state the purpose of the software, including full details of the problem being solved. Maintenance Design Respond to “bugs” and “sugs”, and determine when the software has become obsolete. Develop a solution to the problem, modularizing it and determining specific pre- and post-conditions. DOCUMENTATION!!! CS 140 Testing Coding Design test scenarios for individual modules, interaction between modules, and the entire program. Program the modules using a bottomup approach (with dummy drivers) or a top-down approach (with stubs). Chapter 1 Page 10

Introduction to C++ Ancient Languages Fortran - Great for scientific computations COBOL - Great

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 Chapter 1 Page 11

A Sample C++ Program #include <iostream> #include <cmath> using namespace std; void main() {

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. Assigns the program to a specific namespace. 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. cout << "The square root of " << nbr << " is " << root << endl; // Output a message to the memory file // associated with the monitor (i. e. , cout), // including nbr, root, and a skipped line. cout << "Enter a number: "; cin >> nbr; root = sqrt(nbr); // 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. cout << "The square root of " << nbr << " is " << root << endl; // Output a message to the cout file // concerning the values of nbr and root, // followed bt two skipped lines. return; } CS 140 // Terminate the program's execution. // Closing brace to indicate end of main function. Chapter 1 Page 12