COMS 261 Computer Science I

Slides:



Advertisements
Similar presentations
Problem Solving and Program Design Programming. COMP102 Prog Fundamentals I : Problem Solving and Program Design/Slide 2 Problem Solving Process l Define.
Advertisements

C++ Basics Part I Part II. Slide 2 Part I: basics.
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.
C++ Basics. COMP104 C++ Basics / Slide 2 Introduction to C++ * C++ is a programming language for manipulating numbers and user-defined objects. * C++
J. P. Cohoon and J. W. Davidson © 1999 McGraw-Hill, Inc. The Fundamentals of C++ Basic programming elements and concepts.
1 CS 105 Lecture 3 Constants & Expressions Wed, Jan 26, 2011, 4:15 pm.
The Fundamentals of C++ Basic programming elements and concepts JPC and JWD © 2002 McGraw-Hill, Inc.
Introduction To C++ Programming 1.0 Basic C++ Program Structure 2.0 Program Control 3.0 Array And Structures 4.0 Function 5.0 Pointer 6.0 Secure Programming.
Basic Elements of C++ Chapter 2.
Section 2 - More Basics. The char Data Type Data type of a single character Example char letter; letter = 'C';
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.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Data representation and Data Types Variables.
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.
J. P. Cohoon and J. W. Davidson © 1999 McGraw-Hill, Inc. Modifying objects Operators and Expressions.
Modifying objects Operators and Expressions JPC and JWD © 2002 McGraw-Hill, Inc.
Chapter 2: Using Data.
The Fundamentals of C++ Chapter 2: Basic programming elements and concepts JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
C++ Programming: Basic Elements of C++.
THE BASICS OF A C++ PROGRAM EDP 4 / MATH 23 TTH 5:45 – 7:15.
Computer Engineering 1 st Semester Dr. Rabie A. Ramadan 3.
Chapter 3: Modifying objects Operators and Expressions JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
Chapter 2: Introduction to C++. Language Elements Keywords Programmer-defined symbols (identifiers) Operators Punctuation Syntax Lines and Statements.
1 COMS 261 Computer Science I Title: Functions Date: October 12, 2005 Lecture Number: 17.
Computing and Statistical Data Analysis Lecture 2 Glen Cowan RHUL Physics Computing and Statistical Data Analysis Variables, types: int, float, double,
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 9, 2005 Lecture Number: 6.
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
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++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved Chapter Two: Fundamental Data Types Slides by Evan Gallagher.
Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types.
1 Comments Allow prose or commentary to be included in program Importance Programs are read far more often than they are written Programs need to be understood.
Recap……Last Time [Variables, Data Types and Constants]
CMPSC 121- Spring 2015 Lecture 6 January 23, 2015.
12/14/2016CS150 Introduction to Computer Science 1 Announcements  Website is up!   All lecture slides, assignments,
C++ Basics Programming. COMP104 Lecture 5 / Slide 2 Introduction to C++ l C is a programming language developed in the 1970s with the UNIX operating system.
Literals A literal (sometimes called a constant) is a symbol which evaluates to itself, i.e., it is what it appears to be. Examples: 5 int literal
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
ICS102 Lecture 1 : Expressions and Assignment King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 15, 2004 Lecture Number: 11.
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.
Bill Tucker Austin Community College COSC 1315
Variables, Identifiers, Assignments, Input/Output
Today Variable declaration Mathematical Operators Input and Output Lab
Chapter Topics The Basics of a C++ Program Data Types
Chapter 7: Expressions and Assignment Statements
Chapter 1: Introduction to computers and C++ Programming
Chapter 2 Introduction to C++ Programming
Computing and Statistical Data Analysis Lecture 2
Programming Fundamentals
Computing Fundamentals
Basic Elements of C++.
Chapter 7: Expressions and Assignment Statements
Basic Elements of C++ Chapter 2.
Basic Notions Review what is a variable? value? address? memory location? what is an identifier? variable name? keyword? what is legal identifier? what.
C++ fundamentals Lecture 1, Chapter 2 – pp /22/2018 Y K Choi.
CS150 Introduction to Computer Science 1
C++ Basics.
Variables, Identifiers, Assignments, Input/Output
Wednesday 09/23/13.
Introduction to C++ Programming
CS150 Introduction to Computer Science 1
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
COMS 261 Computer Science I
Primitive Types and Expressions
C++ Programming Basics
COMS 261 Computer Science I
Modifying Objects Assignment operation Assignment conversions
The Fundamentals of C++
Presentation transcript:

COMS 261 Computer Science I Title: C++ Fundamentals Date: September 10, 2004 Lecture Number: 9

Announcements Labs are now due on Tuesday at midnight! Take the time to understand what we did in lab

Review Standard data types Names Strings (not part of C++, but are common) Real (float) Names

Outline Keywords Identifiers Arithmetic Operators Precedence Assignment Statements Assignment Conversions New Types Increment and Decrement

C++ Keywords Keywords are words reserved as part of the language int, return, float, double,this Colored blue in CodeWarrior They cannot be used by the programmer to name (label) things in the program They consist of lowercase letters only They have special meaning to the compiler

Identifiers Identifiers should be Short enough to be reasonable to type (single word is norm) Standard abbreviations are fine (but only standard abbreviations) Long enough to be understandable When using multiple word identifiers capitalize the first letter of each word Examples min, temperature, cameraAngle, currentNumPoints

Definitions All objects used in a program must be defined before they are used An object definition includes Type specifier Name General definition form Our convention is one definition per statement!

Examples char Response; int MinElement; float Score; float Temperature; int i; int n; char c; float x; Objects are uninitialized with this definition form (Value of a object is whatever is in its assigned memory location)

Arithmetic Operators Common Addition + Subtraction - Multiplication * Division / Mod % Write m*x + b not mx + b

Arithmetic Operators Note No exponentiation operator Single division operator Integer and float Operators are overloaded to work with more than one type of object C++ Terminology

Integer Division Integer division produces an integer result Examples Truncates Throws away part of the result Examples 3 / 2 evaluates to 1 4 / 6 evaluates to 0 10 / 3 evaluates to 3

Mod Operator Produces the remainder of the division Examples 5 % 2 evaluates to 1 12 % 4 evaluates to 0 4 % 5 evaluates to 4

Operators and Precedence Consider mx + b which of the following is it equivalent to (m * x) + b m * (x + b) Operator precedence tells how to evaluate expressions

Operators and Precedence Standard precedence order () Evaluate first, if nested innermost done first * / % Evaluate second. If there are several, then evaluate from left-to-right + - Evaluate third. If there are several, then evaluate from left-to-right

Operator Precedence Examples 20 - 4 / 5 * 2 + 3 * 5 % 4 (4 / 5) 20 - 4 / 5 * 2 + 3 * 5 % 4 (4 / 5) ((4 / 5) * 2) ((4 / 5) * 2) (3 * 5) ((4 / 5) * 2) ((3 * 5) % 4) (20 -((4 / 5) * 2)) ((3 * 5) % 4) (20 -((4 / 5) * 2)) + ((3 * 5) % 4)

Defining and Initializing When an object is defined using the basic form, the memory allotted to it contains random information Better idea to specify its desired value at the same time Exception is when the next statement is an extraction for the object Remember our convention of one definition per statement!

Examples int FahrenheitFreezing = 32; char FinalGrade = 'A'; cout << "Slope of line: "; float m; cin >> m; cout << "Intercept: "; float b; cin >> b; cout << "X value of interest: "; float x; cin >> x; float y = (m * x) + b;

Memory Depiction 1001 1002 1003 1004 1005 1006 1007 1008 1009 100a 100b . ffff

Memory Depiction float y = 12.5; float occupies 4 bytes int Temperature = 32; char Letter = 'c'; 1001 1002 y 1003 12.5 1004 1005 1006 1007 Temperature 32 1008 1009 Letter c 100a 100b . ffff

Assignment Statement Basic form Action object = expression ; Celsius = (Fahrenheit - 32) * 5 / 9; y = m * x + b; Action Expression is evaluated Expression value stored in object

Definition int NewStudents = 6;

Definition int NewStudents = 6; int OldStudents = 21;

Definition int NewStudents = 6; int OldStudents = 21; int TotalStudents;

Assignment Statement int NewStudents = 6; int OldStudents = 21; int TotalStudents; TotalStudents = NewStudents + OldStudents;

Assignment Statement int NewStudents = 6; int OldStudents = 21; int TotalStudents; TotalStudents = NewStudents + OldStudents;

Assignment Statement int NewStudents = 6; int OldStudents = 21; int TotalStudents; TotalStudents = NewStudents + OldStudents; OldStudents = TotalStudents;

Assignment Statement int NewStudents = 6; int OldStudents = 21; int TotalStudents; TotalStudents = NewStudents + OldStudents; OldStudents = TotalStudents;

Consider int Value1 = 10;

Consider int Value1 = 10; int Value2 = 20;

Consider int Value1 = 10; int Value2 = 20; int Hold = Value1;

Consider int Value1 = 10; int Value2 = 20; int Hold = Value1; Value1 = Value2;

Consider int Value1 = 10; int Value2 = 20; int Hold = Value1; Value1 = Value2;

Consider int Value1 = 10; int Value2 = 20; int Hold = Value1; Value1 = Value2; Value2 = Hold;

Consider int Value1 = 10; int Value2 = 20; int Hold = Value1; Value1 = Value2; Value2 = Hold; We swapped the values of objects Value1 and Value2 using Hold as temporary holder for Value1’s starting value!

Incrementing int i = 1; i = i + 1; Assign the value of expression i + 1 to i Evaluates to 2

Const Definitions Modifier const indicates that an object cannot be changed Object is read-only Useful when defining objects representing physical and mathematical constants const float Pi = 3.1415;

Const Definitions Value has a name that can be used throughout the program const int SampleSize = 100; Makes changing the constant easy Only need to change the definition and recompile

Assignment Conversions Floating-point expression assigned to an integer object is truncated Integer expression assigned to a floating-point object is converted to a floating-point value

Assignment Conversions float y = 2.7; int i = 15; int j = 10; i = y; // i is now 2 cout << i << endl; y = j; // y is now 10.0 cout << y << endl;

Assignment Conversions float y = 2.7; int i = 15; int j = 10; i = y; // i is now 2 cout << i << endl; y = j; // y is now 10.0 cout << y << endl;

Nonfundamental Types Additions to the language C++ permits the definition of new types and classes A class is a special kind of type Class objects typically have Data members that represent attributes and values Member functions for object inspection and manipulation

Nonfundamental Types Members are accessed using the selection operator “.” j = s.size(); Auxiliary functions for other behaviors Libraries often provide special-purpose types and classes Programmers can also define their own types and classes

Examples Standard Template Library (STL) provides class string Used to represent a sequence of characters as a single object Some definitions string Name = "Joanne"; string DecimalPoint = "."; string empty = ""; string copy = name; string Question = '?'; // illegal

Nonfundamental Types To access a library use a preprocessor directive to add its definitions to your program file #include <string>

Nonfundamental Types The using statement makes syntax less clumsy Without it std::string s = "Sharp"; std::string t = "Spiffy"; With it using namespace std; //std contains string string s = "Sharp"; string t = "Spiffy";

Compound Assignment C++ has a large set of operators for applying an operation to an object and then storing the result back into the object int i = 3; i += 4; // i is now 7 cout << i << endl; float a = 3.2; a *= 2.0; // a is now 6.4 cout << a << endl;

Increment and Decrement C++ has special operators for incrementing or decrementing an object by one int k = 4; ++k; // k is 5 k++; // k is 6 cout << k << endl; int i = k++; // i is 6, k is 7 cout << i << " " << k << endl; int j = ++k; // j is 8, k is 8 cout << j << " " << k << endl;

Class string Some string member functions size() determines number of characters in the string string Saying = "Rambling with Gambling"; cout << Saying.size() << endl; // 22 substr() determines a substring (first position has index 0) string Word = Saying.substr(9, 4); // with find() computes the position of a subsequence int j = Saying.find("it"); // 10 int k = Saying.find("its"); // ?

Class string Auxiliary functions and operators getline() extracts the next input line string Response; cout << "Enter text: "; getline(cin, Response, '\n'); cout << "Response is \"" << Response << "\"” << endl; Example run Enter text: Want what you do Response is "Want what you do"

Class string Auxiliary operators + string concatenation string Part1 = "Me"; string Part2 = " and "; string Part3 = "You"; string All = Part1 + Part2 + Part3; += compound concatenation assignment string ThePlace = "Brooklyn"; ThePlace += ", NY";

#include <iostream> using namespace std; int main() { cout << "Enter the date in American format: “ << "(e.g., January 1, 2001) : "; string Date; getline(cin, Date, '\n'); int i = Date.find(" "); string Month = Date.substr(0, i); int k = Date.find(","); string Day = Date.substr(i + 1, k - i - 1); string Year = Date.substr(k + 2, Date.size() - 1); string NewDate = Day + " " + Month + " " + Year; cout << "Original date: " << Date << endl; cout << "Converted date: " << NewDate << endl; return 0; }