w3resource

C++ Exercises: Check a given array of integers of length 1 or more and return true if 10 appears as either first or last element in the given array


Check 10 in First or Last Element

Write a C++ program to check a given array of integers of length 1 or more and return true if 10 appears as either first or last element in the given array.

Sample Solution:

C++ Code :

#include <iostream> // Including the input/output stream library
using namespace std; // Using the standard namespace

// Function that checks if the first or last element of an integer array is 10
bool test(int nums[], int arr_length)
{
    return nums[0] == 10 || nums[arr_length - 1] == 10; // Return true if the first or last element of nums is 10
}

// Main function
int main() 
{
    int arr_length; // Declare variable to store array length

    int nums1[] = {10, 20, 40, 50 }; // Define an array nums1
    arr_length = sizeof(nums1) / sizeof(nums1[0]); // Calculate the length of nums1
    cout << test(nums1, arr_length) << endl; // Output result of test function for nums1

    int nums2[] = {5, 20, 40, 10}; // Define an array nums2
    arr_length = sizeof(nums2) / sizeof(nums2[0]); // Calculate the length of nums2
    cout << test(nums2, arr_length) << endl; // Output result of test function for nums2

    int nums3[] = {10, 20, 40, 10}; // Define an array nums3
    arr_length = sizeof(nums3) / sizeof(nums3[0]); // Calculate the length of nums3
    cout << test(nums3, arr_length) << endl; // Output result of test function for nums3

    int nums4[] = {12, 24, 35, 55}; // Define an array nums4
    arr_length = sizeof(nums4) / sizeof(nums4[0]); // Calculate the length of nums4
    cout << test(nums4, arr_length) << endl; // Output result of test function for nums4

    return 0; // Return statement indicating successful termination of the program
}

Sample Output:

1
1
1
0

Visual Presentation:

C++ Basic Algorithm Exercises: Check a given array of integers of length 1 or more and return true if 10 appears as either first or last element in the given array.

Flowchart:

Flowchart: Check a given array of integers of length 1 or more and return true if 10 appears as either first or last element in the given array.

For more Practice: Solve these Related Problems:

  • Write a C++ program to check if the first or the last element of an integer array equals 10, and print a boolean result.
  • Write a C++ program that reads an array of integers and returns true if 10 appears at either the beginning or the end of the array.
  • Write a C++ program to determine if the number 10 is located at the first or last index of a given array and outputs 1 for true.
  • Write a C++ program that checks an array’s first and last elements for the value 10 and prints true if found in either position.

Go to:


PREV : Remove First/First Two 'a's if Present.
NEXT : First and Last Elements Match Check.

C++ Code Editor:



Contribute your code and comments through Disqus.

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.