| | |
Alternatives to arrays?
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2007
Posts: 21
Reputation:
Solved Threads: 0
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!
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!
how about a linked list ?
or something like this:
or something like this:
C++ Syntax (Toggle Plain Text)
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"); }
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.
•
•
Join Date: Jul 2005
Posts: 1,675
Reputation:
Solved Threads: 261
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.
•
•
Join Date: Nov 2007
Posts: 21
Reputation:
Solved Threads: 0
Aha! I figured it out on my own:
Thanks everyone.
C++ Syntax (Toggle Plain Text)
#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.
![]() |
Similar Threads
- C++ Books (C++)
- Arrays (C++)
- Quicksorting linked list - simple algorithm (C)
- Pls help :Find first non-repeated char in a string (C++)
- Difficulty with IO (C++)
- Vectors Versus Arrays (Java)
- Doubt about compiling. (Legacy and Other Languages)
- overloading [] with 2 dimensional arrays (C++)
Other Threads in the C++ Forum
- Previous Thread: C++ game?
- Next Thread: help writting a c++ programme
| Thread Tools | Search this Thread |
api array based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock wordfrequency wxwidgets






