jeudi 28 novembre 2019

Finding of specific values in an array C++

I want to find the position of an element in an array, the thing is that when I create the function to return the index of the desired element the if statement does not work. However, if I do it in the main function it works perfectly. I would appreciate if someone could explain me why this is happening.

#include <iostream>
#include <algorithm>
int find_index(double vector, double *value, int size_vector);


int main() {
    double vec_eye_x[2]={2323,1}; // center position x two eyes.
    double vec_eye_y[2]={33,2}; // center position y two eyes.
    int index_val=1;
    double *yc_max=std::max_element(vec_eye_y,vec_eye_y+2); // Creating a pointer
    double *yc_min=std::min_element(vec_eye_y, vec_eye_y+2);
    double *xc_max=std::max_element(vec_eye_x,vec_eye_x+2);
    double *xc_min=std::min_element(vec_eye_x, vec_eye_x+2);

    int index=0;
    for(int i=0; i<2; i++){
        if(*xc_min==vec_eye_x[i]){std::cout<<"Works in the main function"<<std::endl;}
    }
    std::cout << "index val: " <<find_index(vec_eye_x, &xc_max,2)<<std::endl;
    return 0;
}


int find_index(double vector_x, double *value, int size_vector){
    int index=0;
    for(int i=0; i<size_vector; i++){
        if(*value==vector_x[i]){index=i;}
    }
    return index;
}

Aucun commentaire:

Enregistrer un commentaire