compare an entire row of a matrix

Asked

Viewed 114 times

0

What I want to do is simple: I want to compare an array or an entire row of variables of an array in search of certain values in order

var[2][]=={1,2,3,4,5} //inteiro. todos de uma vez

instead of

var[2][0]==1 && var[2][1]==2 && var[2][2]==3 && var[2][3]==4 && var[2][4]==5 //inteiro. um por um

or else

vartwo[4][3...7]==var[2][] or vartwo[4][]==var[2][]

1 answer

0

Take a look at the code below, the source is at the end of the code. Hug.

#include<stdio.h>

/* Searches the element x in mat[][]. If the 
element is found, then prints its position 
and returns true, otherwise prints "not found"
and returns false */
int search(int mat[4][4], int n, int x) {
  int i = 0, j = n-1;  //set indexes for top right element
  while ( i < n && j >= 0 ) {
   if ( mat[i][j] == x ) {
     printf("n Found at %d, %d", i, j);
     return 1;
   }
   if ( mat[i][j] > x )
     j--;
   else //  if mat[i][j] < x
     i++;
   }

  printf("n Element not found");
  return 0;  // if ( i==n || j== -1 )
}

// driver program to test above function
int main()
{
 int mat[4][4] = {{10, 20, 30, 40},
                 {15, 25, 35, 45},
                 {27, 29, 37, 48},
                 {32, 33, 39, 50},
};
search(mat, 4, 29);
return 0;
}

http://www.geeksforgeeks.org/search-in-row-wise-and-column-wise-sorted-matrix/

  • i did not understand very well. my intention is to get true or false to use on items from if/else as if (//comparação de matriz) {&#xA; //do stuff&#xA;}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.