In: Computer Science
Write a program that will search through an array and check to see if the first number in the array occurs more than once. If the first number occurs more than once, return true. Otherwise, return false. If the array is empty, return false?
You will need to use a variable, loop, and if statement.
In c++ it can be written as:-
#include<iostream>
using namespace std;
main(){
cout<<"Please enter the array elements\n";
//prompting the user to enter the array elements
int a[5]; //declaring array of size 5
for(int i=0;i<5;i++)
{
cin>>a[i]; //storing the
entered values in the array
}
int firstnum,count=0; // variables for storing first
element and count of first element, respectively.
firstnum=a[0]; //assigning the first element
for(int i=0;i<5;i++)
{
if(firstnum==a[i])
count=count+1; //increasing the
value of count if the variable is equal to the element in the
array
}
if(count>=2) //if the count is greater than 2 then
print true else false
cout<<"TRUE";
else
cout<<"FALSE";
}
The Screenshot of code and output are given below:-
Output when the first element
is non repeating:-
Output when the first element is repeating:-