How can I make this print the arguments in reverse order

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;
	
}
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
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;

}
If the index of the first element is 0, what will the index of the last element be?
so if i = amount of arguments whilst i is greater than 0 count down?
How does that answer my question?
how is the index of the first element 0 when i = argc which is 3 arguments?
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?
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)
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
sorry for being such a bozo i really need you guys help to understand this.
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
should it be 0?
would the index of the final element be 4?
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?
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;
}
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?
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.
thanks guys
You're welcome. Hope it's all clear now.
Topic archived. No new replies allowed.