C++ Chapter Four
C++ Chapter Four
1
Array
a group of similar types of elements
that have contiguous memory
location.
a container that encapsulates fixed size
arrays. array index starts from 0.
An array is a consecutive group of memory
locations
4
Cont’d …
Array is a collection of variables that can hold
value of same type and reference by
common name .
Its is a derived data Structure
Introduction Array indexing is always start
from zero
and highest address corresponds to last
element
5 CS_Dept
@ DDIT
One Dimensional
Array
a group of elements having the same datatype
and same name. Individual elements are referred
to using common name and unique index of the
elements.
The array itself is given name and its elements are
referred to by their subscripts. an array is denoted
as follows
Declaration :
• Data_type array_name [size of array ];
Eg:
• Int num[10];
8 CS_Dept @ DDIT
Array Visualization
Specifies an array of
variables of type int
The name of
the array
index values
primes[9]
primes[0] primes[1] primes[2] primes[3] primes[4]
Example:
Notice how the first element is the “Zeroth.”
“Z” is the first element, with a subscript of “0”
x[0]
To change an element, you x[1]
assign it this way. x[2]
X[3] = ‘Y’; x[3]
This changes the value of the x[4]
array location, not the x[5]
subscript value. x[6]
If your array is declared with a
x[7]
data type of char, then you x[8]
must use single quotes.
Double quotes are used for String.
1
3
Declaring an Array Variable
Array declarations use square brackets.
Example:
int c[4]= {1, 2, 3, 4};
or
int c[4];
c= {1, 2, 3, 4};
Syntax:datatype label[size];
Example:
Int prices [7];
String names[5];
Cont’d …
You can use an “Initializer List” enclosed in
braces to quickly insert values in your array:
int n[] = { 12, 44, 98, 1, 28 };
9
Accessing Array Elements
To access an item in an array, type the name
of the array followed by the item’s index in
square brackets.
Example:- the
expression:-
names[0];
will return the first
element in the names
array
Filling an Array:-
Assign values to
compartments:
Example:
Write a program that display elements of one
dimensional array.
Out Put
10
300
20
400
900
Exercise
What is the value of c after the
following code segment?
0 1
0 8 4
1 9 7
2 3 6
• value at row index 2, column index
0 is 3
Cont’d …
• A 2-d array is an array in which each element is itself
an array
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} };
1
5
Cont’d …
1
6
Part - Two
C++ String
3
CS_Dept @ DDUIOT
3
Strings
Strings are the combination of characters.
In other words, you can think strings as an
array of characters.
• Examples: “hello”, “high school”, “H2O”.
1
8
Declaration of strings
The following instructions are all equivalent.
They declare x assign the string “high school”
to it:
1 CS_Dept
9 @ DDIT
Operations on strings
Concatenation
Let x and y be two strings
To concatenate x and y, write:
x+y;
string x= “high”; O utp
string y=z;“school”;
string z=highschool
ut
z=x+y; z= highschool was
cout< < “z= “< <z<< e fun
ndl;
z =z+“ was fun”;
cout< < “z= “< <z<< e
2
ndl;
0
C++ String
Example
#include
<iostream>
using namespace
std;
int main( ) {
string s1 =
"H ello";
char ch[] = {
} 'C', '+',
'+'}; Output:
Hello
string s2 =
C++
string(ch);
C++ String Compare
Example
#include
< iostream> O utpu
#include t:
<cstring> What is my
using favorite fruit?
namespace std; apple
int main (){ What is my
char key[] = favorite
"mango"; char fruit? banana
buffer[50]; What is my
do { favorite fruit?
cout<<"What is my favourite mango
fruit? "; cin>>buffer; Answer is
}
correct!!
while (strcmp(key,buffer) != 0);
C++ String Concat Example
#include
<iostream> Outpu
#include t:
Enter the key
<cstring> using string:
namespace std; Welcome to
Enter the buffer
int main()
string: C++
{ Programming.
char key[25], Key = Welcome to
buffer[25]; C++
Programming.
cout < < "Enter Buffer = C+
the key string: +
"; Programmi
cin.getline(key, ng.
cout
25); < < "Buffer = " < <
buffer<<endl;
cout < < "Enter return 0;
the buffer
} string: "; cin.getline(buffer,
C++ String Copy Example
#include
Output
<iostream> :
#include Enter the key
string: C++
<cstring> using
Program
namespace std; Key = C++
int main(){ Program Buffer =
C++ Program
char key[25],
buffer[25];
cout
cout <
<< < "Enter
"Key = "<< key < <
the key string: ";
endl; cout < < "Buffer = "<<
cin.getline(key,
buffer<<endl; return 0;
25);
}
strcpy(buffer,
C++ String Length Example
#include
<iostream>
#include
<cstring> using
namespace std;
int main(){
char ary[] = "Welcome to C++
Programming"; cout < < "Length of
String = " < <
strlen(ary)<<end
l;
return 0;
}
Part Three
Pointers
1
C++ Pointers
🞂 A pointer is a variable, it is also known as
locator or indicator that points to an address
of a value.
🞂 A pointer is a variable whose value is the
address of another variable with the same
type.
🞂 Like any variable or constant, you must
declare a pointer before you can work
with it.
🞂 The general form of a pointer variable
declaration is type * var-name;
Cont’d …
// pointer to an integer
Example: int *ip;
// pointer to a double
double *dp;
//pointer to a float
float *fp;
char *ch; // pointer to character
∗
Declaring a pointer
The pointer in C++ language can be
declared using (asterisk symbol).
Exampl int ∗ a; //pointer to
e: char ∗
int
c; //pointer to char
Cont’d …
A pointer is a variable which holds the
memory address of another variable.
used for sorting the address of a memory
cell.
We can use the pointer to reference this
memory cell.
3
1
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<<e
ndl;
//
prints
a=100
and
3 &a=1
4
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
3
5
;
Cont’d …
🞂 Example:
3
6
Cont’d …
3
8