mercredi 1 avril 2020

Trouble with index

Write the find function, which takes a fixed integer vector reference and a single value integer and returns the index of the first occurrence of this value in the vector or the length of the vector if no value in it. The function should be adapted for use in the sample program below. The function uses only the vector header file.

Sample program
int main () {
int result = find (std :: vector <int> {3, -1, 7, 12, -5, 7, 10}, 7);
std :: cout << result << std :: endl; }

This is what I've done so far :


#include <vector>
#include <iostream>
using namespace std;

int find(const vector<int> &r ,int number)
{
    int i=0;
    int x;
    for(i;i<r.size();++i)
    {
        if(r[i]==number)
        {

            break;

        }   

    }
return 0  ;
}

int main()
{
int result = find(vector<int> {3, -1, 7, 12, -5, 7, 10}, 7);
cout << result << endl; 
}

And Im stuck,I dont know how to make it to return the number of index.

Aucun commentaire:

Enregistrer un commentaire