Answer to Question #325482 in C++ for Anji

Question #325482

Harrypotter is visiting his uncle’s house ,the house of Black ,and ants to know more about the family through their ancestral tree.He notices that the ancestraltreebegins with the head of the family at the top,having 2 children as his descendants,This pattern is followed throught and each member is represented by unique integer.

Given relationships in the form ofan integer arraywhere the head of the family is at the first position (i=0) and his children areat position (2*i+1) and (2*i+2).

Your task is to help harry find and return all the siblings of any given family member and return them in the form of a sorted array.

Note:if there are no siblings ,return {-1}.

Example:

Input1:5

Input2:{1,2,3,4,5}

Input3:1

Output:{-1}


1
Expert's answer
2022-04-12T13:28:15-0400
#include<iostream>

using namespace std;

int main()
{
	int familyMem;
	cout << "Please, enter a count of family members: ";
	cin>> familyMem;
	if (familyMem == 1)
	{
		cout << -1;
	}
	else
	{
		int* arr = new int[familyMem];
		int i = 0;
		int j = 0;
		
		arr[0] = 1;
		while (j < familyMem)
		{
			arr[2 * i + 1] = j+2;
			j++;
			if (j == familyMem)break;
			arr[2 * i + 2] = j+2;
			j++;
			i++;
		}
		cout << "The the ancestral tree:\n";
		for (int i = 0; i < familyMem; i++)
		{
			if (i == 0)
			{
				cout << "\t" << arr[i] << endl;
			}
			else if (i > 0 && i < 3)
			{
				cout<< arr[i] << "\t";
				if (i == 2)cout << endl;
			}
			else if (i >= 3 && i < 7)
			{
				cout <<arr[i] << "\t";
				if (i == 6)cout << endl;
			}
			else if (i >= 7 && i < 15)
			{
				cout << arr[i] << "\t";
				if (i == 14)cout << endl;
			}
		}
	}
	
	
}

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
APPROVED BY CLIENTS