Home > Software engineering >  checking specific condition in array
checking specific condition in array

Time:12-14

have a little problem in exercise in arrays and functions.

I need to write a code that checks every cell in the array for two conditions. The first is that every cell value left to the index exist to the right of the index, and the second is the opposite, every cell value on the right exist to the left.

If those two conditions exist then the cell is "good" and I will add it to the counter of the good cells.

for example: input is 38 79 38 38 17 79 38

For this input I expect that cell #4 will answer the conditions and it will print 1. It will answer the conditions because 38 and 79 exist on the left of #4 and the opposite. Currently for this input it's printing 0 for some reason.

I will gladly use some hints on where the problem is because currently I'm stuck.

Thank you!

// include section
#include <iostream>
#include <cstdlib>

// using section
using std::cin;
using std::cout;
using std::endl;

// constant section
const int N = 7;

// functions section
void input(int arr[]);
bool check_cell(int array[], int cell_index);
bool check_cond_left_to_right(int array[], int cell_index);
bool check_cond_left_to_right(int array[], int cell_index);
bool check_cond_right_to_left(int array[], int cell_index);
bool search_cell_on_the_right(int array[], int cell_value, int original_cell_index);
bool search_cell_on_the_left(int array[], int cell_value, int original_cell_index);


int main() {
    int array[N],
        cell_counter = 0;
    input(array);

    for (int index = 1; index < N - 1; index  ) {
        if (check_cell(array, index)) {
            cell_counter  ;
        }
    }
    cout << cell_counter;
}

void input(int arr[]) { // input function

    for (int index = 0; index < N; index  )
        cin >> arr[index];
}

bool check_cell(int array[], int cell_index) {

    if (check_cond_left_to_right(array, cell_index) &&
        check_cond_right_to_left(array, cell_index)) 
            return true;
    else
        return false;

}

 bool check_cond_left_to_right(int array[], int cell_index) { // check the cells on the left and send them to the right

     for (int i = 0; i < cell_index; i  ) {
         if (search_cell_on_the_right (array, array[i], cell_index) == false)
             return false;
     }
     return true;
}   

 bool check_cond_right_to_left(int array[], int cell_index) {
     for (int i = N; i > cell_index; i--) {
         if (search_cell_on_the_left(array, array[i], cell_index) == false)
             return false;
         else
             continue;
     }
     return true;

 }

 bool search_cell_on_the_right(int array[], int cell_value, int original_cell_index) { // getting cells on the left and checks if they exist on the right
     for (int i = original_cell_index   1; i < N; i  ) {
         if (cell_value == array[i])
             return true;
     }
     return false;
 }

 bool search_cell_on_the_left(int array[], int cell_value, int original_cell_index) {
     for (int i = 0; i < original_cell_index; i  ) {
         if (cell_value == array[i])
             return true;
     }
     return false;
 }

I have tried to identified where the problem is but couldn't find it. Will gladly use some help.

CodePudding user response:

You have an Index out of bounds in one of your functions. If you want to iterate through an array backwards, you have to start at N-1, because N isn't a valid index.

Below is a corrected code

bool check_cond_right_to_left(int array[], int cell_index) {
     for (int i = N - 1; i > cell_index; i--) {
         if (search_cell_on_the_left(array, array[i], cell_index) == false)
             return false;
         else
             continue;
     }
     return true;

 }
  • Related