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!
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?
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
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 take a few moments to Learn and Use The Forum
[quote]Hello.
Welcome to the Arduino Forum. This guide explains how to get the best out of this forum. Please read and follow the instructions below. Being new here you might think this is having rules for the sake of rules, but that is not the case. If you donāt follow the guidelines all that happens is there is a long exchange of posts while we try to get you to tell us what we need in order to help you, which is frustrating for you and frustrating for us.
[/quote]
It will help you get the best out of the forum in the future.
-
Your OS and version can be valuable information, please include it along with extra security you are using.
-
Always list the version of the IDE you are using and the board version if applicable.
-
Use quote or add error messages as an attachment NOT a picture.
-
How to insert an image into your post. ( Thanks @sterretje )
-
Add your sketch where applicable but please use CODE TAGS ( </> )
-
Add a SCHEMATIC were needed even if it is hand drawn
-
Add working links to any specific hardware as needed (NOT links to similar items)
-
Remember that the people trying to help cannot see your problem so give as much information as you can
COMMON ISSUES
-
Ensure you have FULLY inserted the USB cables.
-
Check you have a COMMON GROUND where required. ( Thanks @Perry)
-
Where possible use USB 2.0 ports or a USB 2.0 POWERED HUB to rule out USB 3.0 issues.
-
Try other computers where possible.
-
Try other USB leads where possible.
-
You may not have the correct driver installed. CH340/341 or CP2102 or FT232 VCP Drivers - FTDI
-
There may be a problem with the board check or remove your wiring first.
-
Remove any items connected to pins 0 and 1.
COMPUTER RELATED
-
Close any other serial programs before opening the IDE.
-
Ensure you turn off any additional security / antivirus just to test.
-
There may be a problem with the PC try RESTARTING it.
-
You may be selecting the wrong COM port.
-
Avoid cloud/network based installations where possible OR ensure your Network/Cloud software is RUNNING.
-
Clear your browsers CACHE.
-
Close the IDE before using any other serial programs.
-
Preferably install IDEās as ADMINISTRATOR or your OS equivalent
ARDUINO SPECIFIC BOARDS
-
CH340/341 based clones do not report useful information to the āget board infoā button.
-
NANO (Old Types) some require you to use the OLD BOOTLOADER option.
-
NANO (ALL Types) See the specific sections lower in the forum.
-
NANO (NEW Types) Install your board COREās.
-
Unless using EXTERNAL PROGRAMMERS please leave the IDE selection at default āAVRISP mkIIā.
-
Boards using a MICRO usb connector need a cable that is both DATA and CHARGE. Many are CHARGE ONLY.
CREATE editor install locations.
-
On macOs ~/Applications/ArduinoCreateAgent-1.1/ArduinoCreateAgent.app/Contents/MacOS/config.ini
-
On Linux ~/ArduinoCreateAgent-1.1/config.ini
-
On Windows C:\Users[your user]\AppData\Roaming\ArduinoCreateAgent-1.1
Performing the above actions may help resolve your problem without further help.
Language problem ?
Try a language closer to your native language:
Thanks to all those who helped and added to this list.
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.
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;
}
}
Thatās one way to make it stop.
a7
Thanks a million for your help my friend, yeah I will study the code .
Iād like to open a book on thatā¦
a7
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
This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.