-5

The goal is to search the array and find a specific number in it and return the position. I have the method for how to do it down but trying to get it to run in the main method is giving me the error in the title. What do i do to fix?

#include "stdafx.h"
#include <iostream>
using namespace std;

int searchArray(int a[], int x) {
    for (int i = 0; i < a[100]; i++) {
        if (x == a[i])
            return i + 1;
        break;
    }
    return -1;
}

int main()
{
    int wait, x, y, a[100];

    //problem 3
    cout << "Enter the size of the array(1-100): ";
    cin >> y;

    for (int i = 0; i < y; i++) {
        cout << "Enter an array of numbers:";
        cin >> a[i];
    }
    searchArray(a[100], x); //i get error on this line with a[100]

    cin >> wait;

    return 0;
}

Expected is it should run with no errors and find position of a number in the array but I just get the error and cant run it.

Mooing Duck
  • 64,318
  • 19
  • 100
  • 158
  • Function expects first parameter to be pointer but you are passing value to it. Also, value x is not initialized. – NutCracker Mar 29 '19 at 22:26
  • 1
    `int` is an integer. `int *` is a pointer. You're not passing a pointer. `a[100]` is the 100th element of the int array `a`, not the array itself. – Ken White Mar 29 '19 at 22:28
  • @Chaos_warfare24 1) You can fix the immediate error by passing the pointer to the first array element, instead the out-of-bounds array element: `searchArray(a, x);` 2) The next problem you would hit is undefined behavior in `i < a[100]`, due to you indexing array out of bounds (did you mean `i < 100`?). 3) Consider learning from a [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Algirdas Preidžius Mar 29 '19 at 22:36
  • 2
    @KenWhite "_a[100] is the 100th_" it's the element at index 100, which we call the 101th – curiousguy Mar 29 '19 at 22:36
  • @curiousguy: You're correct, of course. :-) – Ken White Mar 29 '19 at 22:45
  • @KenWhite what is the correct way to fix this issue? – Chaos_warfare24 Mar 29 '19 at 22:50
  • @AlgirdasPreidžius has already told you how to fix it. You want to pass the array, not a single element of that array. How would you do that? – Ken White Mar 29 '19 at 22:50
  • Really? No mention of `std::find_if` anywhere?! Even `std::find` would do here, actually... – Daniel Kamil Kozar Mar 29 '19 at 23:01

2 Answers2

-1

After the for() loop in main(), you need something like:

cout << "Enter the value to search for: ";
cin >> x; 

wait = searchArray(a, x);

cout << x;
cout << " is at position: ";
cout << wait;
myk
  • 708
  • 2
  • 8
  • 20
-2
int searchArray(int a[], int x) 
{
    for (int i = 0; i < 100; i++) //change a[100] to 100 because it only needs the size not the array itself
    {
        if (x == a[i])
            return i + 1;
        break;
    }
    return -1;
}

int main()
{
    int wait, x, y, a[100];

    cout << "Enter the size of the array(1-100): ";
    cin >> y;

    for (int i = 0; i < y; i++) {
        cout << "Enter an array of numbers:";
        cin >> a[i];
    }

    cout<<"Number to look for: "; //sets a value for x
    cin>>x;

    cout<<"Index: "<<searchArray(a, x)<<endl; //function returns an int therefore a cout is needed

    cin >> wait;

    return 0;
}