I can't understand why the outputs are different when I put 1 array into a very simple for loop and when I put two arrays into it.
int arrR[100];
int arrN[100];
//function to run the simulation
void runsim(){
for(int i=1;i<=100;i++){
arrN[i] = i;
arrR[i] = i;
}
}
//function to print the array
void printarr(int x[100]){
for(int i=0;i <= 100;i++){
cout << x[i] << ", ";
}
cout << endl;
}
int main(){
runsim();
printarr(arrR);
printarr(arrN);
return 0;
}
This outputs arrR as: 0,1,2,3,4,5,6,...,100 which is what I want, but it outputs arrN as: 100,1,2,3,4,5,6,...,100 which I do not understand.
If I remove arrR from the for loop arrN prints how I want
int arrR[100];
int arrN[100];
//function to run the simulation
void runsim(){
for(int i=1;i<=100;i++){
arrN[i] = i;
}
}
//function to print the array
void printarr(int x[100]){
for(int i=0;i <= 100;i++){
cout << x[i] << ", ";
}
cout << endl;
}
int main(){
runsim();
printarr(arrN);
return 0;
}
This outputs arrN as 0,1,2,3,4,5,6,...,100
Interestingly, if I change all the 100's to 10's in the code this problem also goes away even if I keep both arrays in the for loop.
Can anyone help me understand what I'm missing? I'm sure it's simple because I'm pretty new to C++. Thank you!