Intro to Programming & C Why Program? 1.2 Computer Systems: Hardware and Software. Why Learn to Program?

Similar documents
! A literal represents a constant value used in a. ! Numbers: 0, 34, , -1.8e12, etc. ! Characters: 'A', 'z', '!', '5', etc.

Intro to Programming & C Why Program? 1.2 Computer Systems: Hardware and Software. Why Learn to Program?

LECTURE 02 INTRODUCTION TO C++

Intro to Programming & C Why Program? 1.2 Computer Systems: Hardware and Software. Hardware Components Illustrated

! A program is a set of instructions that the. ! It must be translated. ! Variable: portion of memory that stores a value. char

Week 0: Intro to Computers and Programming. 1.1 Why Program? 1.2 Computer Systems: Hardware and Software. Hardware Components

Integer Data Types. Data Type. Data Types. int, short int, long int

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

Chapter 2: Introduction to C++

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

The C++ Language. Arizona State University 1

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Basic Elements of C++

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

BITG 1233: Introduction to C++

CHAPTER 3 BASIC INSTRUCTION OF C++

Computer Programming : C++

Lecture 2 Tao Wang 1

7/8/10 KEY CONCEPTS. Problem COMP 10 EXPLORING COMPUTER SCIENCE. Algorithm. Lecture 2 Variables, Types, and Programs. Program PROBLEM SOLVING

CSC 126 FINAL EXAMINATION Spring Total Possible TOTAL 100

Chapter 2 Basic Elements of C++

Objectives. In this chapter, you will:

C++ Programming: From Problem Analysis to Program Design, Third Edition

Chapter 1 Introduction to Computers and Programming

Lab # 02. Basic Elements of C++ _ Part1

Your First C++ Program. September 1, 2010

CS242 COMPUTER PROGRAMMING

Fundamentals of Programming CS-110. Lecture 2

Introduction. Arizona State University 1

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

Chapter 1: Why Program? Computers and Programming. Why Program?

REVIEW. The C++ Programming Language. CS 151 Review #2

2 nd Week Lecture Notes

Chapter 1 Introduction to Computers and C++ Programming

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Variables and Constants

UNIT- 3 Introduction to C++

CSc Introduction to Computing

Chapter Overview. C++ Basics. Variables and Assignments. Variables and Assignments. Keywords. Identifiers. 2.1 Variables and Assignments

4. Structure of a C++ program

CHAPTER 1.2 INTRODUCTION TO C++ PROGRAMMING. Dr. Shady Yehia Elmashad

Chapter 2: Overview of C++

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Chapter 2. C++ Basics

C++ Basic Elements of COMPUTER PROGRAMMING. Special symbols include: Word symbols. Objectives. Programming. Symbols. Symbols.

Chapter 1 INTRODUCTION

BTE2313. Chapter 2: Introduction to C++ Programming

CSCE 206: Structured Programming in C++

CSCE 206: Structured Programming in C++

Getting started with C++ (Part 2)

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Introduction to Programming EC-105. Lecture 2

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

Programming. C++ Basics

CS1500 Algorithms and Data Structures for Engineering, FALL Virgil Pavlu, Jose Annunziato,

CS 241 Computer Programming. Introduction. Teacher Assistant. Hadeel Al-Ateeq

Introduction to C++ Programming Pearson Education, Inc. All rights reserved.

CS101: Fundamentals of Computer Programming. Dr. Tejada www-bcf.usc.edu/~stejada Week 1 Basic Elements of C++

ME240 Computation for Mechanical Engineering. Lecture 4. C++ Data Types

A First Program - Greeting.cpp

Engineering Problem Solving with C++, Etter/Ingber

Chapter 1: Why Program? Main Hardware Component Categories 8/23/2014. Main Hardware Component Categories: Why Program?

Program Organization and Comments

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

Starting Out with C++: Early Objects, 9 th ed. (Gaddis, Walters & Muganda) Chapter 2 Introduction to C++ Chapter 2 Test 1 Key

Chapter 2. C++ Syntax and Semantics, and the Program Development Process. Dale/Weems 1

Chapter 2 C++ Fundamentals

Engineering Problem Solving with C++, 3e Chapter 2 Test Bank

Unit 3. Constants and Expressions

Chapter 1: Introduction to Computers and Programming

UEE1302 (1102) F10: Introduction to Computers and Programming

Creating a C++ Program

UNIT-2 Introduction to C++

Full file at C How to Program, 6/e Multiple Choice Test Bank

c++ keywords: ( all lowercase ) Note: cin and cout are NOT keywords.

6.096 Introduction to C++ January (IAP) 2009

Exercise 1.1 Hello world

Introduction to Programming

Introduction to C++ (Extensions to C)

Today. o main function. o cout object. o Allocate space for data to be used in the program. o The data can be changed

CSCI 123 Introduction to Programming Concepts in C++

Lecture 3. Input and Output. Review from last week. Variable - place to store data in memory. identified by a name should be meaningful Has a type-

Programming with C++ as a Second Language

Your first C++ program

Understanding main() function Input/Output Streams

1. Match each of the following data types with literal constants of that data type. A data type can be used more than once. A.

Makefiles Makefiles should begin with a comment section of the following form and with the following information filled in:

The sequence of steps to be performed in order to solve a problem by the computer is known as an algorithm.

Fundamental of Programming (C)

LOGO BASIC ELEMENTS OF A COMPUTER PROGRAM

Full file at

Characters, c-strings, and the string Class. CS 1: Problem Solving & Program Design Using C++

Chapter 1. C++ Basics. Copyright 2010 Pearson Addison-Wesley. All rights reserved

Chapter Two MULTIPLE CHOICE

VARIABLES & ASSIGNMENTS

Maciej Sobieraj. Lecture 1

Week 3: File I/O and Formatting 3.7 Formatting Output

This watermark does not appear in the registered version - Slide 1

Transcription:

Intro to Programming & C++ Unit 1 Sections 1.1-4 and 2.1-10, 2.12-13, 2.15-17 CS 1428 Spring 2019 Jill Seaman 1.1 Why Program? Computer programmable machine designed to follow instructions Program a set of instructions, stored in computer memory, to make the computer do something Programmer person who writes instructions (programs) to make computer perform a task SO, without programmers, no programs; without programs, a computer cannot do anything!1!2 Why Learn to Program? Programming is a fundamental part of computer science. Having an understanding of programming helps you to understand the strengths and limitations of computers. It helps you become a more intelligent user of computers. It can be fun! It helps you to develop problem solving skills. 1.2 Computer Systems: Hardware and Software! Hardware: the physical components that a computer is made of.! Software: the programs that run on a computer!3!4

Hardware Components Illustrated Hardware Components! Central Processing Unit (CPU)! Arithmetic Logic Unit (math, comparisons, etc)! Control Unit (processes instructions)! Main Memory (RAM): Fast, expensive, volatile! Secondary Storage: Slow, cheap, long-lasting! Input Devices: keyboard, mouse, camera! Output Devices: screen, printer, speakers!5!6 1.3 Programs and Programming Languages! A program is a set of instructions that the computer follows to perform a task! An algorithm: A set of well-defined steps for performing a task or solving a problem. A step by step ordered procedure that solves a problem in a finite number of precise steps.! An algorithm can be in any language (English, C++, machine code, etc).!7 Example (algorithm) 1.Display on screen: how many hours did you work? 2.Wait for user to enter number, store it in memory 3.Display on screen: what is your pay rate (per hour)? 4.Wait for user to enter rate, store it in memory 5.Multiply hours by rate, store result in memory 6.Display on screen: you have earned $xx.xx where xx.xx is result of step 5. Note: Computer does not speak English, it only understands its own machine language!8

Programming Languages! High Level Languages (like C++): Translation Process Words, symbols, numbers, i.e. c = a + b Easier for humans to read and use! Low Level Languages: Load the number from location 2001 into the CPU, Load the number from location 2002 into the CPU, Add the two numbers, Store the result in location 2003 Instructions are encoded as a sequence of 1's and 0 s Computer understands this language (often called Machine Language).! Programs written in high level language must be translated to machine language.!9!10 1.4 What is a Program Made of?! Key Words Have a special meaning in C++ May only be used for their intended purpose. Also known as reserved words. Examples: using, namespace, int, double, and return! Programmer-Defined Identifiers Names made up by the programmer Not part of the C++ language Used to represent various things: variables (memory locations), functions, etc.!11! Operators More Program Elements Used to perform operations on data Examples: << >> = *! Punctuation Characters that mark the end of a statement, or that separate items in a list Examples:, ;!12

! Syntax More Program Elements The rules of grammar that must be followed when writing a program Controls the use of key words, operators, programmer-defined symbols, and punctuation! Lines and Statements A line is a single line in the body of a program A statement is a complete instruction that causes the computer to perform some action Example: cout << "How many hours did you work? ";!13 Variables! Variable: symbolic names that represent locations in the computer s memory (RAM). The data may change while program is running!! Each variable can store only one type of information (for example characters, integers, real numbers).! Variable Definition (or Declaration) A statement that causes a variable to be created in memory. The data type of a variable must be indicated in the variable definition. Example: double hours; (double is a data type corresponding to real numbers)!14 2.1 The Parts of a C++ Program // sample C++ program #include <iostream> using namespace std; int main() { cout << "Hello, world!"; return 0; } Parts of a C++ Program! Comment: // ignored by compiler notes to human reader! Preprocessor Directive: #include <iostream> compiler inserts contents of file iostream here required because cout is defined in iostream! using namespace std; allows us to write cout instead of std::cout!15!16

Parts of a C++ Program int main () start of function (group of statements) named main the starting point of the program {} contains the body of the function cout << Hello, world! ; statement to display message on screen return 0; quit and send value 0 to OS (means success!) 2.2 The cout Object! cout: short for console output a stream object: represents the contents of the screen! <<: the stream insertion operator use it to send data to cout (to be output to the screen) cout << This is an example. ;! when this instruction is executed, the console (screen) looks like this: This is an example. Note: the do not show up in the output!17!18 The endl manipulator more examples! endl: short for end line send it to cout when you want to start a new line of output. cout << Hello << endl << there! ;! or you can use the newline character: \n cout << Hello \nthere! ;! Either way the output to the screen is: cout << Hello << there! ; Hello there! cout << Hello ; cout << there! ; Hello there! Hello there!!19 cout << The best selling book on Amazon\n is \ The Help\ ; The best selling book on Amazon is The Help!20

2.3 The #include Directive! Inserts the contents of another file into the program. #include <iostream>! For example, cout is not part of the core C++ language, it is defined in the iostream file.! Any program that uses the cout object must contain the extensive setup information found in iostream.! The code in iostream is C++ code.!21 2.4 Variables, Literals and Assignment Statements! Variable: named location in main memory! A variable declaration has a name and datatype The data type indicates the kind of data it can contain. The identifier is a name of your choosing. Note the book calls it a variable definition.! A variable must be declared before it can be used!!! Example variable declarations: int somenumber; char firstletter;!22 Literals! A literal represents a constant value used in a program statement.! Numbers: 0, 34, 3.14159, -1.8e12, etc.! Strings (sequence of keyboard symbols): Hello, This is a string 100 years, 100, Y, etc.! NOTE: These are different: 5 5 Assignment Statements! An assignment statement uses the = operator to store a value in an already declared variable. somenumber = 12;! When this statement is executed, the computer stores the value 12 in memory, in the location named somenumber.! The variable receiving the value must be on the left side of the = (the following does NOT work): 12 = somenumber; //This is an ERROR!23!24

Example program using a variable 2.5 Identifiers #include <iostream> using namespace std; int main() { int number; } number = 100; cout << The value of the number is << number << endl; return 0;! An identifier is a name for some program element (like a variable).! Rules: May not be a keyword (see Table 2.4 in the book) First character must be a letter or underscore Following characters must be letters, numbers or underscores.! Identifiers are case-sensitive: myvariable is not the same as MyVariable output screen: The value of the number is 100!25!26 Data Types! Variables are classified according to their data type.! The data type determines the kind of information that may be stored in the variable.! A data type is a set of values.! Generally two main (types of) data types: Numeric Character-based C++ Data Types! int, short, long whole numbers (integers)! float, double real numbers (with fractional amounts, decimal points)! bool logical values: true and false! char a single character (keyboard symbol)! string!27 any text, a sequence of characters!28

2.6 Integer Data Types! Whole numbers such as 12, 7, and -99! Typical ranges (may vary on different systems): Data Type: Range of values: short -32,768 to 32,767 int -2,147,483,648 to 2,147,483,647 2.7 The char Data Type! All the keyboard and printable symbols.! Literal values: A 5? b characters are indicated using single quotes! Numeric value of character from the ASCII character set is stored in memory: long -2,147,483,648 to 2,147,483,647! Example variable declarations: short dayofweek; long distance; int xcoordinate; C++ code segment: char letter; letter = C'; cout << letter << endl; MEMORY: letter 67 Appendix B shows the ASCII code values OUTPUT: C!29!30 2.8 The C++ string class Sequences of characters May require the string header file: To declare string variables in programs: string firstname, lastname; To assign literals to variables: firstname = "George"; lastname = "Washington"; To display via cout cout << firstname << " " << lastname; OUTPUT: George Washington #include <string>!31 2.9 Floating-Point Data Types! Real numbers such as 12.45, and -3.8! Typical ranges (may vary on different systems): Data Type: float double Range of values: +/- 3.4e +/- 38 (~7 digits of precision) +/- 1.7e +/- 308 (~15 digits of precision)! Floating-point literals can be represented in Fixed point (decimal) notation: 31.4159 0.0000625 E (scientific) notation: 3.14159E1 6.25e-5!32

2.10 The bool Data Type! The values true and false.! Literal values: true, false! (false is equivalent to 0, true is equivalent to 1) int main() { bool boolvalue; boolvalue = true; cout << boolvalue << endl; boolvalue = false; cout << boolvalue << endl; return 0; } output screen: 1 0!33 2.12 More about Variable Assignments and Initialization! To initialize a variable means to assign it a value when it is declared: int length = 12;! You can define and initialize multiple variables at once (and change them later) : int length = 12, width = 5, area; area = 35; length = 10; area =40;!34 2.13 Scope! The scope of a variable is the part of the program in which the variable can be accessed.! A variable cannot be used before it is declared. // This program can't find its variable. #include <iostream> using namespace std; int main() { cout << value; // ERROR! value not declared yet! 2.15 Comments Notes of explanation used to document parts of the program Intended for humans reading the source code of the program: Indicate the purpose of the program Describe the use of variables Explain complex sections of code Are ignored by the compiler } int value = 100; return 0;!35!36

Single and Multi-Line Comments Single-Line comments begin with // through to the end of line: int length = 12; // length in inches int width = 15; // width in inches int area; // calculated area // calculate rectangle area area = length * width; Multi-Line comments begin with /*, end with */ /* this is a multi-line comment */ int area; /* calculated area */!37 2.16 Named Constants Named constant : variable whose value cannot be changed during program execution Used for representing constant values with descriptive names: const double TAX_RATE = 0.0675; const int NUM_STATES = 50; Note: initialization required. Often named in uppercase letters (see style guidelines)!38 2.17 Programming Style The visual organization of the source code Includes the use of spaces, tabs, and blank lines Includes naming of variables, constants. Includes where to use comments. Purpose: improve the readability of the source code Programming Style Common elements to improve readability: Braces { } aligned vertically Indentation of statements within a set of braces Blank lines between declaration and other statements Long statements intentionally broken up over multiple lines. See the Style Guidelines on the class website. You must follow these in your programming assignments.!39!40