In: Computer Science
#include<stdio.h>
#include<stdlib.h>
int main()
{
//Q1) 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 in
general.asu.edu server
/* i.) Create a 2 - D array of integers using pointers
(use dynamic memory allocation).
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
*/
return 0;
C language and please comment on your code
Code
#include <stdio.h>
#include<stdlib.h>
int main(void) {
int r = 4, c = 5;
int i,j,num;
//dynamiclly assing memeory to 2d array
int *arr = (int *)malloc(r * c * sizeof(int));
int min,max;
//add value to the array
for (i = 0; i < r; i++)
for (j = 0; j < c; j++)
{
*(arr + i*c + j) = rand()%(100-1) + 1;
}
max=*(arr + 0*c + 0);
//print array
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{
printf("%d\t",*(arr + i*c + j));
if(*(arr + i*c + j)>min)
max=*(arr + i*c + j);
}
printf("\n");
}
printf("\nThe largest value in the 1st row: %d\n",max);
//finding min from 1st row
min=*(arr + 0*c + 0);
i=0;
for (j = 0; j < c; j++)
{
if(*(arr + i*c + j)<min)
min=*(arr + i*c + j);
}
printf("\nThe smallest value in the 1st row: %d\n",min);
//finding min from 1st col
min=*(arr + 0*c + 0);
i=0;
for (j = 0; j < r; j++)
{
if(*(arr + i + j*c)<min)
min=*(arr + i + j*c);
}
printf("\nThe smallest value in the 1st col: %d",min);
//prints diagonal elements
printf("\n\nDiagonal Elemnts: ");
for (i = 0; i < r; i++)
for (j = 0; j < c; j++)
if(i==j)
printf("%d\t",*(arr + i*c + j));
return 0;
}
output
If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.