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