200
How might you make this code more efficient? Write it on the board!
Note: arrayOfIntegers is an array, or collection, of integers. Because computer scientists always count starting at 0, 8 is the "zeroth" element, 6 is the first element, and so on. If you want to access these, you use square brackets. So, arrayOfIntegers[0] = 8, arrayOfIntegers[1] = 6, etc.
int sumOfIntegers = 0;
int arrayOfIntegers[4] = {8,6,2,4};
sumOfIntegers = sumOfIntegers + arrayOfIntegers[0];
sumOfIntegers = sumOfIntegers + arrayOfIntegers[1];
sumOfIntegers = sumOfIntegers + arrayOfIntegers[2];
sumOfIntegers = sumOfIntegers + arrayOfIntegers[3];
return sumOfIntegers;
int sumOfIntegers = 0;
int arrayOfIntegers[4] = {8,6,2,4};
for (int i = 0; i < 4; ++i)
{
sumOfIntegers = sumOfIntegers + arrayOfIntegers[i];
}
return sumOfIntegers;