#include <iostream>
usingnamespace std;
int main()
{
int a, sum;
int arrayname[] = {1, 2, 3, 4, 5};
for (a=0; a<5; a++)
{
sum = 0;
sum+=arrayname[a];
}
cout << sum << endl;
return 0;
}
When "sum" is shown onscreen, it has a value of 5 whereas I think it *should* have a value of 15 because 1+2+3+4+5 = 15. I'm just confused as to why it's not equaling 15. I have the sum within the loop.
#include <iostream>
usingnamespace std;
int main()
{
int a, sum = 0 ;
/* or use this
int a,sum;
sum = 0;
*/
int arrayname[] = {1, 2, 3, 4, 5};
for (a=0; a<5; a++)
{
sum+=arrayname[a];
}
cout << sum << endl;
return 0;
}