Home > Net >  Searching for a number in a given array using pointers
Searching for a number in a given array using pointers

Time:11-06

I need to create a program that searches for a user inserted number from an array using pointers. This is my current code

#include <iostream>
using namespace std;

void FindNumber(int *ptrArr, int size, int *ptr1) {
    for (int *p = ptrArr; p < ptrArr   size;   p) {
        if (ptrArr[*p] == *ptr1) {
            cout << "Number (" << *ptr1 << ") found in the array with an index of: " << *p;
            break;
        }

        if (*p == size) {
            cout  << "No such number in given array\n";
        } 

    }
}

int main () {
    int numbers[10] = {5, 4, 7, 10, 24, 15, 8, 2, 9, 13};
    int num;

    cout << "What number do you want to search for?\n";
    cin >> num;

    FindNumber(numbers, sizeof(numbers) / sizeof(int), &num);
    return 0;
}

The problem is with for loop, but I don't know what it is Sometimes it finds the number with right index, sometimes it doesn't find it even though there is that particular number in the array, sometimes it finds the number but outputs the wrong index

What number do you want to search for?
7
No such number in given array
Number (7) found in the array with an index of: 2

What number do you want to search for?
5
No such number in given array

Tried changing the for loop on my own but no successo. Hoping for some help.

CodePudding user response:

You have 2 small typos in your code.

-No need to index the array with "*p"

-The index need to be calculated by subtracting p from the original pointer.

Please see here the fixed code:

#include <iostream>
using namespace std;

void FindNumber(int* ptrArr, int size, int* ptr1) {
    for (int* p = ptrArr; p < ptrArr   size;   p) {
        if (*p == *ptr1) {
            cout << "Number (" << *ptr1 << ") found in the array with an index of: " << (p-ptrArr);
            return;
        }
    }
    cout << "No such number in given array\n";

}

int main() {
    int numbers[10] = { 5, 4, 7, 10, 24, 15, 8, 2, 9, 13 };
    int num;

    cout << "What number do you want to search for?\n";
    cin >> num;

    FindNumber(numbers, sizeof(numbers) / sizeof(int), &num);
    return 0;
}

CodePudding user response:

If you're iterating though the array with a pointer, you just dereference the pointer to access the current element instead of using the [] operator on the pointer. For determining the index, you can use std::distance

void FindNumber(int *const ptrArr, int size, int const* const ptr1)
{
    for (int* pos = ptrArr; pos != ptrArr   size;   pos)
    {
        if (*pos == *ptr1) { // compare element at the current position with input
            cout << "Number (" << *ptr1 << ") found in the array with an index of: " << std::distance(ptrArr, pos) << '\n';
            return;
        }
    }
    std::cout  << "No such number in given array\n";
}

If you aren't allowed to use std::distance, you can replace

std::distance(ptrArr, pos)

with

(pos - ptrArr)

Note: You should avoid pointers, if possible. There's no point in passing the third parameter as pointer. Furthermore this kind of logic is already available in the <algorithm> standard library header.

void FindNumber(int *const ptrArr, int size, int value)
{
    auto const end = ptrArr   size;
    auto const pos = std::find(ptrArr, end, value);
    
    if (pos == end)
    {
        std::cout  << "No such number in given array\n";
    }
    else
    {
        std::cout << "Number (" << value << ") found in the array with an index of: " << std::distance(ptrArr, pos) << '\n';
    }
}
  •  Tags:  
  • c
  • Related