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

I need help fixing my C++ function to take all the conditions into consideration

ID: 3755607 • Letter: I

Question

I need help fixing my C++ function to take all the conditions into consideration for my program. My program's responibility is to finish the implementation of an iterative solution to the Towers of Hanoi puzzle. Specifically, I need to implement the puzzle initialization and the move operation utilizing the stacks provided. I can't rename existing functions, nor add any additional functions nor member variables! The program will read in a text file with containing a single integer: the number of disks for the puzzle. 2. The program will output the steps required to solve the puzzle for the specified number of disks. 3. The program will utilize the Hanoi class. 4. The program will utilize the std::stack library to complete the move functionality. 5. The only changes will be to the Hanoi.cpp file and will only be within the constructor and the MakeMove function

main: (can't change anything in the main)

#include <iostream>

#include <fstream>

#include <sstream>

#include <string>

#include "hanoi.h"

int main(int argc, char** argv)

{

if (argc != 3)

{

std::cout << "Usage: assignment04 [input file] [output file]" << std::endl;

return 0;

}

std::ifstream fin(argv[1]);

std::ofstream fout(argv[2]);

unsigned int n = 0;

if (!(fin >> n))

{

std::cerr << "Invalid input file!" << std::endl;

}

else

{

Hanoi problem(n);

problem.Solve(fout);

}

return 0;

}

hanoi.cpp (Only thing that needs tweaking or adding)

hanoi.h

#ifndef __HANOI_H__

#define __HANOI_H__

#include <array>

#include <algorithm>

#include <cmath>

#include <iostream>

#include <stack>

class Hanoi

{

private:

unsigned int m_Disks;

std::array<std::stack<unsigned int>, 3> m_Pegs;

void PrintMove(std::ostream& os, unsigned int disk, unsigned int from, unsigned int to)

{

os << "Moving Disk " << disk << " from peg " << from << " to peg " << to << std::endl;

}

void MakeMove(std::ostream& os, unsigned int from, unsigned int to);

public:

Hanoi(unsigned int n);

void Solve(std::ostream& os)

{

unsigned int number_of_moves = std::pow(2, m_Disks) - 1;

unsigned int src = 0;

unsigned int aux = 1;

unsigned int dst = 2;

// if the number of disks are even, swap the aux and dest pegs

if (m_Disks % 2 == 0)

{

std::swap(aux, dst);

}

for (int i = 1; i <= number_of_moves; ++i)

{

if (i % 3 == 1)

{

MakeMove(os, src, dst);

}

else if (i % 3 == 2)

{

MakeMove(os, src, aux);

}

else

{

MakeMove(os, aux, dst);

}

}

}

};

#endif // __HANOI_H__

Explanation / Answer

#include <stdio.h>

  

// C recursive function to solve tower of hanoi puzzle

void towerOfHanoi(int n, char from_rod, char to_rod, char aux_rod)

{

    if (n == 1)

    {

        printf(" Move disk 1 from rod %c to rod %c", from_rod, to_rod);

        return;

    }

    towerOfHanoi(n-1, from_rod, aux_rod, to_rod);

    printf(" Move disk %d from rod %c to rod %c", n, from_rod, to_rod);

    towerOfHanoi(n-1, aux_rod, to_rod, from_rod);

}

  

int main()

{

    int n = 4; // Number of disks

    towerOfHanoi(n, 'A', 'C', 'B'); // A, B and C are names of rods

    return 0;

}

#include <stdio.h>

  

// C recursive function to solve tower of hanoi puzzle

void towerOfHanoi(int n, char from_rod, char to_rod, char aux_rod)

{

    if (n == 1)

    {

        printf(" Move disk 1 from rod %c to rod %c", from_rod, to_rod);

        return;

    }

    towerOfHanoi(n-1, from_rod, aux_rod, to_rod);

    printf(" Move disk %d from rod %c to rod %c", n, from_rod, to_rod);

    towerOfHanoi(n-1, aux_rod, to_rod, from_rod);

}

  

int main()

{

    int n = 4; // Number of disks

    towerOfHanoi(n, 'A', 'C', 'B'); // A, B and C are names of rods

    return 0;

}

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