Algorithms Series of mathematical or variable manipulations Integer a,b,c a = 12 b = 22 c = a * b (what is c?) 12 * 22 = 264.

Slides:



Advertisements
Similar presentations
CMPUT 101 Lab # 5 October 22, :00 – 17:00.
Advertisements

True or false A variable of type char can hold the value 301. ( F )
Computer Science 1620 Loops.
Your First C++ Program Aug 27, /27/08 CS 150 Introduction to Computer Science I C++  Based on the C programming language  One of today’s most.
Announcements The first graded lab will be posted Sunday 2/15 and due Friday 2/27 at midnight It is brand new, so please don’t hand in last semester’s.
1 Lecture 2  Input-Process-Output  The Hello-world program  A Feet-to-inches program  Variables, expressions, assignments & initialization  printf()
Computer Programming and Basic Software Engineering 4. Basic Software Engineering 1 Writing a Good Program 4. Basic Software Engineering 3 October 2007.
1 9/20/06CS150 Introduction to Computer Science 1 Review: Exam 1.
A simple C++ program /* * This program prints the phrase "Hello world!" * on the screen */ #include using namespace std; int main () { cout
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
Libraries Programs that other people write that help you. #include // enables C++ #include // enables human-readable text #include // enables math functions.
Algorithms Series of mathematical or variable manipulations Integer a,b,c a = 12 b = 22 c = a * b (what is c?) 12 * 22 = 264.
Computer Science 1620 C++ - Basics. #include using namespace std; int main() { return 0; } A very basic C++ Program. When writing your first programs,
Basic Elements of C++ Chapter 2.
Programming is instructing a computer to perform a task for you with the help of a programming language.
VARIABLES, TYPES, INPUT/OUTPUT, ASSIGNMENT OPERATION Shieu-Hong Lin MATH/CS Department Chapel.
CSCI 1730 January 17 th, 2012 © by Pearson Education, Inc. All Rights Reserved.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
CS161 Topic #21 CS161 Introduction to Computer Science Topic #2.
CS1 Lesson 2 Introduction to C++ CS1 Lesson 2 -- John Cole1.
C++ Basics Structure of a Program. C++ Source Code Plain text file Typical file extension .CPP Must compile the C++ source code without errors before.
Creating your first C++ program
Input, Output, and Processing
Chapter 1 Introduction to Computers and C++ Programming Goals: To introduce the fundamental hardware and software components of a computer system To introduce.
Chapter 02 (Part III) Introduction to C++ Programming.
1 C++ Programming Basics Chapter 2 Lecture CSIS 10A.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
Basic Elements of C++ Chapter 1.
COMPUTER PROGRAMMING. A Typical C++ Environment Phases of C++ Programs: 1- Edit 2- Preprocess 3- Compile 4- Link 5- Load 6- Execute Loader Primary Memory.
# ACS 168 Structured Programming Using the Computer Chapter 2 Spring 2002 Prepared by Shirley White.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
6/3/2016 CSI Chapter 02 1 Introduction of Flow of Control There are times when you need to vary the way your program executes based on given input.
Chapter 2 Overview of C++. 2 Overview  2.1 Language Elements  2.2 Reserved Words & Identifiers  2.3 Data Types & Declarations  2.4 Input/Output 
C++ Basics C++ is a high-level, general purpose, object-oriented programming language.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
THE BASICS OF A C++ PROGRAM EDP 4 / MATH 23 TTH 5:45 – 7:15.
1 A simple C++ program // ======================================================= // File:helloworld.cpp // Author:Vana Doufexi // Date:1/4/2006 // Description:Displays.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process.
Chapter 2 part #1 C++ Program Structure
A first program 1. #include 2. using namespace std; 3. int main() { 4. cout
1 CS161 Introduction to Computer Science Topic #9.
C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello.
C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved Chapter Two: Fundamental Data Types Slides by Evan Gallagher.
PROGRAM ESSENTIALS. TOKENS  SMALLEST UNITS OF A PROGRAM LANGUAGE  Special Symbols  Mathematical Operators  Punctuation  Word Symbols  Key Words.
CS Jan 2007 Chapter 2 sections 1, 2, 4 – 6, 8,
Introduction to C++.  Computers: CPU, Memory & Input / Output (IO)  Program: Sequence of instructions for the computer.  Operating system: Program.
CSE1222: Lecture 1The Ohio State University1. Computing Basics  Computers CPU, Memory & Input/Output (IO)  Program Sequence of instructions for the.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
1 A more complex example Write a program that sums a sequence of integers and displays the result. Assume that the first integer read specifies the number.
1 8/30/06CS150 Introduction to Computer Science 1 Your First C++ Program.
A Sample Program #include using namespace std; int main(void) { cout
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
Bill Tucker Austin Community College COSC 1315
Chapter Topics The Basics of a C++ Program Data Types
Introduction to C++ Programming
Chapter 2 Introduction to C++ Programming
ANNOUNCEMENT The missed lecture will be made up this Monday evening in the Tech PC classroom (MG51). A tentative time interval is 6:30-8:00. The exact.
Basic Elements of C++.
Chapter 2 Assignment and Interactive Input
Chapter 2 part #3 C++ Input / Output
Basic Elements of C++ Chapter 2.
2.1 Parts of a C++ Program.
Introduction to C++ Programming
Introduction to C++ Programming
Programs written in C and C++ can run on many different computers
Chapter 2 part #3 C++ Input / Output
Introduction to Programming - 1
Chapter 1 c++ structure C++ Input / Output
Presentation transcript:

Algorithms Series of mathematical or variable manipulations Integer a,b,c a = 12 b = 22 c = a * b (what is c?) 12 * 22 = 264

An example…. Describe how to tie a shoe… How to text message someone… Locate a human on a help line… They are all instructions that have "repeats" and "retries" (called iterations) and "if this" and "do that" (called conditionals) top to bottom left to right each step depends on the steps before (called “concatenation”)

Instructions have to be explicit A programmer is told: "Go to the store and buy a loaf of bread. If they have eggs, buy a dozen". He returns with 12 loaves of bread. "Please peel half the potatoes that are in the bag and cook them."

4 algorithmic tools concatination - one step at a time iteration - repeat until a condition is met conditional - make a decision transfer of control - end and start something new

Programs: Complicated, Concatenated Algorithms integer Total, LoopCounter Total = 0 LoopCounter = 0 Start: Add 2 to Total Add 1 to LoopCounter Go Back to Start Until Total = 12 What is LoopCounter? done Total: LoopCounter: test for Total=12, done here

Written in “Code” int LoopCounter = 0; int Total = 0; while (Total < 12) { Total = Total + 2; LoopCounter = LoopCounter + 1; } semicolon open bracket odd-looking algebra no semicolon (because brackets follow) close bracket

Basic C++ Program Framework #include using namespace std; int main( ) { } VERY confusing: - where to put semicolons everywhere but on "conditionals" if and while - where to use brackets to start and end a program or functional paragraph brackets always paired your code here

C++ Commands go in the brackets tells the computer what to do, one step at a time mathematical, algorithmic follows a strict “syntax” in C++, brackets { } always surround "functional paragraphs"

int main( ) memorize it… it means something. the computer - when asked to run your program - looks for the main to start. int main( ) { … put your program here } in C++, brackets always surround "functional paragraphs"

Step 1 - The info at the top The computer views the top few lines to set up a basic execution environment #include using namespace std ; no ; enables keyboard & monitor uses ; what keyboard and monitor are called: cout and cin no ; additional “libraries”

Step 2 - main( ) After setting up the environment, the computer looks for "int main( )" to begin execution of your program int main( ) { } computer finds this then it runs, from here to here never ; before brackets

step 3 - any variables? int main( ) { int x = 0; double y = 1.0; char z = 'Q'; string a = "hello"; } note: x, y, z, and a will "hold" changeable values a counting variable a measuring variable a keyboard character a word or phrase

step 4 - provide a way to EXIT Easy: int main( ) { return 0; } Note: computer exits at last } anyways, but programmers like to be explicit computer finds this then it runs, from here to here

step 5 - write to the monitor #include using namespace std ; int main( ) { cout << "hello" << endl ; return 0 ; }

cout can do “strings” or variables #include using namespace std ; int main( ) { double pi = ; cout << pi << endl ; return 0 ; }

step 6 - accept keyboard input #include using namespace std; int main( ) { int y = 0; cout << "Enter an integer: "; cin >> y; cout << "The integer is "; cout << y << endl; return 0; } // end of program

cout and cin cout << "Enter an integer: "; cin >> y; cout << "The integer is "; cout << y << endl; or a combination of outputs: cout << "The integer is " << y << endl;

Scientific notation Try this: double x; x = e 6; cout.scientific; cout << x << endl; cout.fixed; //eliminates scientific notation cout.precision(12); cout << x << endl;

cout - decimal precision cout only displays 6 decimal characters cout attempts to reduce output to save characters use "cout. precision( n ); " to set displayable values to n decimal characters

count to a million #include using namespace std; int main( ) { int x = 0; while ( x <= ) { x = x+1; // what? cout << x << endl; } } // end program

step 7 - Comments included in your “program” read by humans, not the computer used to explain what you intend the code to do

Commenting code /* anything between “delimiters” */ // anything after two slashes Header - comments at the beginning of a program // one line only /* EAS230 Author: Harry Truman Date: Feb. 4, 2015 */

Readability Indentation Skipped lines Brackets Comments Pages & Functional paragraphs Meaningful variable names

The Assignment Statement “=” int main ( ) { int A; A = 4; A = A + 3; } NOT “A equals 4” instead: “A is assigned the value 4” semicolon, brackets, main… must be C++

Keywords vs. myWords Keywords are C++ defined: –int –main –char –double –while –many more variables, file names are your choice Convention: –lowerUpper

Libraries Programs that other people write that help you. #include // enables C++ #include // enables human-readable text #include // enables math functions using namespace std; // enables cout >

Operators The usual: + - / * Precedence: use ( ) e.g. (y + b) * c not the same as y + b * c Negative numbers: - 8

reading Read all of chapter 1 in the text