Alternatives to arrays?

Reply

Join Date: Nov 2007
Posts: 21
Reputation: picklesandmayo is an unknown quantity at this point 
Solved Threads: 0
picklesandmayo picklesandmayo is offline Offline
Newbie Poster

Alternatives to arrays?

 
0
  #1
Nov 27th, 2007
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!
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,851
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Alternatives to arrays?

 
0
  #2
Nov 27th, 2007
Yes, use a std::vector instead.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,152
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1437
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Most Valuable Poster

Re: Alternatives to arrays?

 
0
  #3
Nov 27th, 2007
how about a linked list ?

or something like this:
  1.  
  2. void foo(int x)
  3. {
  4. int n = 0;
  5. cout << "Enter a number " << x << ":\n";
  6. cin >> n;
  7. if(x < maxNum) // maxNum declared elsewhere
  8. foo(x+1);
  9. cout << "n = " << n << "\n");
  10. }
Last edited by Ancient Dragon; Nov 27th, 2007 at 8:25 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,671
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 261
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Alternatives to arrays?

 
0
  #4
Nov 27th, 2007
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.
Last edited by Lerner; Nov 27th, 2007 at 11:37 pm.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 21
Reputation: picklesandmayo is an unknown quantity at this point 
Solved Threads: 0
picklesandmayo picklesandmayo is offline Offline
Newbie Poster

Re: Alternatives to arrays?

 
0
  #5
Nov 28th, 2007
Aha! I figured it out on my own:

  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cmath>
  4.  
  5. using namespace std;
  6.  
  7.  
  8. int reverseInput(int x); // prototype
  9.  
  10.  
  11. void main()
  12. {
  13. int x;
  14. x = 0;
  15. cout << "ctrl-z to quit\n";
  16. reverseInput(x); // call function
  17. }
  18.  
  19. int reverseInput(int x)
  20. {
  21. int count;
  22. count = 0;
  23. cout << "Enter a number: ";
  24. cin >> x;
  25. if (cin.eof())
  26. {
  27. return x;
  28. }
  29. else
  30. {
  31. count++;
  32. reverseInput(x); //recursively call function
  33. cout << endl
  34. << setw(count * 3) << x; // output, reversed
  35. }
  36. }

Thanks everyone.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,851
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Alternatives to arrays?

 
0
  #6
Nov 28th, 2007
Please stop using void main.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC