Printing numbers in array!

Hye!I needed help regarding this code. Its giving an error "Too much initializers". There are 12 slots(including 0) and 12 numbers.Whats the mistake?

1
2
3
4
5
6
7
8
9
10
11
12
  #include<iostream>
using namespace std;
int main()
{  
   int x[11]={1,2,3,4,5,6,7,8,9,10,11,12};
     for (int i=0;i<12;i++)
	  { 
	  cout << x[i];
               
	  }
   
}Put the code you need help with here.
I think you're misinterpreting what the size of the array means.

When you declare an array with a particular size, that is the number of elements that can be in the array. But the indices for accessing the elements start at 0.

1
2
3
int x[2]; // Declaration -- declares an array with 2 elements in it
x[0] = 42;
x[1] = 169; 


1
2
3
4
int y;
y = x[0]; // valid, first number
y = x[1]; // valid, second number
y = x[2]; // INVALID! Access goes out of bounds of array 


Your array has 12 elements in it, so either make it be
int x[12]={1,2,3,4,5,6,7,8,9,10,11,12};
or, alternative, you don't have to specify the size, it will be deduced for you:
int x[]={1,2,3,4,5,6,7,8,9,10,11,12};
Last edited on
Thanks! Got it!
Topic archived. No new replies allowed.