118k views
4 votes
8. Write a function that takes a 2D array as an input and outputs all values greater than 20 into a column array. Use logical indexing array

User Sjking
by
5.0k points

1 Answer

3 votes

Answer:

Follows are the code to this question:

#include<stdio.h>//defining header file

int main()//defining main method

{

int d[2][2];//defining 2D array

int i, j;//defining integer variable

printf("Enter values: \\");//print message

for(i=0; i<2; i++)//defining for loop for input column value

{

for(j=0;j<2;j++)//defining for loop for input row value

{

scanf("%d", &d[i][j]);//input values

}

}

printf("value which is greater than 20: \\ ");//print message

for(i=0; i<2; i++)//defining for loop for print column value

{

for(j=0;j<2;j++)//defining for loop for print row value

{

if(d[i][j]>20)//defining if block that check value is greater then 20

{

printf("%d ", d[i][j]);//print values

}

}

printf("\\");//use print for line break

}

return 0;

}

Output:

Enter values:

44

12

11

55

value which is greater than 20:

44

55

Step-by-step explanation:

In the above-given code, a 2D array "d" and two integer variable "i and j" are defined, in which two for loop are used for input value from the user end.

After accepting the value from the user end, and again use the two for loop, in which, if block is defined, that checks array value is greater than 20 and print its values.

User Joyleen
by
5.0k points