Search for a Number in an Array in C++

In this exercise, you will create a program that searches for a specific number in an array. Searching through an array is a common task in programming, which is used in various applications such as finding specific values, validating inputs, and data analysis.

The goal is to iterate through the array, compare each element with the target number, and report whether or not the number exists in the array. This exercise will help you understand how to work with arrays and implement a search algorithm in C++.

Group

Arrays and Vectors in C++

Objective

1. Define an array with several integer values.
2. Create a function that takes the array and the target number as input.
3. Loop through the array and compare each element with the target number.
4. If the target number is found, print its index.
5. If the target number is not found, print a message stating it is not in the array.
6. Test the program with different arrays and target numbers.

Implement a program that searches for a number in an array.

Example C++ Exercise

 Copy C++ Code
#include <iostream> // Include iostream for input and output
using namespace std; // Use the standard namespace

// Function to search for a number in an array
int searchNumber(int arr[], int size, int target) {
    // Loop through the array to find the target number
    for (int i = 0; i < size; i++) {
        if (arr[i] == target) { // If the number is found
            return i; // Return the index of the found number
        }
    }
    return -1; // Return -1 if the number is not found
}

int main() {
    // Define an array of numbers
    int numbers[] = {1, 3, 5, 7, 9, 11};
    int size = sizeof(numbers) / sizeof(numbers[0]); // Calculate the size of the array
    int target = 7; // Set the number to search for

    // Call the function to search for the target number
    int index = searchNumber(numbers, size, target);

    // Check if the number was found and print the result
    if (index != -1) {
        cout << "Number " << target << " found at index " << index << endl;
    } else {
        cout << "Number " << target << " not found in the array." << endl;
    }

    return 0; // End of program
}

 Output

Number 7 found at index 3

Share this C++ Exercise


More C++ Programming Exercises of Arrays and Vectors in C++

Explore our set of C++ Programming Exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C++. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C++.

  • Calculate the Average of Array Elements in C++

    In this exercise, you will create a function that calculates the average of the elements in an array. This is a common task when working with arrays, especially in data analysis, w...

  • Remove a Specific Element from an Array in C++

    In this exercise, you will develop a program that removes a specific element from an array. Removing elements from an array is a common task, especially when working with data that...

  • Find the Largest Number in an Array in C++

    In this exercise, you will implement a function that finds the largest number in an array. This task is useful when working with datasets where you need to extract the maximum valu...

  • Reverse an Array in C++

    In this exercise, you will implement a program that reverses an array. This task is useful in many scenarios such as reversing a list of elements, manipulating data structures, and...

  • Sum Two Arrays Element by Element in C++

    In this exercise, you will implement a function that sums two arrays element by element. This means that for two given arrays of the same size, the function will return a new array...

  • Remove Duplicates from an Array in C++

    In this exercise, you will create a program that removes duplicates from an array of numbers. The function will iterate over the array and remove any duplicate values, leaving only...