DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   Alternatives to arrays? (http://www.daniweb.com/forums/thread98670.html)

picklesandmayo Nov 27th, 2007 7:09 pm
Alternatives to arrays?
 
So I started on the final section of my C++ project and got almost done until I re-read the requirements and saw this:

Do not use arrays.

*crap*

Is there any other way to store multiple inputs than arrays? Basically the idea is to use a recursive function get a variable amount of #'s from the user and then display them, only in reverse order. But I don't know how I could have the name of the variable change, without using an array.

I am not looking for a whole program, just someone to point me in the right direction.

Thanks!

Salem Nov 27th, 2007 7:25 pm
Re: Alternatives to arrays?
 
Yes, use a std::vector instead.

Ancient Dragon Nov 27th, 2007 8:21 pm
Re: Alternatives to arrays?
 
how about a linked list ?

or something like this:

void foo(int x)
{
  int n = 0;
  cout << "Enter a number " << x << ":\n";
  cin >> n;
  if(x < maxNum) // maxNum declared elsewhere
        foo(x+1);
  cout << "n = " << n << "\n");
}

Lerner Nov 27th, 2007 11:35 pm
Re: Alternatives to arrays?
 
Do you even need a container (array, vector, list, whatever) for this project? Just a recursive function with single numerical variable should do it I think. The variable would need a default value to discontinue input and spit the variable from each function call return value back out to the screen or file or whereever, if the value isn't the default terminating value.

picklesandmayo Nov 28th, 2007 1:43 am
Re: Alternatives to arrays?
 
Aha! I figured it out on my own:

#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;


int reverseInput(int x);  // prototype


void main()
{       
        int x;
        x = 0;
        cout << "ctrl-z to quit\n";
        reverseInput(x);  // call function
}

int reverseInput(int x)
{
        int count;
        count = 0;
        cout << "Enter a number: ";
        cin >> x;
        if (cin.eof())
        {
                return x;
        }
        else
        {
                count++;
                reverseInput(x);  //recursively call function
                cout << endl
                        << setw(count * 3) << x; // output, reversed
        }
}

Thanks everyone.

Salem Nov 28th, 2007 3:29 am
Re: Alternatives to arrays?
 
Please stop using void main.


All times are GMT -4. The time now is 12:10 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC