Question

In: Computer Science

Write a program that will search through an array and check to see if the first...

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.

Solutions

Expert Solution

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:-


Related Solutions

Create this on Python Write a program that asks for three numbers. Check first to see...
Create this on Python Write a program that asks for three numbers. Check first to see that all numbers are different. If they’re not different, then exit the program. Otherwise, display the largest number of the three. Outputs: - Enter the first number: 4 - Enter the second number: 78 - Enter the third number: 8 (The largest number is 78.) Tasks - Complete the algorithm manually without using any built-in functions to find the largest number on list. -...
Write a program that allows the user to search the array to find the number entered
Write a program that allows the user to search the array to find the number entered
// This program demonstrates a Binary Search, which search for a value // in an array,...
// This program demonstrates a Binary Search, which search for a value // in an array, assuming that the array is sorted in descending order. // You have to modify the function binarySearch() to search for a value // in an array that is sorted in ascending order. // NOTES: // Uncomment line 34 and comment line 32. You don't have to edit anything // else in the main(), just in the binarySearch() function. // EXAMPLES (using the array sorted...
write a python program that will search through a range of launch angles (this will require...
write a python program that will search through a range of launch angles (this will require a loop statement) the target is 500 meters away and the projectile was launched at 100m/s, the initial height of the projectile can be between 2 and 30 meters the print statement will state the initial launch angle required to hit the target from the projectile height. ***explain each step or a rating will not be given***
1. Write a program that includes a function search() that finds the index of the first...
1. Write a program that includes a function search() that finds the index of the first element of an input array that contains the value specified. n is the size of the array. If no element of the array contains the value, then the function should return -1. Name your program .C The program takes an int array, the number of elements in the array, and the value that it searches for. The main function takes input, calls the search()function,...
Binary Search. Write a MIPS assembly program to perform a binary search on A[10], which is an array of 10 positive integers.
Binary Search. Write a MIPS assembly program to perform a binary search on A[10], which is an array of 10 positive integers. Your program should have a main routine that does the following:(a) Prompt the user to enter all the 10 integers in the array.(b) Prompt the user to enter the number to be searched.(c) Reads the integer values and makes sure it is a positive integer.(d) Prints the index of the integer. If the input is not available in...
Q1. Write a Java program to do sequential search to find element 55 in array 10,20,35,45,55,65,75,85....
Q1. Write a Java program to do sequential search to find element 55 in array 10,20,35,45,55,65,75,85.                                                                                                                                                                    Q2.Write a Java program to find element 54 in array 45,41,65,53,76,90 using Binary Search. (Hint: Binary search is applied to sorted array elements) Q4.   Write a java program to create array list subject        - add English, Maths, Science to the list        - add Computers at index 2        - display first occurrence index of Maths        - traverse the list using...
Phython: Given a string x, write a program to check if its first character is the...
Phython: Given a string x, write a program to check if its first character is the same as its last character. If yes, the code should output "True"
using python3 Write a Python program that lets a user search through the 'data.txt' file for...
using python3 Write a Python program that lets a user search through the 'data.txt' file for a given first name, last name, or email address, and print all the matches/results. Prompt the user for which field to search, (f) for first name, (l) for last name, or (e) for email data.txt entries are all formatted as: first_name, last_name, email, separated by TABS Your program should not read the entire file into memory, it should read line by line, and only...
Using python: Given a string x, write a program to check if its first character is...
Using python: Given a string x, write a program to check if its first character is the same as its last character. If yes, the code should output "True"
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT