Click to See Complete Forum and Search --> : Backwards & Forwards
Minime80
11-10-2000, 07:08 PM
Okay, I've got this assignment in my C++ class and I have to write a bunch of programs. There's one that I can't really figure out. This is what I need to do.
Write a program
a. It must contain a function that accepts numbers into an array until it finds a trailer. Make the trailer number in your data statement -99.
b. Write a function that: 1) reads the numbers 2) stops reading when it hits the trailer
c. Write a second function that prints all the elements of the array on one line with appropriate spacing
d. Call the second function a second time using code to 'tell' it to print the elements of the array backwards.
Most of it I can do without even thinking about it, but I can't really figure out how to go about doing the backwards part. Could somebody give me some pointers on what an easy way to do this is. It's just not coming to me. Thanks.
can you just put a parameter in your function prototype that tells whether to print backwards or forwards?
Strike
11-10-2000, 07:20 PM
Depending upon how into C++ you are, this is something that would be perfect for a reverse iterator. But you'd have to know what one of those is first ...
Minime80
11-10-2000, 07:39 PM
Originally posted by siqe:
can you just put a parameter in your function prototype that tells whether to print backwards or forwards?
Yeah, this is what I was thinking about doing, but I couldn't really think of how exactly to make it work.
I've got no idea what a reverse iterator is, but I'd love to learn about it if it's not too much trouble to explain.
when you say print the elements backwards do you mean
10 546 43 is changed to
1. 43 546 10
or
2. 01 645 34
Minime80
11-10-2000, 07:52 PM
Backwards means 10 24 3
would be 3 24 10
something like this, you could find a way to do it so it didn't have to do a an if() in each for loop. maybe have 2 for loops, but i think this is what you want.?
for(int x=0; x<=max_array_index; x++)
{
if(FORWARDS)
cout << array[x] << endl;
else cout << array[max_array_index - x] << endl;
}
[This message has been edited by siqe (edited 10 November 2000).]
Minime80
11-10-2000, 08:27 PM
Yeah, that'll work great! Thanks.
Looking at this also made me curious. Would it be possible to maybe pass the function a number that would determine the direction. I was thinking something like if you pass it a direction variable of 1 it would do x+=direction making it increment by 1, then if you passed it -1 it would subtract 1 from x every iteration of the loop. I don't know if it's possible, but it was just a thought in my head.
you mean the direction could be 2 and it would increment 2 or direction could be -4 and it would jump backwards 4, or you just mean use a number to tell either backward or forward one at a time?
both are possible.
[This message has been edited by siqe (edited 11 November 2000).]
Minime80
11-11-2000, 03:22 AM
I guess I meant being able to jump back 4, and have it start at the end when it's negative and the beginning when it's positive, but I don't want to use an if statement if possible and if an if statement is essential make it as small as possible.