How can I make this print the arguments in reverse order

Oct 23, 2018 at 10:26am
How can I make this print the arguments in reverse order? I tried re-arranging the loop and I can't do it. Thanks

1
2
3
4
5
6
7
8
9
10
#include <iostream>

int main(int argc, char **argv)
{
	std::cout<<"Received " << argc << " arguments.\n";
    
    for (int i = 0; i<argc; i++)
        std::cout<<"argument" << i << ": " << argv[i] << std::endl;
	
}
Oct 23, 2018 at 10:53am
You need to redefine your loop, so that:
- the index starts at its highest value
- the index decrements on each iteration (instead of incrementing)
- the loop stops when the index is lower than the index of the first item
Oct 23, 2018 at 11:04am
this just prints argument 3 without the argument then stops.What is wrong with it?

#include <iostream>

int main(int argc, char **argv)
{
std::cout<<"Received " << argc << " arguments.\n";

for (int i = argc; i>0; i--)
std::cout<<"argument" << i << ": " << argv[i] << std::endl;

}
Oct 23, 2018 at 11:10am
If the index of the first element is 0, what will the index of the last element be?
Oct 23, 2018 at 11:23am
so if i = amount of arguments whilst i is greater than 0 count down?
Oct 23, 2018 at 11:30am
How does that answer my question?
Oct 23, 2018 at 11:32am
how is the index of the first element 0 when i = argc which is 3 arguments?
Oct 23, 2018 at 11:34am
What are the first and last value that this loop prints?
1
2
3
for ( int X = 0; X < argc; ++X ) {
  std::cout << X << '\n';
}


What are the first and last value that this loop prints?
1
2
3
for ( int X = argc; 0 < X; --X ) {
  std::cout << X << '\n';
}



Do the loops print same amount of values?
Do the loops print same list of values?
Oct 23, 2018 at 11:48am
should it be


#include <iostream>

int main(int argc, char **argv)
{
std::cout<<"Received " << argc << " arguments.\n";

for (int i = argc; 0<argc; i--)
std::cout<<"argument" << i << ": " << argv[i] << std::endl;
}

i get segmentation fault(core dumped)
Oct 23, 2018 at 12:06pm
or

#include <iostream>

int main(int argc, char **argv)
{
std::cout<<"Received " << argc << " arguments.\n";

for (int i = argc; 0<i; i--)
std::cout<<"argument" << i << ": " << argv[i] << std::endl;
}

just stalls at argument 3
Oct 23, 2018 at 12:14pm
sorry for being such a bozo i really need you guys help to understand this.
Oct 23, 2018 at 12:15pm
You're misunderstanding me.
I've already told you that you should initialise i to be the value of the index of the final argument. I'm asking you what that value should be.

(Hint: if an array has, say, 5 elements, then what is the index of the final element in the array?)
Last edited on Oct 23, 2018 at 12:17pm
Oct 23, 2018 at 12:18pm
should it be 0?
Oct 23, 2018 at 12:24pm
would the index of the final element be 4?
Oct 23, 2018 at 12:26pm
would the index of the final element be 4?


Correct. Array indexing in C and C++ starts with 0, so the final element would have an index of 4.

So, if argv, is an array, and the number of elements in it is argc, then what will the index of the final element be?
Oct 23, 2018 at 12:28pm
this works but it segfaults at the end of the program


#include <iostream>

int main(int argc, char **argv)
{
std::cout<<"Received " << argc << " arguments.\n";

for (int i = argc-1; 0<argc; i--)
std::cout<<"argument" << i << ": " << argv[i] << std::endl;
}
Oct 23, 2018 at 12:30pm
this worked

#include <iostream>

int main(int argc, char **argv)
{
std::cout<<"Received " << argc << " arguments.\n";

for (int i = argc-1; 0<i+1; i--)
std::cout<<"argument" << i << ": " << argv[i] << std::endl;
}

but how should i really of done it?
Oct 23, 2018 at 12:47pm
You've got the start value of i correct, and you're decrementing it properly.

So, the values of i are going to start at argc-1 - which is correct - and the loop will stop iterating when when i is -2 (because i + 1 will be -1, which is less than 0).

Clearly, that can't be right, as it will mean you try to access argv[-1], which is outside the bounds of the array.

We've already established that the index of the first element in argv (or in any array) is 0, so you can't go lower than that. In fact, the first element in argv will be the name of the program, so you should decide whether you want to include that in your output or not.

Based on that decision, you want to stop either when i < 1, or i < 0.
Oct 23, 2018 at 1:11pm
thanks guys
Oct 23, 2018 at 1:12pm
You're welcome. Hope it's all clear now.
Topic archived. No new replies allowed.