### Print elements from matrix within interval

Hello.

I need to make a program that inputs data for a matrix B[m][n] where m<8 and n<10.
There is interval [c,d] and I need to find which matrix row have the most elements within that interval.

The code:

 ``123456789101112131415161718192021222324252627282930313233343536`` ``````#include int main () { int A[8][10]; int i,j,m,n,c,d,br=0; int z; printf ("m= "); scanf ("%d",&m); if (m>7) {printf ("\n \n" "This program accepts maxmimum 7 and minimum 1 rows. Try again."); return 0;} printf ("\n" "n= "); scanf ("%d",&n); if (n>9) {printf ("\n \n" "This program accepts maximum 9 and minimum 1 columns. Try again."); return 0;} printf ("\n \n"); for (i=0;i=c && A[i][j]<=d && A[i][j]!=0) { br++; printf ("%d ",A[i][j]);}} printf ("\n \n" "There are %d elements in the matrix that belong to the interval [%d,%d]",br,c,d); return 0; } // end ``````

Currently the program is displaying the count and the values of the elements within the interval from the WHOLE matrix. I need it to output the matrix row with the most elements within the interval.

Any ideas?

Regards,
Sasha
 ``123456789101112`` ``````for (i=0;i<=m;i++) { // What if you would start here to count for row i? for (j=0;j<=n;j++) { if (A[i][j]>=c && A[i][j]<=d && A[i][j]!=0) // Why is 0 not allowed? { br++; } } // ... and make use of the count for row i here? }``````
Last edited on
Hello. Thanks for the hint.

Do I understand you correctly?

 ``1234567`` ``````for (i=0;i<=m;i++) { for (i=0;i<=i;i++) for (j=0;j<=n;j++) { if (A[i][j]>=c && A[i][j]<=d && A[i][j]!=0) { br++;}} printf ("%d",i);}``````

Program compiles fine but crashes when executed. I think I don't understand you properly. I don't count for 0 because it's not part of the matrix. I think matrices can't have 0 elements, can they?

It will be nice if the program output the values of the elements and not only their row.
Topic archived. No new replies allowed.