quick array question

How would I Declare an array of ints named Years of size 100. Write code that initializes each element of the array to a value of one more than the index.
do you mean something like you have an array of size 50 and initialize each element with 51?
Array declaration in C/C++ is typically done this way:

int Years[100];

However, setting values for this array can be done in various ways. I'd suggest using for-loop in this situation, 'cause you happen to have a certain condition (each element must have value one bigger than their index, if I understood correctly) required to implement for each element in the array.

Please see these pages for more information:
http://www.cplusplus.com/doc/tutorial/arrays/
http://www.cplusplus.com/doc/tutorial/control/
I do believe something like this will do the trick. I don't know if you need it but the second for loop is for displaying the contents of the array after each position has been given their value.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;

int main(void) {
  int years[100];

  for (int n = 0; n < 100; ++n) {
    years[n] = n+1;
  }

  for (int x = 0; x < 100; ++x) {
    cout << years[x] << " ";
  }
  return 0;
}
Topic archived. No new replies allowed.