In: Computer Science
Note: You can use only pointer based operations while implementing each of the functionalities below// zero points will be given if pointer operations are not used in implementing // each of the operations below. // Also, use C code only in visual studio or GCC
i) Create a 2 - D array of integers using pointers (use dynamic memory allocation) in C. Assume that the array has 4 rows and 5 columns.Then, fill the array with randomly generated int values between 1 and 100. */
ii.) Find the largest values in the array
iii.) Find the smallest value in the 1st row
iv.) Find the smallest value in the 1st column
v.) Display the elements in the diagonal
Thanks for the question. Below is the code you will be needing. Let me know if you have any doubts or if you need anything to change.
If you are satisfied with the solution, please leave a +ve feedback : ) Let me know for any help with any other questions.
Thank You!
===========================================================================
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main(){
srand(time(NULL));
int ROWS = 4;
int COLUMNS = 5;
int**p = (int**)malloc(sizeof(int*)*ROWS)
;
int row,col;
// // fill 2d array will random numbers
for(row=0; row<ROWS; row++){
*(p+row) =
(int*)malloc(sizeof(int)*COLUMNS);
for(col=0;col<COLUMNS;col++){
*(*(p+row)+col)
= rand()%100 +1;
printf("%4d",*(*(p+row)+col));
}printf("\n");
}
//ii.) Find the largest values in the array
int largest = **p;
for(row=0; row<ROWS; row++){
for(col=0;col<COLUMNS;col++){
if(*(*(p+row)+col)>largest) largest = *(*(p+row)+col);
}
}
printf("Largest Number in the 2-D array: %d\n",
largest);
//iii.) Find the smallest value in the 1st row
int smallest = **p;
for(col=0; col<COLUMNS;col++){
if(*((*p)+col)<smallest)
smallest = *((*p)+col);
}
printf("Smallest Number in the first row: %d\n",
smallest);
//iv.) Find the smallest value in the 1st column
smallest = **p;
for(row=0; row<ROWS;row++){
if(*(*(p+row))<smallest)
smallest = *(*(p+row));
}
printf("Smallest Number in the first column: %d\n",
smallest);
//v.) Display the elements in the diagonal
printf("Diagonal Numbers:");
for(row=0; row<ROWS; row++){
for(col=0;col<COLUMNS;col++){
if(row==col)
printf("%d ",*(*(p+row)+col));
}
}
return 0;
}
=====================================================================