0

Why when a code fails tend to give always the same high numbers?

I made some codes and when the calculations are "bad" as ouput it always print the same numbers no matter what I put in "cin"

4th Nov 2017, 9:54 PM
silvia mena
silvia mena - avatar
6 Antworten
+ 1
#include <iostream> using namespace std; int main() { int coef; cin>>coef; int V[coef]; int c ; cin>>c; int a; a = V[0]; for(int i=0;i<coef;i++){ cin>>V[i]; } for(int i=1;i<coef;i++){ cout<<a<<endl; int a = a*c+V[i]; } cout <<"el resto es "<<a; return 0; }
4th Nov 2017, 10:23 PM
silvia mena
silvia mena - avatar
+ 1
Thankks!
4th Nov 2017, 10:27 PM
silvia mena
silvia mena - avatar
0
The computer only does what it is told so you had to tell it to print that number. The simplest way is to print the wrong variable. But, failing to initialize the variable also works.
4th Nov 2017, 10:10 PM
John Wells
John Wells - avatar
0
a = V[0]; puts random junk in a as you haven't stored anything in there. The a in the loop is a different one from the previous as you stated 'int a = a*c+V[I]'
4th Nov 2017, 10:26 PM
John Wells
John Wells - avatar