Fill auto incrementing arrays

Hi there
I am trying to create two arrays of 10000 doubles. The goal is to have one count from 0 to 10000, and have the other count down from 10000 to 0

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

#include <iostream>

using namespace std;

int main ()
{
cout << "testing" << endl;

double first[10000];
    unsigned int i =100;
    for (unsigned int i= 100; i <10001; i++){
        first[i] = i;
        i++;
    }

double second[10000];
    unsigned int j = 10000;
    for (unsigned int j = 10000; j = 100; i--){
        second[j]= j;
        j--;
    }
return 0;
cout << first[4] << endl;

}
I'm stuck, and a little braindead. any tips?

Thanks 
1. You don't state your problem.
2. Why are your arrays type double when you're storing type int?
3. Lines 11 and 18 are unnecessary.
4. Initialization, condition and decrement on line 19 is wrong.
5. Increment on line 14 is unnecessary (unless you want to skip every other number).
6. Decrement on line 21 is unnecessary (when you fix line 19, and assuming you don't want to skip every other number).
Last edited on
good questions, will fix.
any idea why my print wont work? am i not initializing the array properly?
int main ()
{
cout << "testing" << endl;

int first[10000];
for (unsigned int i= 100; i <10001; i++){
first[i] = i;
}
int second[10000];
for (unsigned int j = 10000; j = 100; j--){
second[j]= j;
}
return 0;
cout << first[4] << endl;

}
Your program doesn't output anything because you return before you start outputting. You still haven't finished fixing your second for loop.

@TarikNeaj
Oops, I missed that.
Last edited on
Well its kinda obvious isint it?

Look at your loop

1
2
3
for (unsigned int i= 100; i <10001; i++){
first[i] = i;
}


i starts at 100. Meaning, your array will be filled from 100-10001. Which is wrong in the first place, because place 10000 does not exist since arrays start from 0. So the first element you store in first[100] and then all the way to the end. So first[4] does not exist.
ok, good tips.

say I want o start with element 0 as 100

for (unsigned int i= 0; i <10001; i++){
first[i] = (i + 100);
}

will this assign the first element 100, the second 101, third 102 etc....
Good thinking :) Yes it will! But you would probably want to change the for loop so it runs

i < 10000;
thanks,

still confused why i can't see the element using

cout << first[4] << endl;


Last edited on
It works fine for me? I get the number 104.

1
2
3
4
5
6
int first[10000];
	for (unsigned int i = 0; i <1000; i++){
		first[i] = (i+100);
	}

	cout << first[4] << endl;
hmmm closed and opened....it worked...
Much appreciated!
Topic archived. No new replies allowed.