Help with C++ output

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Moderators General, Prelates

TommyH
Posts: 5
Joined: Wed Feb 24, 2010 3:07 pm UTC

Help with C++ output

A friend and I are working on a C++ program. We are trying to make a graphical output for the classic Towers of Hanoi problem. We have the recursive solution figured out, but we can't get the output to look like we want. The output is three columns of numbers, the largest number representing the largest disk. Our current output ends up looking like this (at any random point for 5 disks)

0 1 0
0 2 0
0 0 3
4 0 0
5 0 0

We don't want the 0s to appear. Preferably the numbers would just drop so that in the example above, the 5, 2, and 3 would all be on the same line. We really do not know how to do this.

Code: Select all

`#include <conio.h>#include <iomanip>#include <stdio.h>#include <iostream>//Protovoid randomFunction();int runner(int n, char begin, char inter, char end);void rerun();int init(int num);//Varsbool runAgain = true;int A;int B;int C;int n2 = 0;//Namespaceusing namespace std;void main(){      do   {      randomFunction();      rerun();   }   while (runAgain == true);}void randomFunction(){   //var   int n = 0;   cout << "How many disks do you want to use?(64 disks or less) ";   cin >> n;   n2 = n;   init(n);   runner(n, 'A', 'B', 'C');   getch();}int init(int n){   for (int i = n; i > 0; i--)   {      A[i] += i;   }   for (int i = n; i > 0; i--)   {      B[i] = 0;   }   for (int i = n; i > 0; i--)   {      C[i] = 0;   }   return 0;}int runner(int n, char X, char Y, char Z){   if (n != 0)   {      runner((n-1), X, Z, Y);      cout << "Move disk from " << X << " to " << Z << endl;      if ((X == 'A') && (Z == 'B'))      {         for (int i = n; i >= 0; i--)         {            if (A[n] == i)            {               A[i] = 0;               B[i] = i;            }         }      }      if ((X == 'A') && (Z == 'C'))      {         for (int i = n; i >= 0; i--)         {            if (A[n] == i)            {               A[i] = 0;               C[i] = i;            }         }      }      if ((X == 'B') && (Z == 'A'))      {         for (int i = n; i >= 0; i--)         {            if (B[n] == i)            {               B[i] = 0;               A[i] = i;            }         }      }      if ((X == 'B') && (Z == 'C'))      {         for (int i = n; i >= 0; i--)         {            if (B[n] == i)            {               B[i] = 0;               C[i] = i;            }         }      }      if ((X == 'C') && (Z == 'A'))      {         for (int i = n; i >= 0; i--)         {            if (C[n] == i)            {               C[i] = 0;               A[i] = i;            }         }      }      if ((X == 'C') && (Z == 'B'))      {         for (int i = n; i >= 0; i--)         {            if (C[n] == i)            {               C[i] = 0;               B[i] = i;            }         }      }      for (int j = 1; j <= n2; j++)      {         cout << A[j] << " " << B[j] << " " << C[j] << endl;      }      runner((n-1), Y, X, Z);   }   return 0;}void rerun(){   char runAns = ' ';   cout << "Would you like to run the problem again? (y or n)" << endl;   cin >> runAns ;   if ((runAns == 'y') || (runAns == 'Y'))      runAgain = true;   else      runAgain = false;}`

jaap
Posts: 2094
Joined: Fri Jul 06, 2007 7:06 am UTC
Contact:

Re: Help with C++ output

So if a disc variable equals zero you want to print a space, else you want it to print the value?

TommyH
Posts: 5
Joined: Wed Feb 24, 2010 3:07 pm UTC

Re: Help with C++ output

Well, if we print out a space, the disks wont drop down still. We thought about doing that. Considering the example we used, we want it to output basically this

4 1
5 2 3

P.S. if you use more than 10 disks the columns get thrown off, we haven't included an adjust function yet

lulzfish
Posts: 1214
Joined: Tue Dec 16, 2008 8:17 am UTC

Re: Help with C++ output

You can't make the output "drop" with normal output.
The way normal output works is that you print one line at a time, and you can't go back to change previous lines.
So you'll have to do some formatting.

TommyH
Posts: 5
Joined: Wed Feb 24, 2010 3:07 pm UTC

Re: Help with C++ output

Yeah, we know we cant just do it with regular output.
We tried we tried using a sort function, but for some reason it moved everything to the top instead of the bottom, and changed the numbers for each column. So we really messed that up.

jaap
Posts: 2094
Joined: Fri Jul 06, 2007 7:06 am UTC
Contact:

Re: Help with C++ output

TommyH wrote:Well, if we print out a space, the disks wont drop down still. We thought about doing that. Considering the example we used, we want it to output basically this

4 1
5 2 3

P.S. if you use more than 10 disks the columns get thrown off, we haven't included an adjust function yet

Ah, now it's clear what you want.
The data structure you have chosen does make printing like that a bit tricky.
You could write a function that, given column/peg number c and height k, finds the disc in that column at that height. In other words it finds the kth disc from the bottom in that column. It should return 0 if there are no discs that high up.
Once you have that function, it should become relatively easy to draw the board from top to bottom.

joeframbach
Posts: 1478
Joined: Sun Nov 05, 2006 12:49 am UTC

Re: Help with C++ output

Oh curses, this is a difficult problem.

lulzfish
Posts: 1214
Joined: Tue Dec 16, 2008 8:17 am UTC

Re: Help with C++ output

joeframbach wrote:Oh curses, this is a difficult problem.

Considering how this program is written, and what it's supposed to do, I think involving libraries would only confuse them harder.

Not_an_S
Posts: 25
Joined: Sun Feb 14, 2010 1:16 am UTC

Re: Help with C++ output

I'd reccomend using a multidimensional array instead of 3 separate arrays, but that's just preference (also, it would fix the problem with having more than 10)
If you use them, here's how I would fix the problem them:

Code: Select all

`int playarea[width][height];int fornum;for(i=0;i>width;i++){for(fornum=0;fornum>height;fornum++){if(playarea[i][fornum]!=0){cout << playarea[i][fornum]}}cout << endl;}`

I haven't tested it, but I think it should work, provided you replace all instances of A, B, and C with the respective heights on the array.

TommyH
Posts: 5
Joined: Wed Feb 24, 2010 3:07 pm UTC

Re: Help with C++ output

lulzfish wrote:
joeframbach wrote:Oh curses, this is a difficult problem.

Considering how this program is written, and what it's supposed to do, I think involving libraries would only confuse them harder.

Now you guys confused me haha.

And Not_an_S, I like the idea of using multi-dimensional array (but your code doesnt work).

TommyH
Posts: 5
Joined: Wed Feb 24, 2010 3:07 pm UTC

Re: Help with C++ output

Alright nevermind guys.

Thanks for all the ideas but we got it figured out.

Not_an_S
Posts: 25
Joined: Sun Feb 14, 2010 1:16 am UTC

Re: Help with C++ output

TommyH wrote:And Not_an_S, I like the idea of using multi-dimensional array (but your code doesnt work).

I figured that out a while later, and I found out that doing this without rewriting most of the program would require two arrays, one with the normal lines, and one with the fixed ones for output, which would require more code for something that was already fixed.
Or at least, that's how I would do it.

EDIT: I'm actually going to try to make the program myself. I need practice anyway.