#include<iostream>

using namespace std;
void main()
{

    int pro[100];

    for(int a=100;a>=1;a--)
    {
    pro[a]=a;

    cout<<pro[a]<<"\t";}

}

Recommended Answers

All 7 Replies

indexing in C++ goes from 0 to N-1, so your code should be (in code tags) as:

#include<iostream>

using namespace std;
int main() //main always returns an int value.
{

  int pro[100];
  for(int a=99;a>=0;a--)
  {
    pro[a]=a+1;
    cout<<pro[a]<<"\t";
  }
  return 0;
}

If you write a loop that runs backward through an array, it is a good idea to get into the habit of decrementing the control variable at the beginning of the loop:

int pro[100];
for (int a = 100; a > 0;) {
    --a;
    // whatever
}

This technique has two advantages: (1) It lets you initialize the variable directly from the array's size rather than computing the size - 1, thereby reducing the chance for error; (2) It generalizes nicely to iterators, even if they do not support ordering comparisons.

Unusual and unconventional approach. If you are going to do that then you might as well use a while loop instead of a for loop.

Unusual and unconventional approach. If you are going to do that then you might as well use a while loop instead of a for loop.

I would if I wanted the control variable to escape into the surrounding scope.

By the way, let me elaborate on what I mean about iterators. Consider iterating backward through a list<int>. You can't write

// This code does not work!
for (list<int>::iterator it = mylist.end()-1; it >= mylist.begin(); --it) {
    // stuff
}

for two reasons: (1) List iterators don't support - or >=; (2) Even if they did, the loop terminates only when it < mylist.begin(), and there are no circumstances under which that can ever be guaranteed to happen.

One correct way to write this loop is

for (list<int>::iterator it = mylist.end(); it != mylist.begin(); ) {
    --it;
    // stuff
}

As Ancient Dragon correctly points out, I could also have written it this way:

list<int>::iterator it = mylist.end();
while (it != mylist.begin()) {
    --it;
    // stuff
}

I chose not to do so for two reasons: (1) I don't want the value of it available after the loop ends; (2) Using a for makes it clearer to the reader that the purpose of the loop is to deal with every value in a range, even if the range is expressed in an unfamiliar way.

You would be right if this thread was about iterators. It isn't, so you aren't.

You would be right if this thread was about iterators. It isn't, so you aren't.

Well, the general principle carries over to any kind of iteration that runs into one of the bounds of the permissible range. For example, let's change the original example just a bit:

int pro[100];
for(size_t a=99;a>=0;a--)  {
    pro[a]=a+1;
    cout<<pro[a]<<"\t";
}

All I've done here is to change the type of a from int to size_t...but now the code doesn't work, because size_t is an unsigned type, so a>=0 is always true.

Personally, I would rather not have to remember to use one style of loop for signed integer types and a different style for unsigned integers and iterators. For that reason, when I'm iterating over an asymmetric range, I tend to change loop control variables at the beginning of the loop if the end of the range is exclusive, and at the end of the loop if the end of the range is inclusive.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.