How to find the product of all array number

Hello everyone, can you help with my code. I need to find the product of all array number that has been sent through Serial Monitor. Firstly I have convert my list of array number to token. This is because I need to identify the delimiter(" ") that separate each number from the others. And then I convert it to integer so that I do not multiply any string element in the array.

My for statement at the last code gives error and I don’t know how to write a function that can multiply those number afterwards.I really appreciate your help :slight_smile:

How my program works:
Enter list of number:
12 2 10 1
Final value:
240

void setup() {
  Serial.begin(115200);
  delay(20);

void loop() {
  int i;
  char array[100];
  char *valPosition;
  int limit;
  }
  valPosition = strtok(array, " ");
  int angle[100];
  limit = strlen(angle);
  for(int i =0; i < limit ; i++){ //This line gives me error
    angle[i] = atoi(valPosition);
    Serial.print(angle[i]);
    valPosition = strtok(NULL, " ");
  } 
}

The forum tells me that you have seven posts, so you should know how to properly post. Unfortunately, you don't, so I cannot make sense of your program. I bet it was not written with italics.

Sorry, my mistakes, now I hope you can read it

 limit = strlen(angle);

angle is an int array. You cannot do strlen on it. strlen only works on char arrays.

strtok will return NULL when you get to the end of “array”, so you need a “while” loop here instead of a “for”.

Your code to read a line form the serial port is also broken.
You are assuming Serial.available() will return false at the end of a line, and that it will return true up until that point. All Serial.available() does is tell you if a character has been received. Since serial is slow, and the Arduino can check for and read characters very quickly, it is very likely that will not be the case.
Perhaps study the Serial Input Basics

Pseudo code for finding product…

product = 1
For all numbers... product = product * number

But I was thinking that using while , the value will not be recorder and lost. Also, I thought that the best way is to use the int array but I don't know how to define the limit of the given int array.
Thanks for the link for Serial.input basic though. :slight_smile:

dave_croft:
But I was thinking that using while , the value will not be recorder and lost. Also, I thought that the best way is to use the int array but I don't know how to define the limit of the given int array.
Thanks for the link for Serial.input basic though. :slight_smile:

That makes no sense. Don’t know what you mean by “lost”. It’s only “lost” if you don’t store it somewhere. While loops can store data in arrays too. Look at your (malformed) serial read line for example.

You don’t know the limit. The only way is to read the number list to the end. Since you only have 100 characters for your string you can guess there will never be more than 50 numbers (single digit plus a space for each).

You don’t need the int array at all if you just want the product of all the numbers...

  1. Read the numbers one at a time into a single variable.
  2. Multiply the product by that variable.
  3. Repeat until you reach the end of the string.

Okay, I understand thanks a lot :slight_smile:

You don't really need an array since once you add the value to the product you don't need it anymore. Let me know if you have any questions.

long product = 1;


void setup()
{
  Serial.begin(115200);
  Serial.println("Enter list of number:");
}


void loop()
{
  // Wait for a character to be available
  while (!Serial.available()) {}


  long value = Serial.parseInt(); // Read one number
  Serial.print(value);
  Serial.print(' ');
  product *= value;


  // Check to see if there is another value
  // The parse either ended with a non-digit (which is left in the buffer) or a
  // timeout, in which case there are no further numbers.
  while (Serial.available())
  {
    char inChar = Serial.peek();  // Read it but don't take it from the buffer
    if (isDigit(inChar) || inChar == '-' || inChar == '+')
      return; // Found another number so go to the next iteration of loop()


    // Not a number, remove it from the buffer and check the next character
    Serial.read();


    // If we loop too quickly we can reach the end of the buffer before
    // the last characters arrives.  Adding a small delay here to make sure
    // we don't get ahead of input.
    delay(100);
  }


  // No more numbers.  Display the result.
  Serial.println();
  Serial.println("Final value:");
  Serial.println(product);
  Serial.println();


  // Reset the product and prompt for another list
  product = 1;
  Serial.println("Enter list of number:");
}

Okay, thank you :slight_smile: