1,105,290 Community Members

Programming

Member Avatar
canadaaaaa
Newbie Poster
2 posts since Nov 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
<M/>
Industrious Poster
4,473 posts since Apr 2012
Reputation Points: 106 [?]
Q&As Helped to Solve: 139 [?]
Skill Endorsements: 114 [?]
Featured
 
0
 

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...

Member Avatar
phorce
Veteran Poster
1,014 posts since Jul 2011
Reputation Points: 105 [?]
Q&As Helped to Solve: 133 [?]
Skill Endorsements: 29 [?]
Featured
 
0
 

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

gezahegn gaje
Newbie Poster
1 post since Dec 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
Unverified Member
 
0
 

Quoted Text Here

Member Avatar
Reverend Jim
Noli mentula
5,428 posts since Aug 2010
Reputation Points: 746 [?]
Q&As Helped to Solve: 651 [?]
Skill Endorsements: 51 [?]
Moderator
Featured
 
0
 

This is not complicated at all

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

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article