C++ array logic issue -


i new c++ , need understanding output of 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 = 0; i<10; i++){              totalage = age[i];            cout << "total age : " << totalage << endl;                 }      system("pause"); } 

the output on command prompt this:

1 2 3 4 5 6 7 8 9  total age : 1700868285 total age : 1 total age : 2 total age : 3 total age : 4 total age : 5 total age : 6 total age : 7 total age : 8 total age : 9  press key continue . . . 

why show total age : 1700868285? believe should total age : 0.

currently loop

 for(int j= 1; j<10; j++){           age[j] = j;          cout << age[j] << endl;  } 

it means have skipped position age[0] because j=1 first iteration. why compiler showing garbage right there. need change

 for(int j= 0; j<10; j++){              age[j] = j;          cout << age[j] << endl;  } 

means need j=0 , rest of things done.


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -