I need help figuring out the algorithm for the sum for all of the elements. I have the sum commented out, Everything else functions properly, but I am having a brain fart figuring out how to sum them.
#include "stdafx.h"
#include <iostream>
usingnamespace std;
// =======================
// Function Prototypes
// =======================
void Banner();
bool GoAgain();
int Sum(int);
int main()
{
int amount = 0;
int sum = 0;
do {
Banner();
sum = Sum(sum);
cout << "Sum: " << sum << endl;
}//do
while (GoAgain() == true);
return 0;
}
// =======================
// ========================
void Banner() {
cout << "Welcome to the Fibonacci program!\n";
cout << "Input how many numnbers to be added\n";
cout << "I will compute the sum of the\n";
cout << "amount you chose\n";
cout << "Let's begin!\n";
} // Function Banner()
// =========================
// =========================
bool GoAgain() {
bool validAnswer;
char answer;
do {
cout << "Go again?" << endl;
cout << "[y/Y] to go again. [n/N] to exit: ";
cin >> answer;
if ((answer == 'y') || (answer == 'Y') ||
(answer == 'n') || (answer == 'N'))
validAnswer = true;
else {
validAnswer = false;
cout << "Error. Enter a valid character: ";
}
} while (!validAnswer);
if ((answer == 'y') || (answer == 'Y'))
returntrue;
elseif ((answer == 'n') || (answer == 'N'))
returnfalse;
} // Function GoAgain()
// ===========================
// =============================
int Sum(int amount) {
int first = 0, second = 1, next;
cout << "Input the amount of numbers to be summed: ";
cin >> amount;
if (amount < 0)
cout << "Please enter an integer the is not less than zero. " << endl;
int sum = 0;
for (int ii = 0; ii < amount; ii++)
{
if (ii <= 1)
next = ii;
else
{
next = first + second;
first = second;
second = next;
}// else
// sum = ????
cout << next << endl;
} // for
return sum;
}// Sum()
// ==============================