Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello i have a small problem. I create a matrix and i want to check if any element is dividable by the num of the row he is in and the num of the column he is in.
For example:
1 2 3
4 5 6
7 8 9
6 is in the 3rd row and 2nd column. 2|6 and 3|6. So he fits with the output. Here is what i have so far.

#include <stdio.h>
#include <stdlib.h>

int main(){
    int nrows,ncols;
    scanf("%d %d", &nrows, &ncols);
    int matA[nrows][ncols];
    int i,j;
    for(i = 0; i < nrows; i++){
        for(j = 0; j < ncols; j++){
        scanf("%d",&matA[i][j]);
        }
    }


for(i = 0; i < nrows; i++){
        for(j = 0; j < ncols; j++){
        printf(" %d \t",matA[i][j]);
    }
    printf("\n");
}
printf("\n");
for(i = 1; i < nrows; i++){
    for(j = 1; j < ncols; j++){
        int nums = matA[i][j];
        if(nums / i && nums / j){
            printf(" %d \t", nums);
        } else {
            printf("Nema pogoden broj!");
        }
    }
}

    return 0;
}


What I have tried:

I've tried dividing by the number but i can't get it to return the count of the current number
Posted
Updated 28-May-18 21:38pm
Comments
Mohibur Rashid 28-May-18 23:28pm    
6 is in the second row and third column.
GKP1992 28-May-18 23:45pm    
What do you mean by the count of the current number? Do you want to know how many hits there are in the matrix or just the number which follows the condition?
Member 13848568 29-May-18 1:23am    
I want to know if the number is dividable with the num of it's row and the num of it's column

if(nums / i && nums / j)

This does not check divisability; it will always return a value, but without the fraction as you are using integers.

You could use the modulus operator (remainder after division) instead:
if(nums % i == 0 && nums % j == 0)
 
Share this answer
 
Comments
[no name] 29-May-18 2:23am    
I saw you marked my answer as solution but then undid it. Please leave a comment if this didn't answer your question :)
I would say 6 is in the 2nd row and 3rd column.
Try
C
#include <stdio.h>
#include <stdlib.h>

int main()
{
  int nrows,ncols;
  scanf("%d %d", &nrows, &ncols);
  int matA[nrows][ncols];
  int i,j;
  for(i = 0; i < nrows; i++)
    for(j = 0; j < ncols; j++)
      scanf("%d",&matA[i][j]);


  for(i = 0; i < nrows; i++)
  {
    for(j = 0; j < ncols; j++)
      printf(" %d \t",matA[i][j]);
    printf("\n");
  }

  printf("\n");
  for(i = 0; i < nrows; i++)
    for(j = 0; j < ncols; j++)
    {
      int nums = matA[i][j];
      if(nums % (i+1) || nums % (j+1))
        printf("Nema pogoden broj!\n");
      else
        printf(" %d\n", nums);
    }
   return 0;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900