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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <stdio.h>
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<m;i++)
for (j=0;j<n;j++) {
    printf ("A[%d][%d]= ",i,j);
    scanf ("%d",&A[i][j]);}
    
printf ("\n \n" "c= ");
scanf ("%d",&c);
printf ("\n" "d= ");
scanf ("%d",&d);

printf ("\n \n" "Matrix elements within the interval [%d,%d]: ",c,d);
for (i=0;i<=m;i++)
for (j=0;j<=n;j++) {
    if (A[i][j]>=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
1
2
3
4
5
6
7
8
9
10
11
12
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?

1
2
3
4
5
6
7
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.