Question: I need help to combine finding zeros into one function. The function can find any number of zeros. See below. Thanks. #include int lin_search(int rows,
I need help to combine finding zeros into one function. The function can find any number of zeros. See below. Thanks.
#include
int lin_search(int rows, int cols, int arr[5][10]) { int i = 0; while(i < rows) { int j = 0; while(j < cols) { if(arr[i][j] == 0) return 1; j += 1; } i += 1; } return 0; }
int main() { int arr[5][10] = { {10, 11, 12, 13, 7, 10, 11, 12, 13, 7}, {0, 11, 2, 1, 7, 0, 11, 2, 1, 7}, {10, 17, 1, 13, 0, 10, 17, 1, 13, 0}, {0, 0, 12, 13, 0, 0, 0, 12, 13, 0}, {3, 51, 12, 33, 7, 3, 51, 12, 33, 7} }; int zero = lin_search(5, 10, arr); printf("Zero present? %s ", (zero == 1) ? "Yes":"No"); return 0; }
#include
int lin_search_2(int rows, int cols, int arr[5][10]) {
int i = 0;
while(i < rows) {
int j = 0;
int count =0; //to keep track of number of zeroes in sequence
while(j < cols) {
if(arr[i][j] == 0)
count++;
else
count=0;
if(count == 2)
return 1;
j += 1;
}
i += 1;
}
return 0;
}
int lin_search_3(int rows, int cols, int arr[5][10]) {
int i = 0;
while(i < rows) {
int j = 0;
int count =0;//to keep track of number of zeroes in sequence
while(j < cols) {
if(arr[i][j] == 0)
count++;
else
count=0;
if(count == 3)
return 1;
j += 1;
}
i += 1;
}
return 0;
}
int main() {
int arr[5][10] = {
{10, 11, 12, 13, 7, 10, 11, 12, 13, 7},
{0, 11, 2, 1, 7, 0, 11, 2, 1, 7},
{10, 17, 1, 13, 0, 10, 17, 1, 13, 0},
{0, 0, 12, 13, 0, 0, 0, 12, 13, 0},
{3, 51, 12, 33, 7, 3, 51, 12, 33, 7}
};
int zero = lin_search_2(5, 10, arr);
printf("Two Zeros present in sequence? %s ", (zero == 1) ? "Yes":"No");
zero = lin_search_3(5, 10, arr);
printf("Three Zeros present in sequence? %s ", (zero == 1) ? "Yes":"No");
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
