Answer to Question #198814 in C++ for adil

Question #198814

Write a Menu Driven C++ program that creates one-dimensional array arr[] and initialize it with user of

size 15. The program should do following Tasks using Menu, The menu operations are implemented using

a) Write a function count(), that counts the occurrences of x (a number) in arr[].

b) Write a function partition (), that take the first element of the array x and put x in a position such

that all smaller elements (smaller than x) are before x, and put all greater elements (greater than

x) after x.

c) Write a function next_XOR(),the count of elements which are equal to the XOR of the next two

elements in an array.

d) Write a function duplicates(),which calculated the frequencies of all the elements and display

them.


1
Expert's answer
2021-05-30T13:35:21-0400
#include <iostream>
#include <string>


using namespace std;


void fillArray(int arr[]);
void displayArray(int arr[]);
int count(int arr[],int x);
void partition(int arr[]);
int next_XOR(int arr[]);
void duplicates(int arr[]);


//main method
int main(){
	//program creates one-dimensional array arr[] and initialize it with user of size 15. 
	int arr[15]={5,6,8,4,9,6,8,7,2,3,2,7,6,3,95};
	int ch=0;
	int x;
	while(ch!=6){
		cout<<"1. Fill array\n";
		cout<<"2. Count the occurrences of x in the array\n";
		cout<<"3. Partition\n";
		cout<<"4. Count of elements which are equal to the XOR of the next two elements\n";
		cout<<"5. Display duplicates\n";
		cout<<"6. Exit\n";
		cout<<"> ";
		cin>>ch;
		if(ch==1){
			fillArray(arr);
			displayArray(arr);
		}else if(ch==2){
			displayArray(arr);
			cout<<"Enter x: ";
			cin>>x;
			cout<<"\n"<<x<<" occurs "<<count(arr,x)<<" times\n\n";
		}else if(ch==3){
			displayArray(arr);
			partition(arr);
			cout<<"\nAfter partition\n";
			displayArray(arr);
		}else if(ch==4){
			cout<<"\n"<<next_XOR(arr)<<" elements which are equal to the XOR of the next two elements in an array.\n\n";
		}else if(ch==5){
			displayArray(arr);
			duplicates(arr);
		}else if(ch==6){
			//exit
		}else{
			cout<<"\nSelect correct menu item.\n\n";
		}
	}


	system("pause");
	return 0;
}
//Fill the array
void fillArray(int arr[]){
	for(int i=0;i<15;i++){
		cout<<"Enter the value "<<(i+1)<<": ";
		cin>>arr[i];
	}
}
//Displays array
void displayArray(int arr[]){
	cout<<"\nCurrent array: ";
	for(int i=0;i<15;i++){
		cout<<arr[i]<<" ";
	}
	cout<<"\n";
}


//a) Write a function count(), that counts the occurrences of x (a number) in arr[].
int count(int arr[],int x){
	int counter=0;
	for(int i=0;i<15;i++){
		if(arr[i]==x){
			counter++;
		}
	}
	return counter;
}
//b) Write a function partition (), that take the first element of the array x and put x in a position such
//that all smaller elements (smaller than x) are before x, and put all greater elements (greater than x) after x.
void partition(int arr[]){
	int tempValue;
	int x = arr[0];
	int leftIndex=1; 
	int rightIndex=14;
	while (leftIndex<rightIndex){
		while (arr[leftIndex]<=x){
			leftIndex++;
		}
		while (arr[rightIndex]>= x){
			rightIndex--;
		}
		if (leftIndex<rightIndex){
			tempValue = arr[leftIndex];
			arr[leftIndex] = arr[rightIndex];
			arr[rightIndex]= tempValue;


			leftIndex++; 
			rightIndex--;	
		}
	}
	tempValue = arr[leftIndex-1];
	arr[leftIndex-1]=x;
	arr[0]=tempValue;
}


//c) Write a function next_XOR(),the count of elements which are equal to the XOR of the next two elements in an array.
int next_XOR(int arr[]){
	int counter=0;
	for(int i=0; i<15; i++){
		int x=arr[(i+1)%15];
		int y=arr[(i+2)%15];
		if (arr[i]==(x^y)){
			cout <<"Match found at index " << i << ", value is " << arr[i] << endl;
			counter++;
		}
	}
	return counter;
}
//d) Write a function duplicates(),which calculated the frequencies of all the elements and display them.
void duplicates(int arr[]){
	int frequencies[100];
	int counter;
	for(int i=0; i<15; i++)
	{
		counter = 1;
		for(int j=i+1; j<15; j++)
		{
			// If duplicate element is found
			if(arr[i]==arr[j]){
				counter++;
				frequencies[j] = 0;
			}
		}
		if(frequencies[i] != 0)
		{
			frequencies[i] = counter;
		}
	}
	cout<<"\nFrequency of all elements of array : \n";
	for(int i=0; i<15; i++)
	{
		if(frequencies[i] != 0)
		{
			cout<<arr[i]<<" occurs "<<frequencies[i]<<" times\n";
		}
	}
}




Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog