Okay I need help on this number problem. What I'm supposed to to is put these numbers into an array, enter the array a index 0. Then what ever integer is in that index + the integer itself is the next index that you go to. You do this until the integer that you land on is zero.
Any suggestions would be helpful.
Here is my code so far.
CODE
#include <iostream>
#include <cctype>
#include <string>
#include <sstream>
#include <fstream>
using namespace std;
const char Number_Puzzle = 'A';
const char Number_Mirror = 'B';
const char GCD_Finder = 'C';
const char Quit = 'Q';
const char FILLCHAR = '*';
const int MAXSIZE = 101;
char PickYourPuzzle();
void NumberPuzzle(ifstream& fin, const string filename);
void FillPuzzleArrays(ifstream& fin, int arry[], int index, int& size);
bool Solvable(int position, int squares[], int nSquares);
int NumPuzzleReturn(int i, int puzzle[], int index, int& size, int previndex);
void NumberMirror();
int WriteNumBackwards(string snum, int i);
void GCDFinder();
int CalculateGCD(int a, int b);
void main()
{
char choice;
ifstream fin;
string filename;
int puzzle[MAXSIZE];
int index = 0;
int size;
int i = 0;
int previndex = 0;
do
{
choice = PickYourPuzzle();
switch (choice)
{
case Number_Puzzle: NumberPuzzle(fin, "puzzle.txt");
FillPuzzleArrays(fin, puzzle, index, size);
NumPuzzleReturn(i, puzzle, index, size, previndex);
break;
case Number_Mirror: NumberMirror();
break;
case GCD_Finder: GCDFinder();
break;
case Quit: cout<< "Goodbye my friend" << endl << endl;
break;
}
}while(choice != Quit);
}
char PickYourPuzzle()
{
char choice;
cout<< "Hello welcome to the Puzzle Center." << endl
<< "What puzzle would you like to try your hand at." << endl << endl;
cout<< "A) Number Puzzle Solver: Solves a number puzzle. " << endl
<< "B) Number Mirror: Displays a number 'backwards'. " << endl
<< "C) GCD Finder: This finds the greatest vommon divisor of two numbers. " << endl
<< "Q) Quit the program. " << endl << endl;
cout<< "Your Choice? ";
cin >> choice;
choice = toupper(choice);
if( choice == Number_Puzzle || choice == Number_Mirror || choice == GCD_Finder)
{
cout<< endl << "Thank You for choosing " << choice << ". One Moment Please." << endl << endl;
}
else if(choice == Quit)
{
cout<< endl;
}
else
{
cerr<< "That is an INVAILD CHOICE! Please choose again." << endl << endl;
}
return(choice);
}
void NumberMirror()
{
string snum;
int number;
int i;
cout<< "Welcome to the Number Mirror Puzzle. " << endl;
cout<< "Please enter a non-negative integer to be mirrored! ";
cin >> snum;
istringstream iss(snum);
iss >> number;
while(number < 0)
{
cout<< "That is not a non-negaitve integer! Try again." << endl;
cin >> snum;
istringstream iss(snum);
iss >> number;
}
i = snum.length();
cout<< "The mirror of " << snum << " is";
WriteNumBackwards(snum, i);
cout<< endl << endl;
}
int WriteNumBackwards(string snum, int i)
{
if (i < 0)
{
return(0);
}
else
{
cout<< snum[i];
return(WriteNumBackwards(snum, i-1));
}
}
void GCDFinder()
{
int number1;
int number2;
int big, little;
int gcd;
cout<< "Welcome to the Greatest Common Divisor Finder." << endl
<< "Please enter the first number: ";
cin >> number1;
cout<< "Please enter the second number: ";
cin >> number2;
if (number1 > number2)
{
big = number1;
little = number2;
}
if (number1 < number2)
{
little = number1;
big = number2;
}
gcd = CalculateGCD(big, little);
cout<< "The greatest common denominator of " << big
<< " and " << little << " is " << gcd << "." << endl << endl;
}
int CalculateGCD(int a, int b)
{
int r;
r = a % b;
if( r == 0 )
{
return(b);
}
else
{
return(CalculateGCD(b, r));
}
}
void NumberPuzzle(ifstream& fin, const string filename)
{
fin.open(filename.c_str());
}
void FillPuzzleArrays(ifstream& fin, int arry[], int index, int& size)
{
string puzsize;
string strnum;
int num;
getline(fin, puzsize, ' ');
istringstream iss(puzsize);
iss >> size;
getline(fin, strnum, ' ');
do
{
istringstream iss(strnum);
iss >> num;
arry[index] = num;
index++;
getline(fin, strnum, ' ');
}while(index < size);
cout<< "Solving puzzle 1. Puzzle size = " << size << endl;
system("pause");
}
int NumPuzzleReturn(int i, int puzzle[], int index, int& size, int previndex)
{
int newindex;
bool flag2;
int j = 0;
j = puzzle[previndex];
i = puzzle[index];
if(i == 0)
{
return(0);
}
else
{
flag2 = Solvable(index, puzzle, size);
if(flag2 == true)
{
cout<< "Entering Solvalble() at index " << index << "(" << i << ")" << endl;
previndex = index;
newindex = (index+i);
puzzle[index] = FILLCHAR;
return(NumPuzzleReturn(i, puzzle, newindex, size, previndex));
}
if(flag2 == false)
{
cout<< "Index " << index << " is not in the array. Returning false." << endl;
//i = ((i - puzzle[index])+ index);
index = previndex;
newindex = (index-i);
puzzle[index] = FILLCHAR;
return(NumPuzzleReturn(i, puzzle, index, size, previndex));
}
}
}
bool Solvable(int position, int squares[], int nSquares)
{
bool flag;
if((position < 0) || (position > nSquares) || (squares[position] == FILLCHAR))
{
flag = false;
}
else
{
flag = true;
}
return(flag);
}