What is the problem with the code why is it not summing the array correctly | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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; }

21st Jul 2021, 2:59 PM
Es -x10
Es -x10 - avatar
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
20th Apr 2024, 5:55 AM
`нттp⁴⁰⁶
`нттp⁴⁰⁶ - avatar