0
What is the problem with the code why is it not summing the array correctly
#include <iostream> using namespace std; int main() { int size, x[size], y[size]; for(int i=0; i<2; i++) cin>>x[i]; for(int i=0; i<2; i++) cin>>y[i]; int z[size]; for(int i=0; i<2; i++) z[i]=x[i]+y[i]; for(int i=0; i<2; i++) cout<<z[i]<<"\t"; return 0; }
1 Answer
0
You're declaring your arrays x, y, and z. But the variable size to declare their size, but you haven't initialized size before using it, which leads to undefined behavior. I fixed those anomalies in your code and it runs great. Take a look:
https://sololearn.com/compiler-playground/cIw6qEUfI2Yt/?ref=app