In: Computer Science
Declare a 3x4 multidimensional array. Receive values for each element from user. Find the smallest value in the multidimensional array, display its index and value, use array access operator [ ]. Find the largest value in the multidimensional array, display its index and value, use pointers. Write C code.
code:
#include
typedef int Array[3][4];
int main()
{
int a[3][4],i,j;
printf("Enter the values\n");
//get the values in to the array
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
{
scanf("%d",&a[i][j]);
}
}
//find the smallest value and the index
int value=a[0][0],row=0,column=0;
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
{
//condition to check the smallest value
if(a[i][j]
value=a[i][j];
row=i;
column=j;
}
}
}
//print the smallest and the index values
printf("Smallest number is %d\n",value);
printf("Index is %d %d\n",row,column);
//assign a pointer to an array with nummvalue
Array *b =NULL;
//assign the values of a to pointer array
b=&a;
int value1=0,row1=0,column1=0;
for (i=0;i<3;i++) //Loop of row
{
for(j=0;j<4;j++)
{
//condition to check the largest number
if((*b)[i][j]>value1)
{
value1=(*b)[i][j];
row1=i;
column1=j;
}
//printf(" array elements = %d\n", (*b)[i][j]);
}
}
//print the values
printf("Largest element is : %d\n",value1);
printf("Index is : %d %d\n",row1,column1);
return 0;
}
output: