2

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

6
  • iostream.h is not a standard header. Use iostream. Commented Apr 27, 2014 at 6:22
  • Use std::array. This won't solve your problem but you should start using it. Also, this is c++. No need to tag C. Commented Apr 27, 2014 at 6:23
  • 1
    thanks dear brothers charis & Ben for the suggestion and correction. Commented Apr 27, 2014 at 6:23
  • @shujat7 when you get a suggestion for how a question might be improved (that you agree with), you should apply it. I have not changed the use of <iostream.h>; @chris is not quite accurate - it is a standard header, it is merely deprecated in favour of <iostream>. Using the preferred header form would also require scope resolution to std:: for all iostream symbols (e.g. std::cout). There is always a chance you are using some ancient compiler that does not support namespaces. Commented Apr 27, 2014 at 7:55
  • @Clifford, It was not in the standard ever since the very first ISO standard back in 1998, which is what I mean by non-standard. As far as I know, before that, implementations could have it or not, but not even being mentioned in the standard, implementations have no need to keep it there. The support for it tends to be in extremely old compilers. Commented Apr 27, 2014 at 15:58

6 Answers 6

5

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);
Sign up to request clarification or add additional context in comments.

2 Comments

ok, thanks for reply but now let me know, in first "Total Age is : 1700868285" what is number "1700868285" is ? is it memory address?
Hopefully it was the memory address used to hold your bank account balance when you last logged on.
3

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.

2 Comments

Strictly speaking this means that you are setting/initializing the values [1...9] in age twice.
@AdeMiller True, but this little bit of performance hit will avoid you a lot of bugs in the future. If in the future you find that it is taking too much CPU, you can then remove it.
1

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.

3 Comments

ok, thanks for reply but now let me know, in first "Total Age is : 1700868285" what is number "1700868285" is ? is it memory address?
It has no special meaning - it just happens to be the content of this memory address. You'll get different numbers in different situations.
Thanks dear and sweet sweet @Benesh for helping
0

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.

Comments

0

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.