Reading/Writing to an Un-initialized variable will produce an Undefined Behavior.
In your case you should initialize the array before using it.
int x[3] = {4, 5, 6};
Also that is not how to iterate over the array elements:
for (i = 0; x[i] < 10; i++);{}
Above you should check whether i
is less than the size of the array (3) not the value 10.
Also remove the semicolon ;
Because in your code no iteration will occur and the body below for loop
will be executed once.
So your code will look like:
int x[3] = {4, 5, 6};
for (int i = 0; i < 3; i++) {
x[i] *= x[i];
std::cout << x[i];
}
std::cin.get();
As I guess above from what output you wanted and what the loop for does I filled the array with the values: 4, 5, 6
;
Also you shouldn't print the elements of the array that way:
std::cout << x;
Above it will only print the address of first element x[0]
because the name of an array will decay to pointer to its first element (element 0).
std::cout << *x; // will print 16 (4 * 4). Which is the first element.
To print or assign all the elements use iteration:
for(int i(0); i < 3; i++)
std::cout << x[i] << ", ";