Finding whether 2 arrays are intersecting

Here are 2 arrays: **A1 1 8 7 5 6 A2 0 9 6 4 2 ** **These two are intersecting at 6. Complexity? ** Solution: This can be solved using Hash Tables. Take 1 Hash Table. Insert elements from A1 in Hast Table as key and put value in front of them (In case elemente get repeated increment the value count). Now traverse the second array A2 and check for element as a key. [Read More]

Given an array of n integers from 1 to n with one integer repeated twice

Problem - Given an array of n integers from 1 to n with one integer repeated twice This can be done via normal array traversal or hash table to find duplicates. But since we are given that the integers are from 1 to n, then there are better methods available. Approach 1 - Arithmetic sum approach So, the efficient method to solve it calculate the sum of actual input array, and expected sum of the array. [Read More]

C code to dynamically allocate one, two and three dimensional arrays (using malloc())

One Dimensional Array int \*myarray = malloc(no\_of\_elements \* sizeof(int)); //Access elements as myarray\[i\] 2 Dimensional Array Method1 int \*\*myarray = (int \*\*)malloc(nrows \* sizeof(int \*)); for(i = 0; i < nrows; i++) { myarray\[i\] = malloc(ncolumns \* sizeof(int)); //allocating 1 D array = number of elements in column } // Access elements as myarray\[i\]\[j\] Method 2 (Contagious Allocation) int \*\*myarray = (int \*\*)malloc(nrows \* sizeof(int \*)); myarray\[0\] = malloc(nrows \* ncolumns \* sizeof(int)); for(i = 1; i < no\_of\_rows; i++) myarray\[i\] = myarray\[0\] + (i \* ncolumns); // Access elements as myarray\[i\]\[j\] In either case, the elements of the dynamic array can be accessed with normal-looking array subscripts: array[i][j]. [Read More]

Initialising arrays

Conside the 2D arrays:  int anArray1[2][3] = {7}; `int anArray2[3][5] = { { 1, 2, 3, 4, 5, }, // row 0 { 6, 7, 8, 9, 10, }, // row 1 { 11, 12, 13, 14, 15 } // row 2 }; int anArray3[][5] = { { 1, 2, 3, 4, 5, }, { 6, 7, 8, 9, 10, }, { 11, 12, 13, 14, 15 } };` [Read More]

Multi-dimensional arrays

#include #include int main() { int a[3][3][3][3]; //it gives address of a[0][0][0][0] . printf(” \n address of array a is %u”, a); printf("\n address of a[2][0][0][0] is %u , " “given by a[2] , %u given by a+2”, a[2], a + 2); printf("\n address of a[2][2][0][0] is %u , " “given by a[2][2] , %u given by a[2]+2”, a[2][2], a[2] + 2); printf("\n address of a[2][2][1][0] is %u , " [Read More]

Program to manage array of pointers to strings

Program to manage array of pointers to strings (Source Code) // Program Ch03pr04 // Program to manage array of pointers to strings #include #include #include const int MAX1 = 6 ; const int MAX2 = 10 ; class string { private :  char *names[MAX1] ; int count ;  public :  string( ) ; int add( ) ; void show( ) ; ~string( ) ; } ; // allocates the memory [Read More]

Program with the use of array of pointers to strings

Program with the use of array of pointers to strings(Source Code) // Program: Ch03pr03 // Program with the use of array of pointers to strings #include #include #include const int MAX = 6 ; class string { private :  char *names[MAX] ; int count ;  public :  string( ) ; int add ( char *s ) ; void swap ( int i, int j ) ; void show( ) ; [Read More]

Program to manage a 2-D array of Characters

Program to manage a 2-D array of Characters(Source Code) // Program Ch03pr02 // Program to manage a 2-D array #include #include const int MAX1 = 6 ; const int MAX2 = 10 ; class string { private :  char masterlist[MAX1][MAX2] ; int count ;  public :  string( ) ; int add ( char *s ) ; int find ( char *s ) ; } ; // initialises the data [Read More]

Program to merge two 1-D arrays

Here is the program in cpp: #include <iostream.h> #include <conio.h> using namespace std; const int MAX1 = 5 ; const int MAX2 = 7 ; class array { private : int \*arr ; int size ; public : void create ( int sz ) ; void sort( ) ; void display( ) ; void merge ( array &a , array &b ) ; } ; // creates array of given size sz, dynamically void array :: create ( int sz ) { size = sz ; arr = new int\[size\] ; int n ; for ( int i = 0 ; i < size ; i++ ) { cout << "\\nEnter the element no. [Read More]