Arash Rafiey TA : Xu Cheng Office Hours M-W 10:30 – 11:30.

Slides:



Advertisements
Similar presentations
Chapter 2: Using Objects Part 1. To learn about variables To understand the concepts of classes and objects To be able to call methods To learn about.
Advertisements

Types, Variables and Operators Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2013.
CSE202: Lecture 2The Ohio State University1 Variables and C++ Data Types.
Chapter 2: Basic Elements of C++
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
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.
Loops different types: – for – while – etc.. For Loop for(initialValue; test-expression ; update- expression) { BODY }
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
1 CS 105 Lecture 3 Constants & Expressions Wed, Jan 26, 2011, 4:15 pm.
How Create a C++ Program. #include using namespace std; void main() { cout
Chapter 2: Basic Elements of C++
Overview of C++ Chapter 2 in both books programs from books keycode for lab: get Program 1 from web test files.
Chapter 2: Introduction to C++.
Chapter 2: Basic Elements of C++
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.
CHAPTER 2 BASIC ELEMENTS OF C++. In this chapter, you will:  Become familiar with the basic components of a C++ program, including functions, special.
PRINCIPLES OF PROGRAMMING Revision. A Computer  A useful tool for solving a great variety of problems.  To make a computer do anything (i.e. solve.
VARIABLES, TYPES, INPUT/OUTPUT, ASSIGNMENT OPERATION Shieu-Hong Lin MATH/CS Department Chapel.
 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++
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
CS31: Introduction to Computer Science I Discussion 1A 4/9/2010 Sungwon Yang
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
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.
C++ Programming: Basic Elements of C++.
Introduction to C++ Basic Elements of C++. C++ Programming: From Problem Analysis to Program Design, Fourth Edition2 The Basics of a C++ Program Function:
Lecture 3: The parts of a C++ program (Cont’d) Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
THE BASICS OF A C++ PROGRAM EDP 4 / MATH 23 TTH 5:45 – 7:15.
PHY-102 SAPVariables and OperatorsSlide 1 Variables and Operators In this section we will learn how about variables in Java and basic operations one can.
C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello.
CHAPTER 2 C++ SYNTAX & SEMANTICS #include using namespace std; int main() { cout
Computing and Statistical Data Analysis Lecture 2 Glen Cowan RHUL Physics Computing and Statistical Data Analysis Variables, types: int, float, double,
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Chapter 2: Introduction to C++. Outline Basic “Hello World!!” Variables Data Types Illustration.
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++
CHAPTER 6 ARRAYS IN C++ 2 nd Semester King Saud University College of Applied studies and Community Service CSC 1101 By: Fatimah Alakeel Edited.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
Lecture 5 Computer programming -1-. Input \ Output statement 1- Input (cin) : Use to input data from keyboard. Example : cin >> age; 2- Output (cout):
1 CSC 1111 Introduction to Computing using C++ C++ Basics (Part 1)
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
A Sample Program #include using namespace std; int main(void) { cout
Intro. to Computer Programming Eng. Nehal A. Mohamed Spring Semester-2016.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Chapter 2: Basic Elements of C++
Chapter Topics The Basics of a C++ Program Data Types
Chapter 1.2 Introduction to C++ Programming
Computing and Statistical Data Analysis Lecture 2
Documentation Need to have documentation in all programs
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Basic Elements of C++.
Programming fundamentals 2 Chapter 1:Array
Basic Elements of C++ Chapter 2.
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
2.1 Parts of a C++ Program.
Introduction to C++ Programming
Variables T.Najah Al_Subaie Kingdom of Saudi Arabia
Chapter 2: Introduction to C++.
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Primitive Types and Expressions
Presentation transcript:

Arash Rafiey TA : Xu Cheng Office Hours M-W 10:30 – 11:30

How Create a C++ Program

#include using namespace std; void main() { cout<<“Hello World”; } If your compiler gives error then

#include void main() { cout<<“Hello World”; cout<<endl; // for going to new line cout<<“ good bye”; } cout is an object for printing out some thing on the screen.

#include void main() { cout<<“Hello World \n”; cout<<“ good bye”; } we can use \n instead of cout<<endl;

How to run your program Using Linux or unix: g++ myFile.cpp./a.out g++ myFile.cpp myFile.out./myFile.out

Declare Statement & Variable Each variable must be declare before use Each variable has a type: For example int, char, float. int for Integer type; char for character like ‘A’; float for real number;

Example int LuckyNumber=17; float RealNumber; char a=‘A’;

Identifiers Identifier: name of a variable, function, or class Rules for identifiers in C++: 1 Can be made up of letters, digits, and the underscore (_) character 2 Cannot start with a digit 3 Cannot use other symbols such as ? or % 4 Spaces are not permitted inside identifiers 5 You cannot use reserved words 6 They are case sensitive

Self Check 1. What is the type of the values 0 and ‘0’? 2. Which of the following are legal identifiers? Greeting1 g void 101dalmatians Hello, World

Answer int and char Only the first two are legal identifiers

Syntax : Variable Definition typeName variableName = value; or typeName variableName; Example : int numbe = 12; Purpose: To define a new variable of a particular type and optionally supply an initial value

The Assignment Operator Assignment operator: = Not used as a statement about equality Used to change the value of a variable int number1; int number2, number3; number1=number2=number3=88;

number2=number2-1; number3=number2+1;

How to read a variable #include using namespace std; void main() { int number; cout<<“please enter a number \n”; cin>>number; number=number+1; cout<<“the number is “<<number; }

Integer Types The short, int and long Integer Types A short integer is at least 16 bits A int integer is at least as big as short A long integer is at least 32 bits and at least as big as int.

E.g. A 16-bit int might run from to The sizeof operator returns the size (in bytes)

#include int main() { int n_int = INT_MAX; short n_short = SHRT_MAX; long n_long = LONG_MAX; cout << “int is “ << sizeof (int) << “ bytes” << endl; cout << “short: “ << n_short << endl; cout << “long: “ << n_long << endl; return 0; }

int is 4 bytes Maximum values: –Short: –Long:

Characters and small integers #include int main() { char ch = ‘M’; // assign ASCII code int a = ch; cout << “ASCII code: “ << ch << “ is “ << a << endl; ch = ch + 1; a = ch; cout << “ASCII code: “ << ch << “ is “ << a << endl; return 0; }

Output M is 77 N is 78

Boolean type bool isReady = true; int ans = true; // ans assigned 1 int promise = false; // promise assigned 0 bool start = -100; // true bool stop = 0; // false

Floating-point number E.g We have float, double, long double

Arithmetic operators Summation: + Multiplication: * Division: / Subtraction: -

Operator Precedence int number = * 5 // 35 or 23? float logs = 120 / 4 * 5 // 150 or 6??

Type Casting Conversion between types: (typeName) value // c typeName (value) // c++ e.g. – cout << int(‘A’) << endl; // 65 – float one, –int two; one = ; two = (int) (int) 2.1;

Functions Building blocks of programs A function –Has an input and an output –Contains a set of instructions x = sqrt(16); // returns 4

Syntax typeName functionName (typeName varName_1, …) { BODY. return value; }

Examples of functions int sum (int firstValue, int secondValue) { int final; final = firstValue + secondValue; return final; } void main() { int a = 1; int b = 2; int total = sum(a,b); cout << “Total is: “ << total << endl; }

Arrays An array is a data form that holds several values of the same type Syntax: –typeName arrayName[value]; e.g: – int someArray[3]; Index starts from 0!!! –someArray[0] = 1; –someArray[2] = 2;

Initializations Rules for Arrays int array[4] = {2,6,4,5}; int secondArray[4]; secondArray[4] = {5,6,7,8}; // error!!! secondArray = array; //error!!! float Hotel[5] = {1.1, 2.2}; long total[500] = {0};

short array[] = {1,2,3,4};

String Series of characters stored in a consecutive bytes Create a string as an array but the last element must be the null character ‘\0’ – e.g char dog[5] = {‘b’,’e’,’a’,’u’,’x’}; //NOT!! char dog[6] = {‘b’,’e’,’a’,’u’,’x’,’\0’}; // STRING!!

More examples char dog[5] = “beaux”; //a better way null character is implicitly included. char name[]=“c++”; // compiler counts char boss[8]=“Arvind”;

#include int main() { int arSize = 20; char name[arSize]; char dessert[arSize]; cout << “Enter your name: “ << endl; cin >> name; cout << “Enter your dessert: “ << endl; cin >> dessert; cout << name << “ has selected: “ << dessert << endl; return 0; }

Another way to read a string #include int main() { int arSize = 20; char name[arSize]; char dessert[arSize]; cout << “Enter your name: “ << endl; cin.getline(name, arSize); // reads through newline cout << “Enter your dessert: “ << endl; cin.getline(dessert, arSize); cout << name << “ has selected: “ << dessert << endl; return 0; }

The String class To define strings more easily: –Include the string class: #include string str_1 = “jaguar”;

Assignment & Concatenation & Appending char char_1[20]; char char_2[20] = “jaguar”; string str_1; string str_2 = “panther”; char_1 = char_2; // INVALID!! str_1 = str_2; // VALID!!

Appending string str_3; str_3 = str_1 + str_2; // join str_1 and str_2 str_1 += str_2; // add str_2 to the end of str_1

More string operations: Copying: #include – strcpy(char_1, char_2); //copy char_2 into char_1 strcat(char_1, char_2); //append char_2 to char_1 Size of a string: – char charOne[20] = {‘p’,’i’,’e’}; – string strOne = “pie”; – strlen(charOne); –strOne.size();

Increment & Decrement Increment: ++ int a = 20; int b = 20; cout << “a: “ << a << “ b: “ << b << endl; cout << “a++: “ << a++ << “ ++b: “ << ++b << endl; cout << “a: “ << a << “ b: “ << b << endl;

a: 20 b:20 a++: 20 ++b:21 a:21 b:21 int x = 5; int y = ++x;

Decrement: -- Same rules as increment ( i.e. ++)

Loops different types: – for – while – etc.

For Loop for(initialValue; test-expression ; update- expression) { BODY }

Example #include int main() { int a; for (a = 0; a < 10; a++ ) { cout << a << endl; } return 0; }

While loop while(test-expression) { BODY }

Example #include int main() { int arSize = 20; char name[arSize]; cin >> name; int a = 0; while (name[a] != ‘\0’) { cout << name[a] << endl; a++; } return 0; }

Conditional Statements if statements Syntax: if (condition) { IF_BODY } else { ELSE_BODY }

Conditions: Relational Expressions: –Comparisons: e.g »== tests equality »> is greater?

Example int number; cin >> number; if ( number < 0) { cout << “Negative Value” << endl; } else { cout << “Non-negative Value” << endl; }

Comparing two strings #include int main() { int arSize = 20; char name1[arSize]; char name2[arSize]; cout<<“String1”<<endl; cin>>name1; cout<<“String2”<<endl; cin>>name2;

int len1,len2,a; len1=strlen(name1); len2=strlen(name2); bool flag=true; if (len1 != len2 ) cout<<“different”; else { for (a=0; name1[a]==name2;a++); if (a==len1) cout<<“The same”; else cout<<“different”; }

Sort an Array of Integer #include int main() { int arSize = 20; int ArrayNumber[arSize]; int indx; for(index=0; index <20; index++) { cout<<“enter an integer”<<endl; cin>>ArrayNumber[index]; }

int i,j; int temp; for( i=1; i< 20;i++) for (j=0; j< i;j++) if (ArrayNumber[ j] > ArrayNumber[ j+1]) { temp= ArrayNumber[ j ]; ArrayNumber[ j ]=ArrayNumber[ j+1]; ArrayNumber[ j+1]=temp; }