230 likes | 410 Views
CS 1. Introduction. Hardware. Central Processing Unit (CPU) Main Memory Secondary Memory / Storage Input Devices Output Devices. Main Memory. It is volatile. Main memory is erased when program terminates or computer is turned off Also called Random Access Memory (RAM)
E N D
CS 1 Introduction CS 1 Part 1
Hardware • Central Processing Unit (CPU) • Main Memory • Secondary Memory / Storage • Input Devices • Output Devices CS 1 Part 1
Main Memory • It is volatile. Main memory is erased when program terminates or computer is turned off • Also called Random Access Memory (RAM) • Organized as follows: • bit: smallest piece of memory. Has values 0 (off, false) or 1 (on, true) • byte: 8 consecutive bits. Bytes have addresses. • Addresses are sequential numbers from 0 to the maximum amount of memory in your computer. CS 1 Part 1
Memory Organization • In the chart above, the number 149 is stored in the byte with the address 16, and the number 72 is stored at address 23. • What these numbers might mean depends upon the program. • The concept of the “stored program” computer is this: that the numbers can be machine instructions. CS 1 Part 1
Secondary Storage • Non-volatile: data retained when program is not running or computer is turned off • Comes in a variety of media: • magnetic: floppy disk, hard drive • optical: CD-ROM, DVD • Flash drives, connected to the USB port • Solid-state drives instead of rotating disks CS 1 Part 1
Machine Instructions • The C++ compiler translates your program, which is relatively easy for you to read, into binary numbers which are the instructions and data the computer understands. CS 1 Part 1
Binary (base 2) • Inside a computer, the contents of memory cells can be either on or off, or zero or 1. • Place value: in base 10 you have units, 10s, 100s, 1000s, etc. • In binary: units, 2s, 4s, 8s, 16s, 32s, etc. Powers of 2 vs. powers of 10. • Count in binary on your fingers. • Thus there are 10 kinds of people: those who understand binary and those who don’t. CS 1 Part 1
Binary Addition Add: 0 0 1 1 0 1 0 1 0 0 1 0 1 1 0 1 --------------- 0 1 1 0 0 0 1 0 Four rules: 0+0=0 1+0=1 1+1=0 carry 1 1+1+1 = 1 carry 1 CS 1 Part 1
Base 8 and Base 16 • It’s easy to convert between base 2 and either base 8 (octal) or base 16 (hexadecimal) by just grouping digits. • Convert 11000111 to hex: • Groups of 4 because 2^4th =16 • 1100=1210 = C16; 0111=710 CS 1 Part 1
Binary to Base 10 Place values: 128 64 32 16 8 4 2 1 0 0 1 1 0 1 0 1 So in the example we have: CS 1 Part 1
Elements of a Program • Common elements in programming languages: • Key Words • Programmer-Defined Identifiers • Operators • Punctuation • Syntax CS 1 Part 1
Keywords • Also known as reserved words • Have a special meaning in C++ • Can not be used for any other purpose CS 1 Part 1
Identifiers • Names made up by the programmer • Not part of the C++ language • Used to represent various things: variables (memory locations), functions, etc. CS 1 Part 1
Operators and Precedence • Used to perform operations on data • Many types of operators: • Arithmetic – * (multiply), / (divide), + (add), - (subtract) • Assignment – ex: = the equal sign • Comparison: == CS 1 Part 1
Punctuation • Use semicolon to end statements (not every line is a statement) • Use comma to separate items in a list. CS 1 Part 1
Grouping • Parentheses alter the order of operations in an expression, and enclose function arguments. • Braces {} group statements together. • Brackets [] are used for array references. CS 1 Part 1
Language Syntax • Syntax refers to the way the elements of a language are put together. For example, in English the verb form doesn’t usually tell you the subject, while in Spanish it does. • Programming language syntax is the way program statements are constructed. • Semantics refers to what the statements “mean.” CS 1 Part 1
Variables • A variable is a named storage location in the computer’s memory for holding a piece of data. • The contents can be changed by a program. CS 1 Part 1
Variable Declaration • To create a variable in a program you must write a variable definition (also called a variable declaration) • You supply a name and the data type. • Examples: • int hours; • double pay; • char answer; CS 1 Part 1
What Programs Do • Reduced to simplest terms, a program: • Takes some form of input • Does some processing to it • Creates some kind of output CS 1 Part 1
Three Basic Constructs • Execution in sequence • Making a decision • Looping CS 1 Part 1
Creating a Program • Define clearly and precisely what the program is to do. • Use various tools to create a model of the program. • Check your model for errors. • Write code that reflects the model. • Compile the code, which checks for syntax errors. • Correct any errors you find and go back to step 5 if there are any. • Run the program with test data and determine whether the results are correct. • If the results are not what you wanted, make changes and go back to step 7. • Validate the results. CS 1 Part 1
Two Programming Paradigms • Procedural programming focuses on the process. The program generally executes from start to finish in a linear way. • Object-oriented programs focus on objects, which have both data and methods that act on the data. While an object-oriented program can be procedural, most respond to messages from external events. CS 1 Part 1