﻿ C++ : Check the first, last elements are equal in an array

# C++ Exercises: Check a given array of integers of length 1 or more and return true if the first element and the last element are equal in the given array

## C++ Basic Algorithm: Exercise-82 with Solution

Write a C++ program to check a given array of integers of length 1 or more. The program will return true if the first element and the last element are equal in the given array.

Sample Solution:

C++ Code :

``````#include <iostream>
using namespace std;

bool test(int nums[], int arr_length)
{
return arr_length > 0 && nums[0] == nums[arr_length - 1];
}

int main()
{
int arr_length;
int nums1[] = {10, 20, 40, 50 };
arr_length = sizeof(nums1) / sizeof(nums1[0]);
cout << test(nums1, arr_length) << endl;
int nums2[] = {10, 20, 40, 10};
arr_length = sizeof(nums2) / sizeof(nums2[0]);
cout << test(nums2, arr_length) << endl;
int nums4[] = {12, 24, 35, 55};
arr_length = sizeof(nums4) / sizeof(nums4[0]);
cout << test(nums4, arr_length) << endl;

return 0;
}
``````

Sample Output:

```0
1
0
```

Pictorial Presentation:

Flowchart:

C++ Code Editor:

What is the difficulty level of this exercise?

﻿

## C++ Programming: Tips of the Day

What is the "-->" operator in C/C++?

--> is not an operator. It is in fact two separate operators, -- and >.

The conditional's code decrements x, while returning x's original (not decremented) value, and then compares the original value with 0 using the > operator.

To better understand, the statement could be written as follows:

while( (x--) > 0 )

Ref : https://bit.ly/3kOTBby

We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook