Anghi22
Anghi22

Reputation: 5

Trying to create a better loop for naming array elements

I have started studying arrays and have just started making some practice but I am having some problems with using loops to name the elements inside of a specific array.

I was trying to make this piece of code that assigned the numbers from 1 up to 12(to resemble the months of the year) to the ints inside of the array, this is what I came up with:

#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
int array[12];

for (int i = 0; i < 12;) {
    cout << "Month number " << i + 1 << endl;
    array[i] = (i++);
}

return 0;
}

What I don't like about this is the fact that I had to leave the increment/decrement space inside of the for loop empty. I had initially tried making the code look something like this:

#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
int array[12];

for (int i = 0; i < 12; i++) {
    cout << "Month number " << i + 1 << endl;
    array[i] = i++;
}

return 0;
}

But this way, even if the first element of the array came out correct, the subsequent ones didn't. I think the reason for this is that the i++ in the last statement of the loop makes the value of i increment but I couldn't find a way around it without having to add another line with i-- or doing as I did in the first code I posted.

Could anyone offer me a hand in understanding how to make it so that i can store the value of i, incremented by one, inside of that specific array element, without incrementing it for the whole loop(if it is possible)?

I know there are ways around it, just like I showed in the first code i posted, but it's something that's bugging me and so I would like to make it more visually pleasing.

Please, keep in mind that I am just a beginner :) Thanks in advance for the answers, and sorry for the long question.

Edit: Apparently, coding like this:

#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
    int array[12];

    for (int i = 0; i < 12; i++) {
        cout << "Month number " << i + 1 << endl;
        array[i] = i + 1;
    }
    cout << array[4] << endl;

    return 0;
}

makes it so that the program works correctly and looks like I wanted, but I can't comprehend why it does :(

Edit 2: Apparently, as UnholySheep pointed out, I missed on the fact that + 1 does not modify the value of the integer, while ++ does. Thanks to everyone that answered and explained how ++ and +1 work!

Upvotes: 0

Views: 66

Answers (2)

user8145466
user8145466

Reputation: 88

Simply do i+1 again.

for (int i = 0; i < 12; i++) 
{
    cout << "Month number " << i + 1 << endl;
    array[i] = i + 1;
}

Now it's obvious you actually want to start at 1 and go to 12, so this seems somewhat better with less repetition:

 for (int i = 1; i <= 12; i++) 
 {
     cout << "Month number " << i << endl;
     array[i-1] = i;
 }

EDIT: As for your edit, the reason why this works is because i++ operator works on the particular i variable. It increments that existing i by one, making it so that the next time you access i, it will be 1 more than it was before.

Writing i+1, on the other hand, creates a completely new, temporary, variable (actually a constant). So when you write

array[i] = i+1; 

you're saying that you want i to remain unchanged, but you want to create a new number, one bigger than i, and put that new number into the array.

You can even write it out longer to be completely explicit:

int newNumber = i+1;
array[i] = newNumber;

Upvotes: 2

chevybow
chevybow

Reputation: 11888

for (int i = 0; i < 12; i++) {
    cout << "Month number " << i + 1 << endl;
    array[i] = i+1;
}

No reason to increment i in the loop

Upvotes: 0

Related Questions