Arrays and String Chapter 7 Contents Array fundamentals



![Cont’d �Array can be initialized as int billy [5] = { 16, 2, 77, Cont’d �Array can be initialized as int billy [5] = { 16, 2, 77,](https://slidetodoc.com/presentation_image_h2/38c2c668174f07c6f840c45a24b3c510/image-4.jpg)
![Example int main() { int age[4]; for(int j=0; j<4; j++) { cout<<“Enter an age: Example int main() { int age[4]; for(int j=0; j<4; j++) { cout<<“Enter an age:](https://slidetodoc.com/presentation_image_h2/38c2c668174f07c6f840c45a24b3c510/image-5.jpg)
![Multidimensional Arrays �Multidimensional arrays can be described as "arrays of arrays“ int jimmy [3][5]; Multidimensional Arrays �Multidimensional arrays can be described as "arrays of arrays“ int jimmy [3][5];](https://slidetodoc.com/presentation_image_h2/38c2c668174f07c6f840c45a24b3c510/image-6.jpg)
![Example #define WIDTH 5 #define HEIGHT 3 int jimmy [HEIGHT][WIDTH]; int n, m; int Example #define WIDTH 5 #define HEIGHT 3 int jimmy [HEIGHT][WIDTH]; int n, m; int](https://slidetodoc.com/presentation_image_h2/38c2c668174f07c6f840c45a24b3c510/image-7.jpg)





![Example int main() { const int MAX = 80; Char str[MAX]; Cout<<“Enter a string”; Example int main() { const int MAX = 80; Char str[MAX]; Cout<<“Enter a string”;](https://slidetodoc.com/presentation_image_h2/38c2c668174f07c6f840c45a24b3c510/image-13.jpg)



- Slides: 16
Arrays and String Chapter 7
Contents �Array fundamentals �Arrays as class member data �Arrays of objects �C-strings �The standard C++ string class
Arrays Fundamental �An array is a series of elements of the same type placed in contiguous memory locations �It can be individually referenced by adding an index to a unique identifier �A typical declaration for an array in C++ is type name [elements]; e. g. int billy[5]; 0 1 2 3 4 billy int
Cont’d �Array can be initialized as int billy [5] = { 16, 2, 77, 40, 12071 }; billy 0 1 2 3 4 16 2 77 40 12071 int �we can access the value of any of its elements individually as if it was a normal variable name[index]; billy[2] = 75; a = billy[2];
Example int main() { int age[4]; for(int j=0; j<4; j++) { cout<<“Enter an age: ”; cin>>age[j]; } for(j=0; j<4; j++) { cout<<“You Entered: ”<<age[j]<<endl; } return 0; }
Multidimensional Arrays �Multidimensional arrays can be described as "arrays of arrays“ int jimmy [3][5]; 0 1 2 3 4 0 1 2 �Multidimensional arrays are not limited to two indices char century [100][365][24][60]
Example #define WIDTH 5 #define HEIGHT 3 int jimmy [HEIGHT][WIDTH]; int n, m; int main () 0 { for (n=0; n<HEIGHT; n++) 1 for (m=0; m<WIDTH; m++) 2 { jimmy[n][m]=(n+1)*(m+1); } return 0; } 0 1 2 3 4
Passing arrays to function � Arrays can be used as arguments to functions #include<iostream. h> void printarray (int arg[], int length) { for (int n=0; n<length; n++) cout << arg[n] << " "; cout << "n"; } int main () { int firstarray[] = {5, 10, 15}; int secondarray[] = {2, 4, 6, 8, 10}; printarray (firstarray, 3); printarray (secondarray, 5); return 0; }
Arrays of structure #include<iostream. h> const int SIZE = 4; Struct part{ int modelnumber; }; Int main() { int n; part apart[SIZE]; for(n=0; n<SIZE; n++) { cout<<“Enter Model Number: ” cin>>apart[n]. modelnumber; } }
Arrays as Class member data class Stack { private: const int MAX = 10; int st[MAX]; int top; public: Stack(){top = 0; } void push(int var) { st[++top] = var; } void pop() { return st[top--]; } } int main() { Stack s 1; s 1. push(11); s 1. push(22); cout<<“ 1: ”<<s 1. pop(); cout<<“ 2: ”<<s 1. pop(); }
Arrays of objects #include <iostream> class My. Class { int x; public: void set. X(int i) { x = i; } int get. X() { return x; } }; int main() { My. Class obs[4]; int i; for(i=0; i < 4; i++) obs[i]. set. X(i); for(i=0; i < 4; i++) cout << "obs[" << i << "]. get. X(): " << obs[i]. get. X() << "n"; } return 0;
C-Strings �String is sequence of characters �Strings can be represented as character of arrays e. g. char jenny[10]; Jenny �Strings are terminated by a null character represented internally as ‘ ’ �Strings can be initialized as constant value e. g. Char str[] = “Farewell! Thou art too dear for my possessing”; �Each character occupies 1 byte of memory
Example int main() { const int MAX = 80; Char str[MAX]; Cout<<“Enter a string”; Cin>>str; Cout<<“you entered: ”<<str<<endl; return 0; }
Cont’d �cin. get() function is use to read embedded blanks where get() is member function of stream class of which cin is an object cin. get(str, MAX, ’n’); �Array of strings can be declared as Char star[7][10]; First dimension specifies number of string while second dimension tells maximum length of string �This array can be accessed as star[j];
The standard C++ string Class #include<string. h> int main() { string s 1(“Man”); string s 2= “Beast”; string s 3; s 3 = s 1; cout<<s 3<<endl; s 3+=s 2; // concatenate cout<<s 3; s 1. swap(s 2); // swap s 1 and s 2 cout<<s 1<<s 2; return 0; }
Cont’d �<< and >> operator are overloaded to handle string object getline(cin, fullname); �String class includes many member functions to operate on strings �Find(“string”), find_first_of(“string”), erase() etc