-4

This is pretty easy if I can import <algorithm> or any other things at the top, but I just started using C++ and my professor asked us to use only loops and conditionals for this problem.

The problem itself is to give a vector v and an int seek to a function. A loop runs through the vector to find the index of the first location of the number. If it can be found, then return the index of where the number is, if not then return -1.

I got the first part down, but now I am having an issue with the second part.

#include <iostream>
#include "vectorutils.h"
#include <vector>


using namespace std;
//endl = new line


int main() {
    cout << find({1, 2, 3, 4, 5, 6, 7, 8}, 28) << endl;
}

int find(const std::vector<int>& v, int seek) {
    int ind = 1;
    for (int i = 0; i < v.size(); i++) {
        if (seek == v[i]) {
            return i;
        }
    else {
            return -1;
    }
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
novi
  • 3
  • 2
  • 1
    [How to find out if an item is present in a std::vector?](https://stackoverflow.com/questions/571394/how-to-find-out-if-an-item-is-present-in-a-stdvector) – Drew Dormann Aug 31 '22 at 20:48
  • So what's your problem? What output do you get? Have you stepped through your code using your debugger? – Stephen Newell Aug 31 '22 at 20:50
  • 2
    Please read [how to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). Take a pen and some paper and trace through the loop for a couple of different vector inputs. Right down, on paper, what each variable is at each loop iteration and when you stop iterating. – Silvio Mayolo Aug 31 '22 at 20:50
  • What does "having the issue with the second part" mean? The shown code won't compile due to syntax errors. And even if it did, what is your question for Stackoverflow? "Having the issue" is a statement, it is not an answerable question. Before posting their first question on stackoverflow.com, everyone should take the [tour], read the [help], understand all the requirements for a [mre] and [ask] questions here. Not doing any of this results in a poor quality question almost every time. It then gets downvoted, closed, and then deleted. – Sam Varshavchik Aug 31 '22 at 20:50
  • @novi There is absent a closing brace in the function definition. – Vlad from Moscow Aug 31 '22 at 20:51
  • If you indented you program with a tool instead of doing it manually, you'd see that your `find` function is missing the ending `}` - also, thinking about what happens if the first element in the `vector` is _not_ equal to `seek` should help. – Ted Lyngmo Aug 31 '22 at 20:54
  • While I do agree that the duplicate question solves the *underlying problem*, none of its answers satisfy the OP's particular requirement that "*my professor asked us to use **only loops and conditionals** for this problem*". They all go for the standard algorithm approach (as they should). – Remy Lebeau Aug 31 '22 at 20:55
  • How about `std::find`? If the vector is sorted, you could use `std::binary_search(), std::lower_bound()` or `std::upper_bound`. – Thomas Matthews Aug 31 '22 at 21:41
  • @ThomasMatthews "*my professor asked us to use **only loops and conditionals** for this problem*", which means no standard algorithms. – Remy Lebeau Aug 31 '22 at 21:47

1 Answers1

4

You have the return -1; statement in the wrong place. It needs to be after the loop exits, not inside the loop. For example:

int find(const std::vector<int>& v, int seek) {
    for (int i = 0; i < v.size(); i++) {
        if (seek == v[i]) {
            return i;
        }
        // else {
        //    return -1;
        // }
    }
    return -1; // <-- moved down here!
}

In your original code (if it could compile, which it doesn't), the logic is wrong. If the vector is empty then you return nothing at all (which is undefined behavior), and if the 1st element does not match the number then you return -1 immediately without checking the remaining elements at all.

The above change fixes both of those issues.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770