Initialize Array using a for loop (HELP)

PROBLEM:

Write a for loop to initialize the following array (int data[10]) with the values 10, 9, 8… 1.

I want to initialize data[0] to 10, data[1] to 9, and so on. The last one will be data[9] initialized to 1. I believe that's what the question wants me to do.

ENVIRONMENT:

Compiler - Xcode4
Operating System - Mountain Lion 10.8

THE ISSUE:

It seems like my output screen shows that i'm doing it right. However, I don't know if it's showing me the data[values] in reverse order or if it's really initializing them. Furthermore, when I try to output the values in the order value[0] = 10, value[1] = 9, and so on it doesn't do that. Also I get some junk in between.

MY OUTPUT:

10// it looks like it's being initialized right but is it?
9
8
7
6
5
4
3
2
1
The values starting from data[0] through data[9] are:
1606420288//this is the junk i was referring to
1//it doesn't print it out in the right order. this says data[0] is 1. It should be 10
2
3
4
5
6
7
8
9

MY CODE:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;
int main()
{
    int data[10];
    
    for (int i = 10; i > 0; i--)
    {
        data[i] = i;
        cout << data[i] << endl;
    }
    cout << "The values starting from data[0] through data[9] are:\n";
    
    for (int i = 0; i < 10; i++)
    {
        cout << data[i] << endl;
    }
    return 0;
}






Last edited on
The reason you are getting the junk value and different outputs from the 2 loops is that on the first loop you aren't writing to data[0] which is the first element of the array. Because you don't write to it it contains an undefined value which is whatever value was left over in that memory block from previous operations. To fix this change i > 0 in the first block to i >= 0. You will also need to change line 9 to data[i-1] = i; and line 10 to cout << data[i-1] << endl;. This will still write to all 10 block of data (0 to 9).
Last edited on
Thanks
//it doesn't print it out in the right order. this says data[0] is 1. It should be 10


When I = 10, You are setting data[i]=10 not 1.
You need a second variable to count up

If you want to use data0 - 9, you don't count from 10 you count from 9.

add this to line 6
int num=1;

change line 9 to
1
2
        data[i] = num;
        num++;


your for statement can look like anything but if you keep it simple, it will be easier to change later.

This would be for Zero Relative
for (int i = 9; i >= 0; i--)

For One Relative, you would just add 1 to each number. 9 and 0.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;
int main()
{
    int data[10];
    int num=1;
    for (int i = 9; i >= 0; i--)
    {
        data[i] = num;
        num++;
        cout << data[i] << endl;
    }
    cout << "The values starting from data[0] through data[9] are:\n";
    
    for (int i = 0; i <= 9; i++)
    {
        cout << data[i] << endl;
    }
    return 0;
}
Topic archived. No new replies allowed.