Tuesday

Logic error and output error: array size problem

Question: Why and how to get the correct output 
which is "Sum: 5" after you have entered 1 five times?

#include <iostream>
using namespace std;

int main()
{
int mysize = 5;
int myarray[4];

cout << "Enter five numbers and calculate sum" << endl;
for (int i=0; i<mysize; ++i)
{
cout << "Enter number " << i+1 << " => ";
cin >> myarray[i]; // to enter 1 five times
}

int sum = 0;
for (int i=0; i<mysize; ++i)
{
sum += myarray[i];
}
cout << "Sum: " << sum << endl; // sum 1 is wrong

return 0;
}

No comments:

Dictator Leader

Let us be clear from the outset, the struggle for indigenous people has always been about land and self-determination, not primarily about r...