Elementary Programming (C++)

Slides:



Advertisements
Similar presentations
Chapter 3 Assignment and Interactive Input. 2 Objectives You should be able to describe: Assignment Operators Mathematical Library Functions Interactive.
Advertisements

Computer Science 1620 Variables and Memory. Review Examples: write a program that calculates and displays the average of the numbers 45, 69, and 106.
Wednesday, 9/4/02, Slide #1 1 CS 106 Intro to CS 1 Wednesday, 9/4/02  Today: Introduction, course information, and basic ideas of computers and programming.
Copyright © 2012 Pearson Education, Inc. Chapter 1: Introduction to Computers and Programming.
Basic Elements of C++ Chapter 2.
Copyright © 2012 Pearson Education, Inc. Chapter 1: Introduction to Computers and Programming.
Why Program? Computer – programmable machine designed to follow instructions Program – instructions in computer memory to make it do something Programmer.
Chapter Introduction to Computers and Programming 1.
COMPUTER SCIENCE I C++ INTRODUCTION
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
Expressions and Interactivity Chapter 3. 2 The cin Object Standard input object Like cout, requires iostream file Used to read input from keyboard Often.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
Introduction to Python
Chapter 1: Introduction to Computers and Programming.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 1: Introduction to Computers and Programming.
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
Chapter 2: Using Data.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
Basic Elements of C++ Chapter 1.
C++ Programming: Basic Elements of C++.
An Object-Oriented Approach to Programming Logic and Design Chapter 1 An Overview of Computers and Logic.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)
PROGRAM ESSENTIALS. TOKENS  SMALLEST UNITS OF A PROGRAM LANGUAGE  Special Symbols  Mathematical Operators  Punctuation  Word Symbols  Key Words.
Recap……Last Time [Variables, Data Types and Constants]
Brief Version of Starting Out with C++ Chapter 1 Introduction to Computers and Programming.
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
Introduction to Algorithmic Processes CMPSC 201C Fall 2000.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 3: Input/Output Samples.
Chapter 1: Introduction to Computers and Programming
Bill Tucker Austin Community College COSC 1315
Variables, Operators, and Expressions
C++ LANGUAGE MULTIPLE CHOICE QUESTION
Today Variable declaration Mathematical Operators Input and Output Lab
Arithmetic Expressions
Chapter 2: Basic Elements of C++
Chapter Topics The Basics of a C++ Program Data Types
Basic Elements of C++ Chapter 1.
Topics Designing a Program Input, Processing, and Output
Chapter 1: Introduction to computers and C++ Programming
Data Types, Variables & Arithmetic
Chapter 3 Assignment and Interactive Input.
Completing the Problem-Solving Process
Introduction to Computer Science / Procedural – 67130
Computing Fundamentals
Basic Elements of C++.
Data Types, Identifiers, and Expressions
Chapter 1. Introduction to Computers and Programming
Variables, Expressions, and IO
Fundamental of Java Programming Basics of Java Programming
Basic Elements of C++ Chapter 2.
Chapter 1: Introduction to Computers and Programming
Building Java Programs Chapter 2
Expressions and Interactivity
Introduction to C++ Programming
Programming Funamental slides
Building Java Programs Chapter 2
CS150 Introduction to Computer Science 1
Topics Designing a Program Input, Processing, and Output
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Fundamental Programming
Summary of what we learned yesterday
Topics Designing a Program Input, Processing, and Output
C++ Programming Basics
Unit 3: Variables in Java
Building Java Programs Chapter 2
Introduction to C Programming
Chapter 1: Introduction to Computers and Programming
Presentation transcript:

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

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.

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.

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.

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

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.

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

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.

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.

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

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.

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 ),

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

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;

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.

Assignment Statements <variable name> = <value to assign to the variable> int hours = 40; int otHours = 0; hours = 32; hours = 32 + 6; 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.

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 “ << 3 + 4 << endl; Tell them when to use one or the other.

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

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 = 25 + 24 – 32; total = 49 – 32; total = 17; total = 25 + 17 * 2; total = 25 + 34; total = 59;

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

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

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