0% found this document useful (0 votes)
3 views

Chapter 3

Chapter four covers arrays and string manipulation in C++. It explains the definition, properties, declaration, initialization, and common errors associated with arrays, as well as the differences between one-dimensional and multidimensional arrays. Additionally, it discusses strings, their declaration, and initialization methods.

Uploaded by

shabir191945
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Chapter 3

Chapter four covers arrays and string manipulation in C++. It explains the definition, properties, declaration, initialization, and common errors associated with arrays, as well as the differences between one-dimensional and multidimensional arrays. Additionally, it discusses strings, their declaration, and initialization methods.

Uploaded by

shabir191945
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 27

Chapter four

Arrays and String Manipulation


What is An Array?
 A data structure containing a number of data values.
 Data structure is a format for organizing and storing data.
 A collection of identical data objects which
Are stored in consecutive memory locations
Are accessed by a single name but with different index.
 An array is a group or a table of values having the same data type.

 An array is a data structure which stores fixed size collection of


elements.
 The individual values in an array is called elements.

Computer programming 1
Cont..
 In C++, square brackets bound all the index right after the name of the
array.
2
1 Array Elements

A B C D E

0 1 2 3 4
Index Array
length = 5

First
index = 0 3
Last
index = 4
Computer programming 2
Properties of arrays

 Arrays in C++ are zero-bounded means that


 The index of the first element in the array is 0 and
 Index of the last element is N-1, if N is the size of the array.
 It is illegal to refer elements outside the array bounds and
 Your program gives unexpected results, depending on compiler.
 Array can only hold values of one data type.
 Array name represents the address of the starting element.
 Array size should be mentioned in the declaration.

Computer programming 3
Array declaration

 In the array declaration, we must define:


 The data type of the array ( integer, float, char, etc.)
 Name of the array
 Number of subscripts in the array
 The general syntax of array declaration looks like:

DataType Arrayname [array_size];

Example: int Age[5];

Computer programming 4
Initializing Arrays

 A simple variable can be initialized when it is declared.


 When array is initialized
The values for indexed variables are enclosed in braces
One value should be separated from others with commas.
 Arrays may be initialized by different rules.
Garbage allocation, Specify value, Specify value and size,
Specify size and null ….. etc
Example: int Age[3]={12,6,8};

Computer programming 5
Initializing Arrays
 Garbage allocation: defines data type, name & size of array.
Example: int grade[6];
 Specify value: specify the data type, name & value of array.
Example: int grade[ ]= {98, 69,78, 86, 53}; //the best style
 Specify value and size: defines all parameters of the array.
Example: int Age[3]={12,6,8};
 Specify size and null : this rule miss the value of array.

Example: int grade[8]= { }; //not appropriate

Computer programming 6
Cont..
 The number of constants in the initialize list must be
 Less than or equal to size of the array.
 The other way for initialization is passing a value for each indexed
array.
Example: int Age[3];
Age[0]=12; This style consumes memory and
time
Age[1]=6;
Age[2]=8;

Computer programming 7
Cont..
 An array can be initialized to be all zeros by using an empty
initialization list.
Example:
 float salary [ ] = {0, 0, 0, 0, 0, 0};
 float salary [6] = {0, 0, 0};
 float salary [6] = {0, 0, 0, 0, 0, 0};

If an array is not initialized, it will contain garbage values.

Computer programming 8
Designated Initialization
 When the value of an array is not assigned sequentially, we have
used designated initialization.
Example: int array [10 ] = {1, 0, 0, 0, 0, 2, 3, 0, 0, 0};

 Here, we want
 1 in position 0;
 2 in position 5 and
 3 in position 6

int array[10] = {[0] = 1, [5] = 2, [6] = 3};


 This way of initialization is called designated initialization.

Computer programming 9
Cont..
 When the initialization contains different styles, the designated
initialization wins other techniques.
Example: int a [ ] = {1, 2, 3, [2] = 4, [6] = 45};
 What will be the value of the array a(2)?
 The above array is equivalent to
 int a [] = {1, 2, 4, 0, 0, 0, 45};
 The index of the array never be negative value.

Computer programming 10
Example
#include<iostream>
using namespace std;
int main()
{
int array[5]={12,23,34,45,56};
cout<<"output of the above declaration is\n";
cout<<array[0]<<endl;
cout<<array[1]<<endl;
cout<<array[2]<<endl;
cout<<array[3]<<endl;
cout<<array[4]<<endl;
return 0;
}

Computer programming 11
Cont..
The above code can be easily written as follows:-
#include<iostream>
using namespace std;
int main()
{
int array[5]={12,23,34,45,56};
cout<<"output of the above declaration is\n";
for(int i=0;i<=4;i++)
cout<<array[i]<<endl;
return 0;
}

Computer programming 12
Cont..
Write c++ code that accept numbers from user input and display it.
#include<iostream>
using namespace std;
int main()
{
int n, num [100];
cout<<“Enter the number of array you want\n";
cin>>n;
for (int i=0; i<=n-1; i++)
{
cout<<“Enter Your "<< i <<" array ";
cin>>num[i];
}
cout<<"the contents of the array are\n";
for (int j=0;j<=n-1;j++)
cout<<"the "<<j <<"array is " <<num[j]<<endl;
return 0;
}
Computer programming 13
Common Errors on Array

 The most common programming errors occurred in array


 Attempting to refer a non-exist array index.
 Array indexes get out of rang most commonly at the first or last
iteration of a loop that process the array.
 Remember that the array always starts on its first iteration that is
0.

Computer programming 14
Multidimensional Array

 A 1D array is a simple data structure that stores a collection of


similar type data in a contiguous block of memory.
 While the 2D array stores multiple data elements of the same type
in matrix or table like format with a number of rows and columns.
 The main difference between 1D and 2D arrays are:
 A two dimensional array will require two pair of brackets (two
independent subscripts)
 A three dimensional array will require three pairs of square
brackets and so on

Computer programming 15
Cont..

 The general format of multidimensional array with n dimension is:


Data_type array_name [size_1] [size_2] ---[size_n];
Where
 Data_type: type of data to be stored in the array
 Array_name: name of the array
 Size1, Size2, ….. Size n: size of the dimension.
 Two dimensional array is the simplest form of a multidimensional
array.

Computer programming 16
Cont..

 2D array or multi-dimensional array stores data in a format


consisting of rows and columns.

Computer programming 17
Size of Multidimensional Array
 The length of an array can be specified by any positive integer
constant expression (int arr[5+5] ;).
 The total number of elements stored in multidimensional array can
be calculated by multiplying size of all dimensions.
Example:
Float x[10][10]; // total of 100 elements with two
dimensions
int age[3[[6]; // age has 18 elements
int x [2][2] ={1,2,3,4}; // two column and two rows
int x [2][3]={{1,2,3},{4,5,6}}; //best one
Computer programming 18
Cont..

Computer programming 19
Cont..
// a program to accept input and cout<<"enter the "<<i<<" row and enter
display its content "<<j<<" column ";
cin>>nums[i][j]; }}
#include<iostream.h>
cout<<"the output is \n";
int main()
for (i=0; i<2; i++)
{ {
int i, j; cout<<" row "<<i<<"\t";
int nums[2][2]; for (j=0; j<2; j++)
for (i=0; i<2; i++) {
{ cout<<nums[i][j]<<"\t";
}
cout<<"the "<<i<<" row \n";
cout<<endl;
for (j=0; j<2; j++)
}
{ return 0;
}

Computer programming 20
Constant Array
 Either one dimensional or two dimensional array can be made
constant by starting declaration with the keyword const.
 One the is given a value, we can not modify that value.
 Example const int array[5]={12,23,34,45,56};
 Let as try to change one value of the array.
Array[2] = 45; //gives a response of Error
 If we have very large elements in an array, how we determine the
size of array?
Use ‘sizeof’ operator (sizeof(array)).

Computer programming 21
Strings of Characters
What are Strings?
 Strings of characters that allow us to represent successive
characters
 Like words, sentences, names, texts, etc
 Strings are used for storing texts and it should be surrounded by
either single or double quotes.

Computer programming 22
Declaring and Initialization of Strings

Example:
 char mystring [] = { 'H', 'e', 'l', 'l', 'o', '\0' };
 char mystring [] = "Hello";

 In both cases:
 The Array or string of characters mystring is declared with a
size of 6.
 The 5 characters that compose Hello & character ( '\0' )
 The final null specifies the end of the string
 In the second case, when using double quotes ( " )
It is automatically appended. 23
Computer programming
Cont..
 The following expressions within a code are not valid for arrays.
mystring="Hello";
mystring[] = "Hello";
mystring = { 'H', 'e', 'l', 'l', 'o', '\0' };
Example:
#include <iostream>
#include <string>
using namespace std;
int main ()
{
char name [20]=“Melkamu";
cout << name[4];
return 0;
}
Computer programming 24
Cont..
 The following expressions within a code are not valid for arrays.
mystring="Hello";
mystring[] = "Hello";
mystring = { 'H', 'e', 'l', 'l', 'o', '\0' };
Example:
#include <iostream>
#include <string>
using namespace std;
int main ()
{
char name [20]=“Melkamu";
cout << name[4];
return 0;
}
Computer programming 24
Thank You for Your Attention!!!

Computer Programming 25

You might also like