c++ - Find the smallest number in an array and the position it is -
i have code, , want find smallest number , in position in array 3x3,how do that?
#include <stdio.h> int main() { int a[3][3]; int i,j; int min; min=0; for(i=0;i<3;i++) { printf("\n"); for(j=0;j<3;j++) { scanf("%d",&a[i][j]); } } for(i=0;i<3;i++) { for(j=0;j<3;j++) { if(a[i][j]>=min) min=a[i][j]; } printf("\n"); printf("%d\n",min); }
}
try this
#include < stdio.h > #include < limits.h > int main() { int a[3][3]; int i, j; (i = 0; < 3; i++) { printf("\n"); (j = 0; j < 3; j++) { scanf("%d", &a[i][j]); } } int min = int_max, y = -1, x = -1; (i = 0; < 3; i++) { (j = 0; j < 3; j++) { if (a[i][j] < min) { min = a[i][j]; y = i; x = j; } printf("%d ", a[i][j]); } printf("\n"); } printf("minimum %d, located @ %d, %d\n", min, y, x); }
Comments
Post a Comment