Click to See Complete Forum and Search --> : this is supposed to be a simple program


flynnwallace
02-22-2001, 08:28 PM
I need to write a program that read integers from the user and will then write those numbers backwards.

Ex. You entered numbers 23 45 3445 56
The numbers backwards are 56 3445 45 23

I wanted to use arrays but was told not to...this is what I have so far


#include <iostream.h>

{void main()
{

const int SENTINEL = -1;
int number;

cout << "Enter numbers one at a time as requested." << endl;
cout << "When done, enter " << SENTINEL << " to stop." << endl;
cout << "Enter the first score: ";
cin >> number;

while (number != SENTINEL)
{
cout<< "Enter the next numbers." <<endl;
cin >> number;
}
//now what????

}

I cant for the life of me figure out what the algorithem should be to write the entered numbers in reverse. Is there a function that does this? Does anyone have any ideas.

Flynn

f'lar
02-22-2001, 09:47 PM
You need a stack, but I don't know how to do that aside from arrays, because somehow I get the impression that you haven't gotten to dynamic memory or the STL yet.

f'lar
02-22-2001, 10:00 PM
figured it out: recursion.
something like this:

#include <iostream.h>
int test=0;

void recursivestack()
{
int num2;
cin >> num2;
if (num2!=test)
{
recursivestack();
cout << num2;
}
}

void main()
{
int num;
cout << "Numbers?"
cin >> num;
if (num != test)
{
recursivestack();
cout << num;
}
}

that should do it, but I haven't debugged it at all, so check it very thouroghly. It uses the operating system's call stack.

[ 22 February 2001: Message edited by: f'lar ]

flynnwallace
02-23-2001, 01:29 AM
Ok the stack part might be ok but I dont think it is what my professor wants. This program is basicly for beginning C++ programmers and while I could use a call to stack, I think it might be beyond them for now. I think I might use an array but I dont know where to start. I havent used arrays in a long time. Any suggestions there?

Flynn

Gweedo
02-23-2001, 09:22 AM
Here is it using an array:




#include <iostream.h>

int main()
{
int number[1000];
const int SENTINEL = -1;
int count=0;

//Gets the numbers

cout << "Enter numbers one at a time as requested." << endl;
cout << "When done, enter " << SENTINEL << " to stop." << endl;
cout << "Enter the first score: ";
cin >> number[count];


while (number[count] != SENTINEL)
{
cout<< "Enter the next score: ";
count++;
cin >> number[count];
}


// Outputs original numbers

cout<< "The numbers you entered are: ";
int holder=0;

for (int b=0; b<count; b++)
{
cout<<number[holder]<< " ";
holder++;
}

// Ouputs original numbers backward

cout<<endl<<"The number you entered backwards are: ";
holder=count - 1;

for (int i=0; i<count; i++)
{
cout<< number[holder]<<" ";
holder--;
}

return 0;
}


This will work. :)

flynnwallace
02-23-2001, 11:12 AM
thanks to everyone that helped me. I got the program to work about 3 am last night. I did it with arrays and the professor didnt really mind after all. I know how to use arrays but it had been a long time since I used them last. Thanks again to everyone for their help.

Flynn