Question of Sequence Numbers

I need you to help me understanding this question:
Build an algorithm that receives as input a sequence of numerical values and outputs the sum of the even values and the multiplication of the odd ones. Consider that a negative value means that there is no more data and it should not be considered as valid data.

Are you intending to do any of your homework, or is the plan to get others to do it for you?

I´m trying to do my homework my friend. But I can't find a solution to stop the program with a negative value

This means that you get a sequence of positive numbers followed by a negative number. Negative number will indicate that you can start processing all the positive numbers as no more numbers will be given.

You can then take all even numbers, such as 2, 4, 6.... etc and add them together, this will be your 1st result. Then you can take all odd numbers such as 1,3,5...etc and multiply them together, that will be you second result. output both and you are done!

1 Like

Test the value to see if it is less than zero; that's the usual definition of negative.

To see if a value is odd, simply bitwise AND the value with 1.
If the result is zero, the value is even, otherwise it is odd.

I'm curious - how is this "important", and does the number of blubbing emoticons indicate the degree of importance?

1 Like

Do you actually want to stop the program when a negative number is received or should that be the trigger to do the calculations required by the assignment ?

Where are the numbers coming from, the Serial monitor perhaps, and do you know how to test when a number is negative ?

Get a refund on your tuition.

You don’t seem to be getting any kind of learning done at that school.

a7

1 Like

Yeah, I want the program to stop when a negative number is detected. and the numbers come by typing....

If the program stops when a negative number is detected how are you going to output the results, or are you planning to do that as each number is read, which does not appear to be a requirement of the assignment

Other post/duplicate DELETED
Please do NOT cross post / duplicate as it wastes peoples time and efforts to have more than one post for a single topic.

Continued cross posting could result in a time out from the forum.

Could you also take a few moments to Learn How To Use The Forum.

Other general help and troubleshooting advice can be found here.
It will help you get the best out of the forum in the future.

Wasn't the other topic about temperatures?
It had fewer blubbing emoticons too.

Be prepared to explain how every step works in case the teacher doesn't believe you wrote this yourself. :slight_smile:

long SumOfEvens = 0;
long ProductOfOdds = 1;

void setup()
{
  Serial.begin(115200);
  delay(200);
  Serial.println("Enter numbers, separated by spaces or commas.");
  Serial.println("Enter a negative number to indicate the end of input.");
}

void loop()
{
  if (Serial.available() == 0)
    return;  // Nothing to do

  int number = Serial.parseInt();

  // Throw away any line ending characters
  while (Serial.peek() == '\n' || Serial.peek() == '\r')
    Serial.read();

  if (number < 0)
  {
    // Negative number: End of input
    Serial.print("Sum of even numbers: ");
    Serial.println(SumOfEvens);
    Serial.print("Product of odd numbers: ");
    Serial.println(ProductOfOdds);

    // Reset
    SumOfEvens = 0;
    ProductOfOdds = 1;
  }
  else if ((number % 2) == 0)
  {
    // Even
    SumOfEvens += number;
  }
  else
  {
    // Odd
    ProductOfOdds *= number;
  }
}
1 Like

That’s one way to make it stop. :wink:

a7

Thanks a million for your help my friend, yeah I will study the code . :+1:

I’d like to open a book on that…

a7

2 Likes

When I look through your postings I get this impression:

a student which is a totally newbee about programming
The learning progress about writing programs on his own is almost zero.

No attempts on trying to write code himself

At least able to say thank you.

Nick: The real job-life will serve quite a lot of situations where to do the work is no joy. Your boss expects you to do the work anyway.

So my recommendation is: to see school as a training place to learn how to finish tasks with a subtstantial part of own work even if you don't enjoy doing it with additional help from others.

Stefan