Chapter 4
Chapter 4
0 1
0 8 4
1 9 7
2 3 6
27
Initialize Two Dimensional Array in C++
· The general form to initialize values to two dimensional array in C++
· Sintax:
data_type array_name[row_size][column_size]
= { {comma_separated_value_list} };
int arr[5][2] = { {1, 2}, {3, 4}, {5, 6}, {7, 8}, {9, 10} };
21
Cont’d …
Out put
22
C++ Multidimensional Arrays
The multidimensional array is also
known as rectangular arrays in C++.
• Comparison:“high”<“school”// alphabetical
25
Declaration of strings
· The following instructions are all equivalent.
· They declare x assign the string “high school” to it:
string x= “high school”;
string x;
x=“high school”;
26 CS_Dept @ DDIT
Operations on strings
Concatenation
· Let x and y be two strings
· To concatenate x and y, write: x+y;
· string x= “high”;
· string y= “school”; O utput
string z; z=highschool
· z=x+y; z= highschool was fun
· cout< < “z= “< <z<< endl;
· z =z+“ was fun”;
· cout< < “z= “< <z<< endl;
27
C++ String Example
#include <iostream>
#include<string>
using namespace std;
int main( ) {
string s1 = "H ello";
char ch[] = { 'C', '+', '+'};
string s2 = string(ch); Output:
Hello
cout<<s1<<endl;
} cout<<s2<<endl;
C++
C++ String Concatenation Example
#include <iostream>
#include <cstring> Output:
using namespace std;
Enter the key string:
int main()
Welcome to
{
Enter the buffer string:
char key[25], buffer[25];
C++ Programming.
cout < < "Enter the key string: ";
cin.getline(key, 25); Key = Welcome to C++
Programming.
cout < < "Enter the buffer string: ";
cin.getline(buffer, 25); Buffer = C++
Programming.
strcat(key, buffer);
cout < < "Key = " < < key < < endl;
cout << "Buffer = " << buffer<<endl;
return 0;
}
String comparison
• comparing strings is very simple using the built-in comparison
operators like ==, !=, <, >, <=, >=, and the compare() method.
• Example
Out put
str1 is lexicographically less than str2
C++ String Compare Example
O utput:
What is my favorite fruit?
Apple
What is my favorite fruit?
banana
What is my favorite fruit?
mango
Answer is correct!!
C++ String Copy Example
#include <iostream>
#include <cstring> Output:
using namespace std;
int main(){ Enter the key string:
C++ Program
char key[25], buffer[25]; Key = C++ Program
cout < < "Enter the key string: "; Buffer = C++ Program
3
Cont’d …
Advantage of pointer
· Reduces the code and improves the performance.
It is used to retrieving strings, trees etc. and used with
arrays, structures and functions.
37
Cont’d …
Address of operator(&)
· It is a unary operator that returns the memory
address of its operand.
· Here the operand is a normal variable.
Eg. int x = 10;
int *ptr = &x;
Example:
int a=100;
// to gets the value, use the variable name
Example:
int a=100;
int
*p=&a;
cout<<a<<” ”<<&a<<endl;
//prints a=100 and &a=1024
cout<<p<<” ”<<&p<<endl;
//prints P=1024 and &P=1032
• The variable of pointer p is the address of variable a
• A pointer is also a variable, so it has its own memory address
Cont’d …
Dereference operator (*)
• It is a unary operator that returns the value stored at the
address pointed to by the pointer.
• Here the operand is a pointer variable.
· Example:
int x = 10;
int *ptr = &x;
cout<< ptr;
// address stored at ptr will be displayed
cout<<*ptr;
// value pointed to by ptr will be displayed
· Now ptr can also be used to change/display the value of x.
41
Cont’d …
• cout<<a<<endl; cout<<&a<<endl;
// prints 1024
cout<<p<<” “<<*p<<endl;
// prints 1024 and 100 respectively
cout<<&p<<endl;
// prints 1032
42
Cont’d
· Example:
…
43
Cont’d …
• Before p1 = p2:
• p1 points to the address of value1 (p1 = &value1).
• p2 points to the address of value2 (p2 = &value2).
• After p1 = p2:
• p1 no longer points to value1. Instead, it now points to the
same address as p2 (the address of value2).
• From this point on, any operation on *p1 (the value pointed
to by p1) will affect value2, because both p1 and p2 refer to
value2.
NULL Pointer
· Example:
int* ptr = nullptr;
if (ptr == nullptr) {
cout << "Pointer is null." << endl;
}