Wednesday, May 5, 2010

cs201 Ass Sol

 
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

Solution:-

#include
using namespace std;
/*Function*/
int count_repeats(int array2[],int compare_element,int j){
int counter = 0,i;
for(i=j;i<10;i++)
{
if(array2[i]==compare_element)
counter++;
j--;
}
return counter;
}
/*Main Method*/
int main(){
int array1[10],i,j,no_of_repeats;
cout<<"Enter the length of the array you want to campre ... ";/*By assingnment set length to 10 time*/
cin>>j;
for(i=0;i="" 1){="" :="" \n";>array1[i];="" cout<<"element="" cout<<"enter="" elements="" for(i="0;i<10;i++){" if(no_of_repeats="" no_of_repeats="count_repeats(array1,array1[i],i);" repeats:"<}
}
return 0;
system("pause");
}
 

NOTE: - These Assignments OR Quiz are just for idea, so kindly don't copy it, after viewing it make your own. Thanks We always try our best to upload 100% correct solution BUT it is requested that you kindly review it before submission, please BEST OF LUCK, Thanks to those students those send me Assignments and quizzes. If you have any Assignment and quiz kindly send at jamilbookcenter@yahoo.com

No comments:

Post a Comment