// A simple C++ program # include using namespace std; int main () { cout << “Welcome to CS1005! \n”; return 0; }

Slides:



Advertisements
Similar presentations
Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
Advertisements

CS 6301 Lecture 2: First Program1. CS Topics of this lecture Introduce first program  Explore inputs and outputs of a program Arithmetic using.
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
Overview Order of presentation different than published handouts Run a program on ccc Finish Arithmetic operations Data types integer char floating point.
Announcements Quiz 1 Next Week. int : Integer Range of Typically -32,768 to 32,767 (machine and compiler dependent) float : Real Number (i.e., integer.
1 9/20/06CS150 Introduction to Computer Science 1 Review: Exam 1.
Data types and variables
How Create a C++ Program. #include using namespace std; void main() { cout
Chapter 2 Data Types, Declarations, and Displays
CSCI/CMPE 4341 Topic: Programming in Python Chapter 3: Control Structures (Part 1) – Exercises 1 Xiang Lian The University of Texas – Pan American Edinburg,
Basic Elements of C++ Chapter 2.
Introduction to C++ Programming
C++ Operators CS242 COMPUTER PROGRAMMING T.Banan Al-Hadlaq.
Chapter 01: Introduction to Computer Programming
Introduction to C++ Programming
COMPUTER SCIENCE I C++ INTRODUCTION
Chapter 2 Data Types, Declarations, and Displays.
Objectives You should be able to describe: Data Types
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
CSC 125 Introduction to C++ Programming Chapter 2 Introduction to C++
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Data representation and Data Types Variables.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
CS1 Lesson 2 Introduction to C++ CS1 Lesson 2 -- John Cole1.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Arithmetic Operations. Review function statement input/output comment #include data type variable identifier constant declaration.
Week 1 Algorithmization and Programming Languages.
C++ Programming: Basic Elements of C++.
Knowledge Base C++ #include using std namespace; int main(){} return 0 ; cout
1 INTRODUCTION TO PROBLEM SOLVING AND PROGRAMMING.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Introduction to C++ Basic Elements of C++. C++ Programming: From Problem Analysis to Program Design, Fourth Edition2 The Basics of a C++ Program Function:
CSC 107 – Programming For Science. The Week’s Goal.
1.  By the end of this section you should: ◦ Understand what the variables are and why they are used. ◦ Use C++ built in data types to create program.
Arithmetic Expressions in C++. CSCE 1062 Outline Data declaration {section 2.3} Arithmetic operators in C++ {section 2.6} Mixed data type arithmetic in.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 2: Basic Elements of C++
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Chapter 3 – Variables and Arithmetic Operations. First Program – volume of a box /************************************************************/ /* Program.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 2 September 3, 2009.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
1 st Semester Module2 Basic C# Concept อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
1 09/10/04CS150 Introduction to Computer Science 1 What Actions Do We Have Part 2.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
Chapter 2: Basic Elements of C++
Chapter Topics The Basics of a C++ Program Data Types
Computer Programming BCT 1113
Introduction to C++ Programming
Chapter 2 Introduction to C++ Programming
Variables Mr. Crone.
Basic Elements of C++.
Revision Lecture
Variables and Arithmetic Operators in JavaScript
Basic Elements of C++ Chapter 2.
Introduction to C++ Programming
1.13 The Key Software Trend: Object Technology
Variables Kingdom of Saudi Arabia
CS111 Computer Programming
Variables T.Najah Al_Subaie Kingdom of Saudi Arabia
Introduction to C++ Programming
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Presentation transcript:

// A simple C++ program # include using namespace std; int main () { cout << “Welcome to CS1005! \n”; return 0; }

General comments Use whitespace to make it more readable Common programming errors Don’t reinvent the wheel Maximum warnings on compiler

What not to do with Whitespace #include using namespace std;int main(){cout<<"Welcome to CS1005!\n";return 0;}

// This program adds two numbers and displays the result // on the screen # include using namespace std; int main () { int result; result = 2 + 3; cout << “The sum of is “ << result << endl; return 0; }

Data Type Determines how much memory is needed to store this information Determines what sort of information the data type represents Determines which operations are legal on this information

Rules for Identifiers Must start with a letter May only contain letters, digits and underscore character Must not be a keyword C++ is case sensitive. C++ allows any length Our standard - start with small letter, capitalize first letter of each word (avgTemp) Should be meaningful. Reflect it’s purpose (avgGrade, worstGrade, bestGrade)

Arithmetic Operators C++ OperationArithmetic Operator Algebraic Expression C++ Expression Addition+a + b Subtraction-a - b Multiplication*ab or a x ba * b Division/a/b or a or a ba/b Modulus%a mod ba % b

Rules of precedence for Arithmetic Operations OperatorOperationOrder of evaluation ( )ParenthesesEvaluated first. If the parentheses are nested the innermost pair is evaluated first. If there are several pairs on the same level they are evaluated left to right. * / %Division, mulitiplication, modulus Evaluated second. If there are several they are evaluated left to right. + -Addtition, subtractionEvaluated last. If there are several, they are evaluated left to right

Exercise 2 int a, x, b, c, y; a = 2; x = 5; b = 3; c = 7; y = a * x * x + b * x + c; What is y after this program executes? Exercise 1 int num1, num2, num3; num1 = 3; num2 = num1; num1 = num2 + 1; num3 = num2 * num1 - 6; What are the values for num1, num2 and num3 after this program executes?

Relational Operators Standard algebraic expression C++ operatorExample of C++ expression Meaning >>a > ba is greater than b <<a < ba is less than b >=a >=ba is greater than or equal to b <=a <= ba is less than or equal to b ===a == ba is equal to b !=a != ba is not equal to b

Logical Operators OperatorNameExampleTrue if !Not!xx is false &&Andx && yx and y are both true ||Orx || yx or y is true

OperatorType ()parentheses !not * / %multiplication, division, modulus + -addition, subtraction =relationals == !=equality &&and ||or Precedence chart

Data Type Determines how much memory is needed to store this information Determines what sort of information the data type represents Determines which operations are legal on this information

Determining Size and Range // Determining the size and range of the the signed long integer type. #include using namespace std; int main() { cout << "signed long type is " << sizeof (signed long) << "bytes\n"; cout << "largest value of signed long type is " << LONG_MAX << endl; cout << "smallest value of signed long type is " << LONG_MIN << endl; return 0; }

Floating Point Data Types TypeTypical rangeTypical precision float10 e -38 to 10 e 38 6 digits double10 e -308 to 10 e digits long double10 e to 10 e digits

Things to remember about floating point data types Mantissa is limited in size so value may not be exact Arithmetic is faster using integer types than floating point types Don’t use equality operators Modulus operator not valid for floating point Don’t use floating point #’s for loop control

Mixing float and integer types #include using namespace std; int main() { float average; int numberOfGrades = 2, totalOfGrades = 9; average = totalOfGrades / numberOfGrades; cout << "The average is " << average << endl; return 0; }

Data Types Summary Given the following declarations, determine the value of the variable on the left-hand- side of each assignment statement. int int1, int2, int3; float f1=1.0, f2=2.5, f3=5.0; double d1, d2; char ch1, ch2; int1 = 5; d2 = 5.0; ch1 = '5'; int3 = f2; int2 = int1 / int3; d1 = f3 - -f1 * 6 / int * (int1 - d2); ch2 = ch1 - 2; int3 = 'a' - 'A'; ch1 = 'W' + int3;