Presentation is loading. Please wait.

Presentation is loading. Please wait.

Elementary Programming (C++)

Similar presentations


Presentation on theme: "Elementary Programming (C++)"— Presentation transcript:

1 Elementary Programming (C++)
INF 120 Elementary Programming (C++)

2 INF 120 Outline Computers and Programming Data Types and Variables
Arithmetic and Logical Operators Decision-Making Statements Repetition Statements Arrays We might add some optional topics, if we have time.

3 Why do we need people who know how to write programs?
Computers need instructions to follow. Programs are the sets of instructions that tell the computer how to accomplish tasks. Programmers have to write these programs according to the tasks that have to be accomplished. Without programs, computers are just expensive door stops.

4 Three Things a Computer Does Really Well
Computer never forgets. Computer is really, really fast. Once told how to do something correctly, never, ever, ever makes a mistake. 1. Like an ATM program, does it over and over and over again. 2. Calculating space shuttle trajectory, moving work to non-critical time. 3. Lots of examples.

5 Computer Systems A computer is made up of hardware.
A computer system is a combination of hardware and software.

6 Computer Hardware CPU – Processes the instructions. Receives instructions and data stored in the Main Memory. Main Memory – Random Access Memory (RAM). Volatile, loses contents when power is turned off. Secondary Storage – Disk drives, store data magnetically. Doesn’t lose contents when power removed. Input Devices – Get information from the outside world into the computer. Keyboard, mouse, scanner. Output Devices – Show results to the outside world. Screen, printer, etc.

7 Two Categories of Software
System Software – Runs the computer. Application Software – Actually helps you accomplish work.

8 Program Development Process
Understand the requirement Create an algorithm Write the code Compile the code If there are syntax errors Run the code If there are run-time errors Check output for logic errors Compiling converts the English-like source code into language the processor understands. Three types of Errors: 1. Syntax – Easy to find and fix 2. Run-Time – Not as easy, but still get help in finding them. 3. Logic – The worst. Absolutely no help in finding them.

9 Algorithm for Calculating Hypotenuses
Get value of Side A from user. Display prompt telling user what to enter. Wait for user to enter a value and store it. Get value of Side B from user. Apply formula to calculate hypotenuse. Display the result.

10 Calculates hypotenuse of right triangle from user
/* hypotenuse.cpp Lee Weiner - 7/28/07 Calculates hypotenuse of right triangle from user input of side A & side B */ #include <iostream.h> #include <math.h> int main() { double sideA, //Input by user sideB, //Input by user hypotenuse; //Calculated and output //Get the values for side A & side B cout << "Enter the value of Side A: "; cin >> sideA; cout << "Enter the value of Side B: "; cin >> sideB; //Calculate and ... hypotenuse = sqrt( sideA * sideA + sideB * sideB ); //Display the output cout << "The hypotenuse is " << hypotenuse << "." << endl; return 0; } HANDOUT Every line of code ends in a semi-colon. A line of code goes from semi-colon to semi-colon. Three types of comments: 1. Header 2. Variable dictionary 3. In-line

11 Arithmetic Operators + Addition - Subtraction * Multiplication
/ Division % Modulus Addition, Subtraction, Multiplication work just the way you expect them to. Division is a little hinky. Modulus – Both operands must be integers.

12 Data Types int – An integer value between -2 Billion and +2 Billion
double – A floating point value char – One character worth of data bool – Contains a boolean value ( true or false ),

13 Variables Containers to hold values when the program runs.
They’re called variables because their value can vary during the course of the program. Variables have 3 characteristics: A name A data type Contents

14 Variables (con’d) Have to be declared before they can be used. The declaration reserves memory to store values and assigns a name to the memory. A variable declaration statement has two parts, a data type and a variable name: Variable names must be a combination of letters (a – z), numbers and underscores, and must start with a letter. No spaces in variable names. There is a naming convention: 1. Names should be meaningful (payRate, numberDays). 2. Variables are normally named in lower-case, but if the name is more than one word, subsequent words are in initial caps. int days; double payRate; char dayOfWeek;

15 Variables (con’d) Optionally, variables may be initialized to a particular value in the declaration statement: int days = 7; double hourlyRate = 13.25; char choice = ‘A’; Until a variable is initialized or assigned a value, it has no value.

16 Assignment Statements
<variable name> = <value to assign to the variable> int hours = 40; int otHours = 0; hours = 32; hours = ; hours = hours + 8; otHours = hours – 40; Assigning a value to a variable trashes whatever value that variable had before. Read the assignment statement as: Evaluate the expression on the right, and assign the result to the variable named on the left.

17 Performing Arithmetic
In an assignment statement: int total; total = 3 + 4; cout << “The total of 3 and 4 is “ << total << endl; In a cout statement: cout << “The total of 3 and 4 is “ << << endl; Tell them when to use one or the other.

18 Constants const double ST_TAX_RATE = .065; const int NUM_EMPS = 100;
const int MAX_LENGTH = 260; Explain why they’re used.

19 Operator Precedence Generally, operators are evaluated from left to right, however, Operators have different precedence, meaning multiplication and division are always evaluated before addition and subtraction. Can change sequence of operations with parentheses. Examples: total = – 32; total = 49 – 32; total = 17; total = * 2; total = ; total = 59;

20 Arithmetic Assignment Operators
+= hours += 6 is the same as hours = hours + 6 - = hours - = 6 is the same as hours = hours – 6 *= hours *= 6 is the same as hours = hours * 6 /= hours /= 6 is the same as hours = hours / 6 Increment/Decrement Operators Increment variable by one Decrement variable by one

21 Math Library Functions
ceil – Returns the next higher integer value. y = ceil(x); floor – Returns the next lower integer value. y = floor(x); pow – Returns the first argument raised to the power of the second argument. z = pow(x, y); sqrt – Returns the square root of the argument. y = sqrt(x); HANDOUT – math.cpp

22 Math Library Functions (con’d)
Trigonometric functions sin – Return the sine of an angle cos – Return the cosine of an angle tan – return the tangent of an angle asin, acos, atan – Return the angle associated with the sin, cos or tan. HANDOUT – trig.cpp


Download ppt "Elementary Programming (C++)"

Similar presentations


Ads by Google