In: Computer Science
In printArray2D(prints a 2-D array)
void printArray2D(int arr[M][N]){ /*defines a function named printArray2D of return value void (means it returns nothing and receives a 2-D array named arr with M rows and N columns*/
int i, j;//declares two integers i and j
for(int i =0; i < M; i++){ /*runs a for loop from i=0 to i<m to iterate through the rows, M is number of columns in a 2-D array passed as parameter to the function( named arr)*/
for(int j=0; j<N; j++){ /*runs a for loop from j=0 to i<n to iterate through the columns,N is number of columns in a 2-D array passed as parameter to the function( named arr)*/
printf(" %d ", arr[i][j]); /*prints all the array elements %d specifies the type of output as integer and printf takes to argunments here first one displays the string as it is without %d,which gets replaced by arr[i][j] whien iterated through array using for loop*/
}
printf("\n");/*prints the output on next
line->'\n' basically means change the line
}
}
_______________________________________________________________________________________
In populateRandom2D(populates a 2-D array by random elements within min and max range)
void populateRandom2D(int arr[M][N]){
/*defines a function named populateRandom2D of return value void (means it returns nothing and receives a 2-D array named arr with M rows and N columns*/
int i, j;//declares two integers i and j
int min, max;//declares two integers min and max
printf("Enter min number in the array:\n"); /*prints the value between "" on the screen as it is and changes line for next event ,input or output(due to \n at the end)*/
scanf("%d", &min); /*receives the value from the console of type(integer , %d signifies type as integer) and store the value in variable named min as defined on the 4th line*/
printf("Enter max number in the array:\n"); /*prints the value between "" on the screen as it is and changes line for next event ,input or output(due to \n at the end)*/
scanf("%d", &max); /*receives the value from the console of type(integer , %d signifies type as integer) and store the value in variable named max as defined on the 4th line*/
for(int i =0; i < M; i++){ /*runs a for loop from i=0 to i<m to iterate through the rows, M is number of columns in a 2-D array passed as parameter to the function( named arr)*/
for(int j=0; j<N; j++){ /*runs a for loop from j=0 to i<n to iterate through the columns,N is number of columns in a 2-D array passed as parameter to the function( named arr)*/
arr[i][j] = rand() % (max+1-min) +
min;/*generate the random numbers between the range of min
and max using predefined rand() function that generates random
numbers,to make sure the limit of random number generated between
min and max,it is further manipulated by taking modulo and adding
min.*/
}
}
}
_______________________________________________________________________________________
In linearSearch2D(searches for an element in a 2-D array using linear search method)
void linearSearch2D(int arr[M][N]){ /*defines a function named linearSearch2D of return value void (means it returns nothing and receives a 2-D array named arr with M rows and N columns*/
int i, j, r; //declares three integers i, j and r
printf("Please enter a value to search for:"); /*prints the value between "" on the screen as it is */
scanf("%d", &r); /*receives the value from the console of type(integer , %d signifies type as integer) and store the value in variable named r as defined on the 2nd line*/
for(int i =0; i < M; i++){ /*runs a for loop from i=0 to i<m to iterate through the rows, M is number of columns in a 2-D array passed as parameter to the function( named arr)*/
for(int j=0; j<N; j++){ /*runs a for loop from j=0 to i<n to iterate through the columns,N is number of columns in a 2-D array passed as parameter to the function( named arr)*/
if(r == arr[i][j]){/*loop the array and print
out the location of the element when it catches the element entered
to be present in the array,since whole statement is in for loop ,
it checks the same for each element of the array and prints the
below statement if match occurs*/
printf("value %d was found at (%d, %d)\n", r, i+1,
j+1);/*prints the value r, alongwith the position it was
found at(in the array) signified using i+1 and j+1(i+1 & j+1
because of 0-indexing and we need to display position not index)
*/
}
}
}
}
_______________________________________________________________________________________
In rightShift2D(right shifts the content of an array)
void rightShift2D(int arr[M][N]){ /*defines a function named rightShift2D of return value void (means it returns nothing and receives a 2-D array named arr with M rows and N columns*/
int i,j, temp;//declares three integers i, j and temp
int a[M][N];//declare nother array a[M][N] as the same size of the arr[M][N]
temp = arr[3][2]; //save last value of arr into temp variable
for(int i =0; i < M; i++){ /*runs a for loop from i=0 to i<m to iterate through the rows, M is number of columns in a 2-D array passed as parameter to the function( named arr)*/
for(int j=0; j<N; j++){ /*runs a for loop from j=0 to i<n to iterate through the columns,N is number of columns in a 2-D array passed as parameter to the function( named arr)*/
a[i][j] = arr[i][j];//store the original arrray contents to the new array a[M][N]
if(j==0){//check for the first column of the array j is for iterating the columns (2nd for loop).
arr[i][j] = a[i-1][j+2];//store the temp array a[i-1][j+2]'s content to the location specified as arr[i][j],it is basically moving last element of a row to the first column of next row but only for first element of a column checked in if condition as j==0*/
else{//beginning of else of the if
arr[i][j] = a[i][j-1];//simply shift elements to the right,ie,to the next column in else case
}
}
}
arr[0][0] = temp; /*assign the first element(previously
stored in temp) as the last element in the array */
}