arash rafiey [email protected] ta : xu cheng [email protected] office hours m-w 10:30 – 11:30
Post on 21-Dec-2015
225 views
TRANSCRIPT
![Page 1: Arash Rafiey arafieyh@cs.sfu.ca TA : Xu Cheng xuc@cs.sfu.ca Office Hours M-W 10:30 – 11:30](https://reader036.vdocuments.net/reader036/viewer/2022062313/56649d6b5503460f94a4a3ef/html5/thumbnails/1.jpg)
Arash Rafiey
TA : Xu Cheng
Office Hours
M-W 10:30 – 11:30
![Page 2: Arash Rafiey arafieyh@cs.sfu.ca TA : Xu Cheng xuc@cs.sfu.ca Office Hours M-W 10:30 – 11:30](https://reader036.vdocuments.net/reader036/viewer/2022062313/56649d6b5503460f94a4a3ef/html5/thumbnails/2.jpg)
How Create a C++ Program
![Page 3: Arash Rafiey arafieyh@cs.sfu.ca TA : Xu Cheng xuc@cs.sfu.ca Office Hours M-W 10:30 – 11:30](https://reader036.vdocuments.net/reader036/viewer/2022062313/56649d6b5503460f94a4a3ef/html5/thumbnails/3.jpg)
#include<iostream>using namespace std; void main() { cout<<“Hello World”;}
If your compiler gives error then
![Page 4: Arash Rafiey arafieyh@cs.sfu.ca TA : Xu Cheng xuc@cs.sfu.ca Office Hours M-W 10:30 – 11:30](https://reader036.vdocuments.net/reader036/viewer/2022062313/56649d6b5503460f94a4a3ef/html5/thumbnails/4.jpg)
#include<iostream.h>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.
![Page 5: Arash Rafiey arafieyh@cs.sfu.ca TA : Xu Cheng xuc@cs.sfu.ca Office Hours M-W 10:30 – 11:30](https://reader036.vdocuments.net/reader036/viewer/2022062313/56649d6b5503460f94a4a3ef/html5/thumbnails/5.jpg)
#include<iostream.h>void main() { cout<<“Hello World \n”; cout<<“ good bye”;}
we can use \n instead of cout<<endl;
![Page 6: Arash Rafiey arafieyh@cs.sfu.ca TA : Xu Cheng xuc@cs.sfu.ca Office Hours M-W 10:30 – 11:30](https://reader036.vdocuments.net/reader036/viewer/2022062313/56649d6b5503460f94a4a3ef/html5/thumbnails/6.jpg)
How to run your program
• Using Linux or unix:g++ myFile.cpp
./a.out
g++ myFile.cpp myFile.out
./myFile.out
![Page 7: Arash Rafiey arafieyh@cs.sfu.ca TA : Xu Cheng xuc@cs.sfu.ca Office Hours M-W 10:30 – 11:30](https://reader036.vdocuments.net/reader036/viewer/2022062313/56649d6b5503460f94a4a3ef/html5/thumbnails/7.jpg)
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;
![Page 8: Arash Rafiey arafieyh@cs.sfu.ca TA : Xu Cheng xuc@cs.sfu.ca Office Hours M-W 10:30 – 11:30](https://reader036.vdocuments.net/reader036/viewer/2022062313/56649d6b5503460f94a4a3ef/html5/thumbnails/8.jpg)
Example
int LuckyNumber=17;
float RealNumber;
char a=‘A’;
![Page 9: Arash Rafiey arafieyh@cs.sfu.ca TA : Xu Cheng xuc@cs.sfu.ca Office Hours M-W 10:30 – 11:30](https://reader036.vdocuments.net/reader036/viewer/2022062313/56649d6b5503460f94a4a3ef/html5/thumbnails/9.jpg)
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
![Page 10: Arash Rafiey arafieyh@cs.sfu.ca TA : Xu Cheng xuc@cs.sfu.ca Office Hours M-W 10:30 – 11:30](https://reader036.vdocuments.net/reader036/viewer/2022062313/56649d6b5503460f94a4a3ef/html5/thumbnails/10.jpg)
Self Check
1. What is the type of the values 0 and ‘0’?
2. Which of the following are legal identifiers?
Greeting1gvoid101dalmatiansHello, World<greeting>
![Page 11: Arash Rafiey arafieyh@cs.sfu.ca TA : Xu Cheng xuc@cs.sfu.ca Office Hours M-W 10:30 – 11:30](https://reader036.vdocuments.net/reader036/viewer/2022062313/56649d6b5503460f94a4a3ef/html5/thumbnails/11.jpg)
Answer
• int and char
• Only the first two are legal identifiers
![Page 12: Arash Rafiey arafieyh@cs.sfu.ca TA : Xu Cheng xuc@cs.sfu.ca Office Hours M-W 10:30 – 11:30](https://reader036.vdocuments.net/reader036/viewer/2022062313/56649d6b5503460f94a4a3ef/html5/thumbnails/12.jpg)
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
![Page 13: Arash Rafiey arafieyh@cs.sfu.ca TA : Xu Cheng xuc@cs.sfu.ca Office Hours M-W 10:30 – 11:30](https://reader036.vdocuments.net/reader036/viewer/2022062313/56649d6b5503460f94a4a3ef/html5/thumbnails/13.jpg)
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;
![Page 14: Arash Rafiey arafieyh@cs.sfu.ca TA : Xu Cheng xuc@cs.sfu.ca Office Hours M-W 10:30 – 11:30](https://reader036.vdocuments.net/reader036/viewer/2022062313/56649d6b5503460f94a4a3ef/html5/thumbnails/14.jpg)
number2=number2-1;
number3=number2+1;
![Page 15: Arash Rafiey arafieyh@cs.sfu.ca TA : Xu Cheng xuc@cs.sfu.ca Office Hours M-W 10:30 – 11:30](https://reader036.vdocuments.net/reader036/viewer/2022062313/56649d6b5503460f94a4a3ef/html5/thumbnails/15.jpg)
How to read a variable
#include<iostream>using namespace std; void main() { int number; cout<<“please enter a number \n”; cin>>number; number=number+1; cout<<“the number is “<<number;}
![Page 16: Arash Rafiey arafieyh@cs.sfu.ca TA : Xu Cheng xuc@cs.sfu.ca Office Hours M-W 10:30 – 11:30](https://reader036.vdocuments.net/reader036/viewer/2022062313/56649d6b5503460f94a4a3ef/html5/thumbnails/16.jpg)
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 .
![Page 17: Arash Rafiey arafieyh@cs.sfu.ca TA : Xu Cheng xuc@cs.sfu.ca Office Hours M-W 10:30 – 11:30](https://reader036.vdocuments.net/reader036/viewer/2022062313/56649d6b5503460f94a4a3ef/html5/thumbnails/17.jpg)
• E.g. A 16-bit int might run from -32768 to 32767
• The sizeof operator returns the size (in bytes)
![Page 18: Arash Rafiey arafieyh@cs.sfu.ca TA : Xu Cheng xuc@cs.sfu.ca Office Hours M-W 10:30 – 11:30](https://reader036.vdocuments.net/reader036/viewer/2022062313/56649d6b5503460f94a4a3ef/html5/thumbnails/18.jpg)
#include<iostream.h>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;}
![Page 19: Arash Rafiey arafieyh@cs.sfu.ca TA : Xu Cheng xuc@cs.sfu.ca Office Hours M-W 10:30 – 11:30](https://reader036.vdocuments.net/reader036/viewer/2022062313/56649d6b5503460f94a4a3ef/html5/thumbnails/19.jpg)
• int is 4 bytes
• Maximum values:– Short: 32767
– Long: 2147483647
![Page 20: Arash Rafiey arafieyh@cs.sfu.ca TA : Xu Cheng xuc@cs.sfu.ca Office Hours M-W 10:30 – 11:30](https://reader036.vdocuments.net/reader036/viewer/2022062313/56649d6b5503460f94a4a3ef/html5/thumbnails/20.jpg)
Characters and small integers#include<iostream.h>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;}
![Page 21: Arash Rafiey arafieyh@cs.sfu.ca TA : Xu Cheng xuc@cs.sfu.ca Office Hours M-W 10:30 – 11:30](https://reader036.vdocuments.net/reader036/viewer/2022062313/56649d6b5503460f94a4a3ef/html5/thumbnails/21.jpg)
Output
• M is 77
• N is 78
![Page 22: Arash Rafiey arafieyh@cs.sfu.ca TA : Xu Cheng xuc@cs.sfu.ca Office Hours M-W 10:30 – 11:30](https://reader036.vdocuments.net/reader036/viewer/2022062313/56649d6b5503460f94a4a3ef/html5/thumbnails/22.jpg)
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
![Page 23: Arash Rafiey arafieyh@cs.sfu.ca TA : Xu Cheng xuc@cs.sfu.ca Office Hours M-W 10:30 – 11:30](https://reader036.vdocuments.net/reader036/viewer/2022062313/56649d6b5503460f94a4a3ef/html5/thumbnails/23.jpg)
Floating-point number
• E.g. • 12.34• 9300.3• 0.02• 8.0
• We have
float, double, long double
![Page 24: Arash Rafiey arafieyh@cs.sfu.ca TA : Xu Cheng xuc@cs.sfu.ca Office Hours M-W 10:30 – 11:30](https://reader036.vdocuments.net/reader036/viewer/2022062313/56649d6b5503460f94a4a3ef/html5/thumbnails/24.jpg)
Arithmetic operators
• Summation: +
• Multiplication: *
• Division: /
• Subtraction: -
![Page 25: Arash Rafiey arafieyh@cs.sfu.ca TA : Xu Cheng xuc@cs.sfu.ca Office Hours M-W 10:30 – 11:30](https://reader036.vdocuments.net/reader036/viewer/2022062313/56649d6b5503460f94a4a3ef/html5/thumbnails/25.jpg)
Operator Precedence
• int number = 3 + 4 * 5 // 35 or 23?
• float logs = 120 / 4 * 5 // 150 or 6??
![Page 26: Arash Rafiey arafieyh@cs.sfu.ca TA : Xu Cheng xuc@cs.sfu.ca Office Hours M-W 10:30 – 11:30](https://reader036.vdocuments.net/reader036/viewer/2022062313/56649d6b5503460f94a4a3ef/html5/thumbnails/26.jpg)
Type Casting
• Conversion between types:• (typeName) value // c• typeName (value) // c++• e.g.
– cout << int(‘A’) << endl; // 65– float one, – int two;
• one = 1.9 + 2.1;• two = (int) 1.9 + (int) 2.1;
![Page 27: Arash Rafiey arafieyh@cs.sfu.ca TA : Xu Cheng xuc@cs.sfu.ca Office Hours M-W 10:30 – 11:30](https://reader036.vdocuments.net/reader036/viewer/2022062313/56649d6b5503460f94a4a3ef/html5/thumbnails/27.jpg)
Functions
• Building blocks of programs
• A function
– Has an input and an output– Contains a set of instructions
• x = sqrt(16); // returns 4
![Page 28: Arash Rafiey arafieyh@cs.sfu.ca TA : Xu Cheng xuc@cs.sfu.ca Office Hours M-W 10:30 – 11:30](https://reader036.vdocuments.net/reader036/viewer/2022062313/56649d6b5503460f94a4a3ef/html5/thumbnails/28.jpg)
Syntax
• typeName functionName (typeName varName_1, …)
{
BODY
.
.
.
return value;
}
![Page 29: Arash Rafiey arafieyh@cs.sfu.ca TA : Xu Cheng xuc@cs.sfu.ca Office Hours M-W 10:30 – 11:30](https://reader036.vdocuments.net/reader036/viewer/2022062313/56649d6b5503460f94a4a3ef/html5/thumbnails/29.jpg)
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;}
![Page 30: Arash Rafiey arafieyh@cs.sfu.ca TA : Xu Cheng xuc@cs.sfu.ca Office Hours M-W 10:30 – 11:30](https://reader036.vdocuments.net/reader036/viewer/2022062313/56649d6b5503460f94a4a3ef/html5/thumbnails/30.jpg)
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;
![Page 31: Arash Rafiey arafieyh@cs.sfu.ca TA : Xu Cheng xuc@cs.sfu.ca Office Hours M-W 10:30 – 11:30](https://reader036.vdocuments.net/reader036/viewer/2022062313/56649d6b5503460f94a4a3ef/html5/thumbnails/31.jpg)
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};
![Page 32: Arash Rafiey arafieyh@cs.sfu.ca TA : Xu Cheng xuc@cs.sfu.ca Office Hours M-W 10:30 – 11:30](https://reader036.vdocuments.net/reader036/viewer/2022062313/56649d6b5503460f94a4a3ef/html5/thumbnails/32.jpg)
• short array[] = {1,2,3,4};
![Page 33: Arash Rafiey arafieyh@cs.sfu.ca TA : Xu Cheng xuc@cs.sfu.ca Office Hours M-W 10:30 – 11:30](https://reader036.vdocuments.net/reader036/viewer/2022062313/56649d6b5503460f94a4a3ef/html5/thumbnails/33.jpg)
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!!
![Page 34: Arash Rafiey arafieyh@cs.sfu.ca TA : Xu Cheng xuc@cs.sfu.ca Office Hours M-W 10:30 – 11:30](https://reader036.vdocuments.net/reader036/viewer/2022062313/56649d6b5503460f94a4a3ef/html5/thumbnails/34.jpg)
More examples
• char dog[5] = “beaux”; //a better way
null character is implicitly included.
• char name[]=“c++”; // compiler counts
• char boss[8]=“Arvind”;
![Page 35: Arash Rafiey arafieyh@cs.sfu.ca TA : Xu Cheng xuc@cs.sfu.ca Office Hours M-W 10:30 – 11:30](https://reader036.vdocuments.net/reader036/viewer/2022062313/56649d6b5503460f94a4a3ef/html5/thumbnails/35.jpg)
#include<iostream.h>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;}
![Page 36: Arash Rafiey arafieyh@cs.sfu.ca TA : Xu Cheng xuc@cs.sfu.ca Office Hours M-W 10:30 – 11:30](https://reader036.vdocuments.net/reader036/viewer/2022062313/56649d6b5503460f94a4a3ef/html5/thumbnails/36.jpg)
Another way to read a string#include<iostream.h>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;}
![Page 37: Arash Rafiey arafieyh@cs.sfu.ca TA : Xu Cheng xuc@cs.sfu.ca Office Hours M-W 10:30 – 11:30](https://reader036.vdocuments.net/reader036/viewer/2022062313/56649d6b5503460f94a4a3ef/html5/thumbnails/37.jpg)
The String class
• To define strings more easily:– Include the string class:
• #include<string>
• string str_1 = “jaguar”;
![Page 38: Arash Rafiey arafieyh@cs.sfu.ca TA : Xu Cheng xuc@cs.sfu.ca Office Hours M-W 10:30 – 11:30](https://reader036.vdocuments.net/reader036/viewer/2022062313/56649d6b5503460f94a4a3ef/html5/thumbnails/38.jpg)
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!!
![Page 39: Arash Rafiey arafieyh@cs.sfu.ca TA : Xu Cheng xuc@cs.sfu.ca Office Hours M-W 10:30 – 11:30](https://reader036.vdocuments.net/reader036/viewer/2022062313/56649d6b5503460f94a4a3ef/html5/thumbnails/39.jpg)
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
![Page 40: Arash Rafiey arafieyh@cs.sfu.ca TA : Xu Cheng xuc@cs.sfu.ca Office Hours M-W 10:30 – 11:30](https://reader036.vdocuments.net/reader036/viewer/2022062313/56649d6b5503460f94a4a3ef/html5/thumbnails/40.jpg)
More string operations:
• Copying: #include <cstring>
– 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();
![Page 41: Arash Rafiey arafieyh@cs.sfu.ca TA : Xu Cheng xuc@cs.sfu.ca Office Hours M-W 10:30 – 11:30](https://reader036.vdocuments.net/reader036/viewer/2022062313/56649d6b5503460f94a4a3ef/html5/thumbnails/41.jpg)
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;
![Page 42: Arash Rafiey arafieyh@cs.sfu.ca TA : Xu Cheng xuc@cs.sfu.ca Office Hours M-W 10:30 – 11:30](https://reader036.vdocuments.net/reader036/viewer/2022062313/56649d6b5503460f94a4a3ef/html5/thumbnails/42.jpg)
a: 20 b:20
a++: 20 ++b:21
a:21 b:21
int x = 5;
int y = ++x;
![Page 43: Arash Rafiey arafieyh@cs.sfu.ca TA : Xu Cheng xuc@cs.sfu.ca Office Hours M-W 10:30 – 11:30](https://reader036.vdocuments.net/reader036/viewer/2022062313/56649d6b5503460f94a4a3ef/html5/thumbnails/43.jpg)
Decrement: --
• Same rules as increment ( i.e. ++)
![Page 44: Arash Rafiey arafieyh@cs.sfu.ca TA : Xu Cheng xuc@cs.sfu.ca Office Hours M-W 10:30 – 11:30](https://reader036.vdocuments.net/reader036/viewer/2022062313/56649d6b5503460f94a4a3ef/html5/thumbnails/44.jpg)
Loops
• different types:
– for– while– etc.
![Page 45: Arash Rafiey arafieyh@cs.sfu.ca TA : Xu Cheng xuc@cs.sfu.ca Office Hours M-W 10:30 – 11:30](https://reader036.vdocuments.net/reader036/viewer/2022062313/56649d6b5503460f94a4a3ef/html5/thumbnails/45.jpg)
For Loop
• for(initialValue; test-expression ; update-expression)
{
BODY
}
![Page 46: Arash Rafiey arafieyh@cs.sfu.ca TA : Xu Cheng xuc@cs.sfu.ca Office Hours M-W 10:30 – 11:30](https://reader036.vdocuments.net/reader036/viewer/2022062313/56649d6b5503460f94a4a3ef/html5/thumbnails/46.jpg)
Example
#include<iostream.h>int main() { int a; for (a = 0; a < 10; a++ ) { cout << a << endl; }return 0;}
![Page 47: Arash Rafiey arafieyh@cs.sfu.ca TA : Xu Cheng xuc@cs.sfu.ca Office Hours M-W 10:30 – 11:30](https://reader036.vdocuments.net/reader036/viewer/2022062313/56649d6b5503460f94a4a3ef/html5/thumbnails/47.jpg)
While loop
• while(test-expression)
{
BODY
}
![Page 48: Arash Rafiey arafieyh@cs.sfu.ca TA : Xu Cheng xuc@cs.sfu.ca Office Hours M-W 10:30 – 11:30](https://reader036.vdocuments.net/reader036/viewer/2022062313/56649d6b5503460f94a4a3ef/html5/thumbnails/48.jpg)
Example#include<iostream.h>int main() { int arSize = 20; char name[arSize]; cin >> name; int a = 0; while (name[a] != ‘\0’) { cout << name[a] << endl; a++; }return 0;}
![Page 49: Arash Rafiey arafieyh@cs.sfu.ca TA : Xu Cheng xuc@cs.sfu.ca Office Hours M-W 10:30 – 11:30](https://reader036.vdocuments.net/reader036/viewer/2022062313/56649d6b5503460f94a4a3ef/html5/thumbnails/49.jpg)
Conditional Statements
• if statements• Syntax: if (condition) { IF_BODY } else { ELSE_BODY }
![Page 50: Arash Rafiey arafieyh@cs.sfu.ca TA : Xu Cheng xuc@cs.sfu.ca Office Hours M-W 10:30 – 11:30](https://reader036.vdocuments.net/reader036/viewer/2022062313/56649d6b5503460f94a4a3ef/html5/thumbnails/50.jpg)
Conditions:
• Relational Expressions:– Comparisons:
• e.g» == tests equality» > is greater?
![Page 51: Arash Rafiey arafieyh@cs.sfu.ca TA : Xu Cheng xuc@cs.sfu.ca Office Hours M-W 10:30 – 11:30](https://reader036.vdocuments.net/reader036/viewer/2022062313/56649d6b5503460f94a4a3ef/html5/thumbnails/51.jpg)
Example
int number; cin >> number;if ( number < 0) { cout << “Negative Value” << endl; } else { cout << “Non-negative Value” << endl; }
![Page 52: Arash Rafiey arafieyh@cs.sfu.ca TA : Xu Cheng xuc@cs.sfu.ca Office Hours M-W 10:30 – 11:30](https://reader036.vdocuments.net/reader036/viewer/2022062313/56649d6b5503460f94a4a3ef/html5/thumbnails/52.jpg)
Comparing two strings
#include<iostream.h>int main() { int arSize = 20; char name1[arSize]; char name2[arSize]; cout<<“String1”<<endl; cin>>name1; cout<<“String2”<<endl; cin>>name2;
![Page 53: Arash Rafiey arafieyh@cs.sfu.ca TA : Xu Cheng xuc@cs.sfu.ca Office Hours M-W 10:30 – 11:30](https://reader036.vdocuments.net/reader036/viewer/2022062313/56649d6b5503460f94a4a3ef/html5/thumbnails/53.jpg)
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”;}}
![Page 54: Arash Rafiey arafieyh@cs.sfu.ca TA : Xu Cheng xuc@cs.sfu.ca Office Hours M-W 10:30 – 11:30](https://reader036.vdocuments.net/reader036/viewer/2022062313/56649d6b5503460f94a4a3ef/html5/thumbnails/54.jpg)
Sort an Array of Integer
#include<iostream.h>int main() { int arSize = 20; int ArrayNumber[arSize]; int indx; for(index=0; index <20; index++) { cout<<“enter an integer”<<endl; cin>>ArrayNumber[index]; }
![Page 55: Arash Rafiey arafieyh@cs.sfu.ca TA : Xu Cheng xuc@cs.sfu.ca Office Hours M-W 10:30 – 11:30](https://reader036.vdocuments.net/reader036/viewer/2022062313/56649d6b5503460f94a4a3ef/html5/thumbnails/55.jpg)
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; }}