Integer value multiplication

Hi!
I've created four integers:

int number1 = 5;
int number2 = 10;
int number3 = 15;
int number4 = 20;

I'm going use a for-loop that goes from 2 to 5.
Inside the for-loop, the numbers must be given a new value by multiplying with the value from the for-loop (they SHOULD use the value that the for-loop creates).

E.g: When for-loop has the value 2, number1 must be multiplied by the for-loop's value 2. When the for-loop has the value 3, number2 must be multiplied by the for-loop's value 3 etc.

The new value should also be printed inside the for loop right after multiplication has been performed for each of the numbers.

Does anyone have a good example? I was looking in the Arduino.cc -> Built-In examples -> For loops with arrays, but that is not what I'm looking for.

Thanks for every reply in advance!

Make things simpler for yourself - put the numbers you've been given into an array.

You pretty much wrote the code right there. Try replacing the word "when" with the word "if"

Sure arrays is one way to code it
If you don't use arrays you have to code four different if-conditions or a switch-case-break-statement

As a starting point
Write down on a sheet of paper each iteration of your for-loop with the values that you have chosen

Your description is confusing

Is this the output that you are looking for ?

2 times 5 equals 10
2 times 10 equals 20
2 times 15 equals 30
2 times 20 equals 40
3 times 5 equals 15
3 times 10 equals 30
3 times 15 equals 45
3 times 20 equals 60
4 times 5 equals 20
4 times 10 equals 40
4 times 15 equals 60
4 times 20 equals 80
5 times 5 equals 25
5 times 10 equals 50
5 times 15 equals 75
5 times 20 equals 100

I'm looking for an example that will give the following output:

2 times 5 equals 10 and print out 10
3 times 10 equals 30 and print out 30
4 times 15 equals 60 and print out 60
5 times 20 equals 100 and print out 100

Was this easier to understand? :smiley:

pseudo-code

if for-loop-variable is equal to 2
calculate for-loop-variable * number1
store result in a variable
print variable to serial monitor

if for-loop-variable is equal to 3
calculate for-loop-variable * number2
store result in a variable
print variable to serial monitor

if for-loop-variable is equal to 4
calculate for-loop-variable * number3
store result in a variable
print variable to serial monitor

if for-loop-variable is equal to 4
calculate for-loop-variable * number4
store result in a variable
print variable to serial monitor

This is still very very basic
You should at least demonstrate that you know how to:

  • code a for-loop
  • code how to store a result of a calculation in a variable
  • code how to print to the serial monitor

if you can't
Take a look into this tutorial:

Arduino Programming Course

It is easy to understand and has a good mixture between explaining important concepts and example-codes to get you going. So give it a try and report your opinion about this tutorial.

best regards Stefan

When is our assignment due to be handed in ?

2 Likes

Hello

Try this with best regards to you tutor :nerd_face:

constexpr int Inputs[] {5,10,15,20};

void setup() 
{
  Serial.begin(115200); 
  int element=2;
  for (auto Input:Inputs) Serial.println(Input*element++);
}
void loop() {}

Have a nice day and enjoy coding in C++.

  for (int loopIndex = 2; loopIndex <= 5; loopIndex++)
  {
    Serial.println(number1 * loopIndex);
    Serial.println(number2 * loopIndex);
    Serial.println(number3 * loopIndex);
    Serial.println(number4 * loopIndex);
  }

or

  for (int loopIndex = 2; loopIndex <= 5; loopIndex++)
  {
    number1 = 5 * loopIndex;
    Serial.println(number1);
    number2 = 10 * loopIndex;
    Serial.println(number2);
    number3 = 15 * loopIndex;
    Serial.println(number3);
    number4 = 20 * loopIndex;
    Serial.println(number4);
  }

Or do you want the multiplications to accumulate? Like:

number1 = number1 * 2; // 10
number1 = number1 * 3; // 30
number1 = number1 * 4; // 120
number1 = number1 * 5; // 600

That would be something like:

  for (int loopIndex = 2; loopIndex <= 5; loopIndex++)
  {
    number1 *= loopIndex;
    Serial.println(number1);
    number2 *= loopIndex;
    Serial.println(number2);
    number3 *= loopIndex;
    Serial.println(number3);
    number4 *= loopIndex;
    Serial.println(number4);
  }

Note "x *= y;" is a shortcut for "x = x * y;" and saves you from entering the 'x' twice.

It would help if you showed an example of the desired output.

See post #5

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.