write a program to read a seuence of numbers terminated by 0 and print the sum of all the even numbers.
How would you write the algorithm/pseudocaode for this

5
Contributors
4
Replies
7
Views
5 Years
Discussion Span
Last Post by Reverend Jim

Do you have any code that you have tested? Because the odds of us doing it for is slim rather than us assisting you in building it...

This is not complicated at all, THINK about it!

1) Input values into an array that are above "0":

input = {1, 2, ..., 2, 3, .. 9,> 0};

2) Check to see if input (at the given time) is "0"

``````   if this value is not greater than 0
then
check to see if the values of the array are even
if true, then->
initalise sum = 0;
sum += value[x] // x representing the location at the given time
end
end
``````

Something like this =) Here, look at this example code, to give you a clear understanding:

``````#include <iostream>
#include <vector>

using namespace std;

double summationOfEven(vector<int> &nums)
{
int sum = 0;

for(unsigned i=0; (i < nums.size()); i++)
{
if(nums[i] % 2 == 0)
{
sum += nums[i];
}
}

return sum;
}
int main(int argc, char *argv[]) {

vector<int> numbers;

bool isSet = false;

while(!isSet)
{
int num = 0;

cout << "Please enter a number: ";
cin >> num;

numbers.push_back(num);

if(num == 0)
isSet = true;
}

cout << summationOfEven(numbers);
}
``````

Output:

Please enter a number: 2
Please enter a number: 4
Please enter a number: 6
Please enter a number: 8
Please enter a number: 5
Please enter a number: 0
20

Quoted Text Here

This is not complicated at all

Especially if you can get someone else to do your homework for you.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.