Changing the Length of 1 D Array What

![In C++ int *a; int n=10; while (n>0) { a=new int[n]; for (int i=0; In C++ int *a; int n=10; while (n>0) { a=new int[n]; for (int i=0;](https://slidetodoc.com/presentation_image_h2/5ab5bb24f9b479a3e674fd265ddea815/image-2.jpg)





![void main() { int a[5]={20, 30, 40, 50, 60}; for (int i=0; i<4; i++) void main() { int a[5]={20, 30, 40, 50, 60}; for (int i=0; i<4; i++)](https://slidetodoc.com/presentation_image_h2/5ab5bb24f9b479a3e674fd265ddea815/image-8.jpg)
![Two Dimensional Arrays In C++ Static Array: int arr_2 d[10][12]; Dynamic Array: int **A; Two Dimensional Arrays In C++ Static Array: int arr_2 d[10][12]; Dynamic Array: int **A;](https://slidetodoc.com/presentation_image_h2/5ab5bb24f9b479a3e674fd265ddea815/image-9.jpg)

![Two Dimensional Arrays In Java int [ ] A; A= new int[rows][ ]; for Two Dimensional Arrays In Java int [ ] A; A= new int[rows][ ]; for](https://slidetodoc.com/presentation_image_h2/5ab5bb24f9b479a3e674fd265ddea815/image-11.jpg)
![In C++ int a[3][3]={1, 2, 3, 4, 5, 6}; for (int i=0; i<3; i++) In C++ int a[3][3]={1, 2, 3, 4, 5, 6}; for (int i=0; i<3; i++)](https://slidetodoc.com/presentation_image_h2/5ab5bb24f9b479a3e674fd265ddea815/image-12.jpg)
![In C++, Example 2 int a[3][3]={1, 2, 3, 4, 5, 6}; for (int i=0; In C++, Example 2 int a[3][3]={1, 2, 3, 4, 5, 6}; for (int i=0;](https://slidetodoc.com/presentation_image_h2/5ab5bb24f9b479a3e674fd265ddea815/image-13.jpg)
![In C++, Example 3 int a[ ][3]={1, 2, 3, 4, 5, 6}; for (int In C++, Example 3 int a[ ][3]={1, 2, 3, 4, 5, 6}; for (int](https://slidetodoc.com/presentation_image_h2/5ab5bb24f9b479a3e674fd265ddea815/image-14.jpg)
![In C++, Example 4 int a[3][ ]={1, 2, 3, 4, 5, 6}; for (int In C++, Example 4 int a[3][ ]={1, 2, 3, 4, 5, 6}; for (int](https://slidetodoc.com/presentation_image_h2/5ab5bb24f9b479a3e674fd265ddea815/image-15.jpg)
![In C++, Example 5 int a[ ][3]={1, 2, 3, 4, 5, 6}; for (int In C++, Example 5 int a[ ][3]={1, 2, 3, 4, 5, 6}; for (int](https://slidetodoc.com/presentation_image_h2/5ab5bb24f9b479a3e674fd265ddea815/image-16.jpg)
![In C++, Example 6 int a[ ][3]={1, 2, 3, 4, 5, 6, 7}; for In C++, Example 6 int a[ ][3]={1, 2, 3, 4, 5, 6, 7}; for](https://slidetodoc.com/presentation_image_h2/5ab5bb24f9b479a3e674fd265ddea815/image-17.jpg)
![Address of [i 1][i 2]th location element in a 2 D-Array Given a[r 1][r Address of [i 1][i 2]th location element in a 2 D-Array Given a[r 1][r](https://slidetodoc.com/presentation_image_h2/5ab5bb24f9b479a3e674fd265ddea815/image-18.jpg)


![Strings in Java Char char. Array[ ] = new char[4]; char. Array[0]=‘J’; char. Array[1]=‘A’; Strings in Java Char char. Array[ ] = new char[4]; char. Array[0]=‘J’; char. Array[1]=‘A’;](https://slidetodoc.com/presentation_image_h2/5ab5bb24f9b479a3e674fd265ddea815/image-21.jpg)


![What is the output of following strange code? Why? int a[10]; cout<<a<<endl ; cout<<&a; What is the output of following strange code? Why? int a[10]; cout<<a<<endl ; cout<<&a;](https://slidetodoc.com/presentation_image_h2/5ab5bb24f9b479a3e674fd265ddea815/image-24.jpg)
- Slides: 24
Changing the Length of 1 D Array What does it mean to change the length of an array? In what situations do you need to do this? How can you change the length of one dimensional array? 1
In C++ int *a; int n=10; while (n>0) { a=new int[n]; for (int i=0; i<n; i++) cin>>a[i]; sort(a, n); //some function n--; delelte [ ] a; } In Java int [ ] a; int n=10; while (n>0) { a=new int[n]; for (int i=0; i<n; i++) a[i] = input. next. Int(); sort(a, n); //some function n--; } 2
Binary Search 3
4
5
6
Find the output of the following C++ Program void change(int* b); void main() { int a[5]={20, 30, 40, 50, 60}; change(a); for (int i=4; i>=0; i--) cout<<a; } void change(int* b) { for (int i=0; i<4; i++) { *b=*b+1; b++; } } 7
void main() { int a[5]={20, 30, 40, 50, 60}; for (int i=0; i<4; i++) { *a=*a+1; a++; } for (int i=4; i>=0; i--) cout<<a[i]; } 8
Two Dimensional Arrays In C++ Static Array: int arr_2 d[10][12]; Dynamic Array: int **A; A= new int*[rows]; for (int i=0; i<rows; i++) A[i]=new int [cols]; 9
Java arrays can be multidimensional. For example, a 2 dimensional array is an array of arrays. Twodimensional arrays need not be rectangular. Each row can be a different length. Here's an example: int [ ][ ] A; // A is a two-dimensional array A = new int[5][ ]; // A now has 5 rows, but no columns yet A[0] = new int [1]; // A's first row has 1 column A[1] = new int [2]; // A's second row has 2 columns A[2] = new int [3]; // A's third row has 3 columns A[3] = new int [5]; // A's fourth row has 5 10
Two Dimensional Arrays In Java int [ ] A; A= new int[rows][ ]; for (int i=0; i<rows; i++) A[i]=new int [cols]; 11
In C++ int a[3][3]={1, 2, 3, 4, 5, 6}; for (int i=0; i<3; i++) { for (int j=0; j<3; j++) cout<<a[ i ][ j ]<<“ “; cout<<endl; } Example 1 Output: 1 2 4 5 0 0 3 6 0 12
In C++, Example 2 int a[3][3]={1, 2, 3, 4, 5, 6}; for (int i=0; i<3; i++) { for (int j=0; j<7; j++) cout<<a[ i ][ j ]<<“ “; cout<<endl; } Output: 1 2 3 G 1 G 2 G 3 G 4 4 5 6 G 5 G 6 G 7 G 8 0 0 0 G 9 G 10 G 11 G 12 13
In C++, Example 3 int a[ ][3]={1, 2, 3, 4, 5, 6}; for (int i=0; i<3; i++) { for (int j=0; j<3; j++) cout<<a[ i ][ j ]<<“ “; cout<<endl; } Output: 1 2 3 4 5 6 G 1 G 2 G 3 14
In C++, Example 4 int a[3][ ]={1, 2, 3, 4, 5, 6}; for (int i=0; i<3; i++) { for (int j=0; j<3; j++) cout<<a[ i ][ j ]<<“ “; cout<<endl; } Output: Error 15
In C++, Example 5 int a[ ][3]={1, 2, 3, 4, 5, 6}; for (int i=0; i<3; i++) { for (int j=0; j<7; j++) cout<<a[ i ][ j ]<<“ “; cout<<endl; } Output: 1 2 3 G 1 G 2 G 3 G 4 4 5 6 G 5 G 6 G 7 G 8 G 9 G 10 G 11 G 12 G 13 G 14 G 15 16
In C++, Example 6 int a[ ][3]={1, 2, 3, 4, 5, 6, 7}; for (int i=0; i<3; i++) { for (int j=0; j<3; j++) cout<<a[ i ][ j ]<<“ “; cout<<endl; } Output: 1 2 4 5 7 0 3 6 0 17
Address of [i 1][i 2]th location element in a 2 D-Array Given a[r 1][r 2] array, r 1 -No. of rows, r 2 -No. of Cols. 0≤i 1<r 1 and 0≤i 2<r 2, finding the address of a[i 1][i 2] &a[i 1][i 2]=&a[0][0]+(i 1*r 2+i 2)*e_size ; 18
Matrix Computation Diagonal Element Trace of the square matrix Rank Inverse Rotation matrix Eigenvalues 19
Character String (C++) char *str=“IITRoorkee”; for (int i=0; i<10; i++) { cout<<str+i; cout<<*str; } How to print the base address of string (int *) str &str 20
Strings in Java Char char. Array[ ] = new char[4]; char. Array[0]=‘J’; char. Array[1]=‘A’; char. Array[2]=‘V’; char. Array[3]=‘A’; String str; str=new string(“IITRoorkee”); 21
Write a efficient Java or C++ program to find whether given string is Palindrome or not. 22
Iterators in C++ An iterator is a pointer to an element of an object (a pointer to an array) int main( ) { int x[3]={0, 1, 2}; for (int *y=x; y!=x+3; y++) cout<<*y<<“ “; cout<<endl; return 0; } 23
What is the output of following strange code? Why? int a[10]; cout<<a<<endl ; cout<<&a; int a[10]; cout<<a+0<<endl ; cout<<&(a+0); 24