Answer to Question #198816 in C++ for adil

Question #198816

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 circular(),which replace every element of the array by the sum of next two

consecutive elements in a circular manner i.e. arr[0] = arr[1] + arr[2], arr[1] = arr[2] + arr[3],

... arr[n – 1] = arr[0] + arr[1].

b) Write a function Search(), takes an array and element to search in the array and returns the index

of element if the element is found. And return the negative number if not found.

c) Write a function shift_circular (), which shifts an array circularly left by two positions. Thus, if p[0]

= 15, p[1]= 30, p[2] = 28, p[3]= 19 and p[4] = 61 then after the shift p[0] = 28, p[1] = 19, p[2] = 61,

p[3] = 15 and p[4] = 30.


1
Expert's answer
2021-05-31T00:50:55-0400
#include<iostream>
#include<bits/stdc++.h>
using namespace std;


void circular(int arr[],int n)
{
	cout<<"Circular array sum"<<endl;
	int temp = arr[0] + arr[1];
	int temp1 = arr[n-1]+arr[0];
	for(int i=0; i<n-2; i++)
	{
	arr[i] = arr[(i%n)+1]+ arr[(i%n)+2];
	cout<<arr[i]<<" ";
	}
	arr[n-2] = temp1;
	arr[n-1] = temp;
	cout<<temp1<<" "<<temp;
}


int search(int arr[], int x,int n)
{
	bool ans = false;
	for(int i=0; i<n; i++)
	{
		if(arr[i] == x)
		{
			ans = true;
			return i;
			break;
		}
	}
	if(ans == false)
	{
		return -1;
	}
}


void shift_array(int arr[], int n)
{
	int temp1 = arr[0];
	int temp = arr[1];
	for(int i=0; i<n-1; i++)
	{
		arr[i] = arr[i+2];
	}
	arr[n-2] = temp1;
	arr[n-1] = temp;
	
	cout<<endl<<"Rotated Array "<<endl;
	for(int i=0; i<n; i++)
	{
		cout<<arr[i]<<" ";
	}
	
}


int main()
{
	cout<<"1.Circular Sum Array"<<endl<<"2.Search in Array"<<endl<<"3.Shift Array"<<endl<<endl;
	cout<<"Choose any option from above menu : ";
	int op;
	cin>>op;
	int n;
	cout<<"Enter size of array : ";
	cin>>n;
	int arr[n];
	cout<<"Enter array elements"<<endl;
	for(int i=0; i<n; i++)
	{
		cin>>arr[i];
	}
	cout<<endl;
	switch(op)
	{
		case 1:
			{
				circular(arr,n);
				break;
			}
		
		case 2:
			{
				cout<<"Enter element you want to search in array : ";
				int x;
				cin>>x;
				int num = search(arr, x,n);
				if(num == -1)
				{
					cout<<"Element not found"<<endl;
				}
				else
				{
					cout<<"Element Found at index "<<num<<endl;
				}
				break;
			}
			
		case 3:
			{
				shift_array(arr, n);
				break;
			}
	}
}








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