Reputation:
I am new with C++, so I need your help. I wrote this program,
#include<iostream.h>
int main(){
int totalAge = 0;
int age[10];
for(int j= 1; j<10; j++){
age[j] = j;
cout << age[j] << endl;
}
for(int i = 0; i<10; i++){
totalAge = age[i];
cout << "Total Age is : " << totalAge << endl;
}
system("pause");
}
Where as the output on command prompt is this:
1 2 3 4 5 6 7 8 9
Total Age is : 1700868285
Total Age is : 1
Total Age is : 2
Total Age is : 3
Total Age is : 4
Total Age is : 5
Total Age is : 6
Total Age is : 7
Total Age is : 8
Total Age is : 9
Press any key to continue . . .
The only thing I want to know is that why is first "Total Age is : 1700868285" I believe it should be "Total Age is : 0" Please explain it. Thanks
Upvotes: 2
Views: 131
Reputation: 13743
Your first loop never initialized age[0]
. In some languages variables are automatically set to their default value. C++ is not one of them. C++ makes no guarantees about the value of an uninitialized variable. The 1700868285 value is simply whatever happened to be in the memory used to store age[0]
when interpreted as an int
.
Your code should read like this. Now age[0]
is set to 0
.
for(int j = 0; j < 10; j++)
{
age[j] = j;
cout << age[j] << endl;
}
This is a good example of why you should be very diligent about initializing variables in C++. As pointed out below by @Caesar you could start off by declaring the array and initializing it at the same time. This adds a small overhead in that you have written zeros into the array and then update it with the values you really want but this will help avoid the issue that caught you here.
You can actually do this using std::array
(as pointed out by @Ben) and std::iota
from the Standard Library which will make your code simpler.
std::array<int, 10> age = { 0 }; // initialize all the values to zero.
std::iota(begin(age), end(age), 0); // Set the values of the elements to 0...9
If you want to make even more C++ like you can also consider using the new form of for
syntax.
for (auto a : age)
{
totalAge = a;
cout << "Total Age is : " << totalAge << endl;
}
And consider using the std:accumulate
algorithm, rather than manually calculating the sum.
int totalAge = std::accumulate(begin(age), end(age), 0);
Upvotes: 5
Reputation: 21
Your first loop starts from
int j= 1;
and the first element of the array is age[0]. In the second loop you're trying to get the value of age[0] and since it was never initialized you receive this long ass address number.
Upvotes: 0
Reputation:
In your first for
loop, you assign array index 1 to 1. Array index 0 is the first, which is left unassigned to anything, which means that it can have any value (because it's uninitilized). Then, in your second for
loop, you access that index, which has an uninitilized value.
I think you want to change your first for
loop to look something like this:
for(int j = 0; j < 10; j++){
age[j] = j;
cout << age[j] << endl;
}
Note that the only change is that it now starts from 0.
Upvotes: 0
Reputation: 7542
Array indexing begins from zero.While inputting you have given no value to age[0] while at the time of outputting you are using age[0] which gives a garbage value.So run your first loop from 0 to 9.
Upvotes: 0
Reputation: 9853
The reason you have this issue is because you never set the value for the first element in the array age
. Therefore, it is value is undefined.
An easy fix is to declare your array like this int age[10] = { 0 };
, initializing all the values in the array to zero.
Upvotes: 3
Reputation: 3428
You never initialize age[0]
, becuase j
is never equal to 0
. So in the second loop, when i=0
, totalAge
is assigned the uninitialized value age[0]
which contains garbage value.
Upvotes: 1