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

A. Implement the code below, document, test and submit a full report.C++ B. Desc

ID: 3888640 • Letter: A

Question

A. Implement the code below, document, test and submit a full report.C++

B. Describe how you could implement this using an array instead of the node class.C++

C .Write a program to store a tree as an array. In your test program, be sure to print the array values, and to print the values in tree­­­ “format”.Implement (Write a function to add an element to an existing tree.)C++

//Josephus Problem
#include <iostream>
#include <cstdlib>

using namespace std;

class node
{
public:

int item; node* next;
    node(int x, node* t)
      { item = x; next = t; }
};

typedef node *link;

int main(int argc, char *argv[])
{ int i, N = atoi(argv[1]), M = atoi(argv[2]);
    link t = new node(1, 0); t->next = t;
    link x = t;
    for (i = 2; i <= N; i++)
      x = (x->next = new node(i, t));
    while (x != x->next)
      {
        for (i = 1; i < M; i++) x = x->next;
        x->next = x->next->next;
      }
    cout << x->item << endl;
}

Explanation / Answer

#include <iostream>
#include <cstdlib>

using namespace std;

class node
{
public:

int item; node* next;
    node(int x, node* t)
      { item = x; next = t; }
};

typedef node *link;

int main(int argc, char *argv[])
{ int i, N = atoi(argv[1]), M = atoi(argv[2]);
    link t = new node(1, 0); t->next = t;
    link x = t;
    for (i = 2; i <= N; i++)
      x = (x->next = new node(i, t));
    while (x != x->next)
      {
        for (i = 1; i < M; i++) x = x->next;
        x->next = x->next->next;
      }
    cout << x->item << endl;
}

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