Week 1-2 In this class, you will learn about: Program Structure, Actions and Data Types Data Type: Variables Output and Input action: function cout > Assignment.

Slides:



Advertisements
Similar presentations
Lecture 2 Introduction to C Programming
Advertisements

Introduction to C Programming
CS 6301 Lecture 2: First Program1. CS Topics of this lecture Introduce first program  Explore inputs and outputs of a program Arithmetic using.
 2005 Pearson Education, Inc. All rights reserved Introduction.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Introduction to C++ September 12, Today’s Agenda Quick Review Check your programs from yesterday Another Simple Program: Adding Two Numbers Rules.
True or false A variable of type char can hold the value 301. ( F )
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.
Correction of the Handout #include //Preprocessor using namespace std; int main (){ ………….. return 0; } A namespace is a named group of definitions. When.
Basic Elements of C++ Chapter 2.
Introduction to C++ Programming
VARIABLES, TYPES, INPUT/OUTPUT, ASSIGNMENT OPERATION Shieu-Hong Lin MATH/CS Department Chapel.
COMPUTER SCIENCE I C++ INTRODUCTION
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
A Variable is symbolic name that can be given different values. Variables are stored in particular places in the computer ‘s memory. When a variable is.
2440: 211 Interactive Web Programming Expressions & Operators.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
CH2 – Using Data. Constant Something which cannot be changed Data Type Format and size of a data item Intrinsic Data Types Pg. 47 – Table 2-1 Basic ones.
Chapter 2: Using Data.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Week 1 Algorithmization and Programming Languages.
C++ Programming: Basic Elements of C++.
1 INTRODUCTION TO PROBLEM SOLVING AND PROGRAMMING.
Introduction to C++ Basic Elements of C++. C++ Programming: From Problem Analysis to Program Design, Fourth Edition2 The Basics of a C++ Program Function:
THE BASICS OF A C++ PROGRAM EDP 4 / MATH 23 TTH 5:45 – 7:15.
Chapter 2: Introduction to C++. Language Elements Keywords Programmer-defined symbols (identifiers) Operators Punctuation Syntax Lines and Statements.
CHAPTER 2 C++ SYNTAX & SEMANTICS #include using namespace std; int main() { cout
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
Today’s Lecture  Literal  Constant  Precedence rules  More assignment rules  Program Style.
Module B - Computation1/61 Module-B-Computation Variables Basic Memory Operations Expressions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
2/4/2016Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 2 Simple C++ Programs.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 2 September 3, 2009.
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Bill Tucker Austin Community College COSC 1315
Chapter Topics The Basics of a C++ Program Data Types
Chapter 1: Introduction to computers and C++ Programming
Introduction to C++ Programming
Chapter 2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 2: Introduction to C++
Chapter 2 - Introduction to C Programming
Computing Fundamentals
Basic Elements of C++.
Chapter 2 - Introduction to C Programming
Chapter 2: Introduction to C++
Basic Elements of C++ Chapter 2.
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
C++ fundamentals Lecture 1, Chapter 2 – pp /22/2018 Y K Choi.
2.1 Parts of a C++ Program.
Introduction to C++ Programming
1.13 The Key Software Trend: Object Technology
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Introduction to C++ Programming
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Chapter 2: Introduction to C++.
Programs written in C and C++ can run on many different computers
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Capitolo 1 – Introduction C++ Programming
Chapter 2 - Introduction to C Programming
C++ Programming Basics
Introduction to C Programming
Presentation transcript:

Week 1-2 In this class, you will learn about: Program Structure, Actions and Data Types Data Type: Variables Output and Input action: function cout > Assignment Simple calculations Reference: Programming with C++, Theory and Problems of, Second Edition; John R Hubbard (McGraw Hill, 2000, Schaum's Outline Series)

Program Structure, Actions and Data Types C++ Program Structure #include main() { cout<<"HEY, you, I'm alive!"; // output a string. } –The program is saved as a file that should be named. In this example, the name of the file is cout1a.cpp..cpp –The #include is called a preprocessor directive which tells the computer compiler to include the header file iostream.h in our program cout1a.cpp. –The next part is main(). –The body of the –The lines for the body of a program is called statements, for example, cout –Comment lines

Data Type: Variables Variables Name of a variable –A variable has a name, which is given by the programmer by declaration or definition. When the name is given, the memory for it is specified. Rules for creating names of variables –Rule 1: a name starts with a letter. –Rule 2: a name is unique, which should be different from other words in the program. –Rule 3: names are sensitive to letters upper case or lower case. Doug is not equivalent to doug. –Rule 4: names should be meaningful.

Data Type: Variables For example, int alice; name of variable: alice type: int(eger) tail: ;

Data Types

Part of Integral Type

Float type example It shows that the 32 bits it uses to store a float are partitioned into 3 parts: 23 bits for the mantissa, 8 bits for the exponent, and 1 bit for the sign. The 23-bit mantissa produces a floating-point value with 6 significant digits, and the 8-bit exponent yields a range in magnitude from about 10–37 to about 3 × i.e., < |x| < 300,000,000,000,000,000,000,000,000,000,000,000,000 for any variable x declared to have type float.

A new variable : char (character data) Definition –A char variable is defined to hold a single character that appears on the keyboard. –Difference between a char and a string is that a char is a single character while a string is a collection of serial characters. Declaration –char c; Input a char variable from keyboard A space cannot be input from keyboard

A new variable : char (character data)

Output action: function cout<< More actions by using a back slash \ The sign \ is not a normal character but a control character to instruct the computer to take an action corresponding to next character. See table below, Escape characterEffect\nNewline\tHorizontal tab\rCarriage return\bBackspace\aBell or beep\\Backslash\”Double quote\’Single quote\?Question mark

Input action: input function cin>> Assignment We have seen that one way of assigning a variable with a value is to input a number of digits on keyboard. Another important way is to use an assignment statement. For example (assignment1.cpp),

Prefix and postfix increment Example:k = ++x;//prefix increment is equivalent to: x = x + 1; //increment n first k = x;//assign x’s value to k Example:k = x++;//postfix increment is equivalent to k = x;//assign x’s value to k x = x + 1;//and then increment x

Applying the Pre-increment and Post-increment Operators int main() { // shows the difference between m++ and ++m: int m,n; m = 44; n = ++m; // the pre-increment operator is applied to m cout << "m = " << m << ", n = " << n << endl; m = 44; n = m++; // the post-increment operator is applied to m cout << "m = " << m << ", n = " << n << endl; }

COMPOSITE ASSIGNMENT OPERATORS The standard assignment operator in C++ is the equals sign =. In addition to this operator, C++ also includes the following composite assignment operators: +=, -=, *=, /=, and %=. When applied to a variable on the left, each applies the indicated arithmetic operation to it using the value of the expression on the right.

Applying Composite Arithmetic Assignment Operators int main() { // tests arithmetic assignment operators: int n=22; cout << "n = " << n << endl; n += 9; // adds 9 to n cout << "After n += 9, n = " << n << endl; n -= 5; // subtracts 5 from n cout << "After n -= 5, n = " << n << endl; n *= 2; // multiplies n by 3 cout << "After n *= 2, n = " << n << endl; n / = 3; // divides n by 9 cout << "After n /= 3, n = " << n << endl; n %= 7; // reduces n to the remainder from dividing by 4 cout << "After n %= 7, n = " << n << endl; }

Simple calculations

Integer Calculation Example int main() { // tests operators +,-,*,/,and %: int m=54; int n=20; cout << "m = " << m << " and n = " << n << endl; cout << "m+n = " << m+n << endl; // = 74 cout << "m-n = " << m-n << endl; // = 34 cout << "m*n = " << m*n << endl; // 54*20 = 1080 cout << "m/n = " << m/n << endl; // 54/20 = 2 cout << "m%n = " << m%n << endl; // 54%20 = 14 Return 0; }