Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Problem Statement: Finding the repeated elements in an array You are required to

ID: 3619446 • Letter: P

Question


Problem Statement: Finding the repeated elements in an array
You are required to write a program that takes 10 integer values in an array as input from user. After getting input, the program should find the elements with repetition and number of times each element is repeated.


Detailed Description:

1. Declare an integer type array.
2. Take input from user in the array i.e. 10 integer values.
3. Pass this array to a function which will calculate and display the number of times an element is repeated.
4. If the array contains elements with repetition, the program should only display the repeated elements and their count.
5. If there is no repetition in the array, the program should simply display the message “There is no repetition in the array”.

Sample Output 1
Enter the value of array element 1 : 8
Enter the value of array element 2 : 6
Enter the value of array element 3 : 2
Enter the value of array element 4 : 4
Enter the value of array element 5 : 2
Enter the value of array element 6 : 2
Enter the value of array element 7 : 7
Enter the value of array element 8 : 8
Enter the value of array element 9 : 5
Enter the value of array element 10 : 6


2 is repeated 3 times
6 is repeated 2 times
8 is repeated 2 times


Sample Output 2
Enter the value of array element 1 : 1
Enter the value of array element 2 : 3
Enter the value of array element 3 : 8
Enter the value of array element 4 : 7
Enter the value of array element 5 : 4
Enter the value of array element 6 : 9
Enter the value of array element 7 : 6
Enter the value of array element 8 : 5
Enter the value of array element 9 : 0
Enter the value of array element 10 : 2


There is no repetition in the array

Explanation / Answer

 #include <iostream.h>
#include <conio.h>

void count(int []); //Delaration of count function to count the repeated values




main()
{
int array[10]; //Declaring an array of 10 integers

for(int i=0; i<10; i++) //In this loop, taking input from user into the array
{
cout << "Enter the value of array element " << i+1 << " : ";
cin >> array[i];
}

count(array); // Passing array to the function count

getch();
}





void count(int arr[]) //Definition of count function

{

//counter variable will contain the number of repetitions of a value and k will refer to the index of an array
int counter=0, k=0, repetition = 0;

//in this nested for loop, the array is sorted in ascending order using bubble sort algorithm
for(int i=0; i<10; i++)
{
for(int j=0; j<10; j++)
{
if(arr[j] > arr[j+1])
{
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}


for(int i=k; i<=9; i++) //This loop will start from index 0 of array and run 10 times
{
for(int j=k; j<=9; j++)
{

//if match is found on index i and j, the counter is incremented and value of k is same as current index
if(arr[i] == arr[j])
{
counter++;
k = j;
}
else
{
k = j; //value of k remains the same as inner loop current index

/*if the value is not matched with the current index on j, then i is assigned one
less than the current index on j so that it should not compare already compared values.
It means the outer loop will start from the values which are not compared yet.*/
i = k - 1;

/* break to the outer loop. It means that array is already sorted and if no match is found in
the current iteration it means there is no such value in the rest of the array*/
break;
}
} // ending inner for loop



if(counter >= 2) // counter greater than or equal to 2 means that a number is repeated atleast two times
{
cout << endl<<arr[i]<<" is repeated "<<counter <<" times " <<endl; //Here displaying the repeated number
repetition++; //This variable indicates that there is atleast on repetition in the array
}

counter = 0; //setting counter as 0 again so that it may contain the number of repetitions for next values in the array

} //ending outer for loop


if(repetition == 0) //if the value of repetition is not incremented it means there was no repetition found
cout << " There is no repetition in the array";

} //ending function definition
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote