53 lines
1.1 KiB
C++
53 lines
1.1 KiB
C++
/**
|
|
* Øvelse variabler 1:
|
|
*
|
|
* Finn gjennomsnittet av 4 verdier og skriv det ut i «seriell overvåker» vinduet
|
|
* Det skal brukes variabler!
|
|
*/
|
|
|
|
/**
|
|
* Version 1
|
|
*/
|
|
float numOne = 1337;
|
|
float numTwo = 256;
|
|
float numThree = 512;
|
|
float numFour = 1024;
|
|
|
|
/**
|
|
* Version 2
|
|
*/
|
|
float myNumbers[4] = {1337, 256, 512, 1024};
|
|
|
|
void setup() {
|
|
Serial.begin(9600);
|
|
// Version 1
|
|
float sumOne = numOne + numTwo + numThree + numFour;
|
|
float dividedOne = sumOne / 4;
|
|
|
|
Serial.print("Version 1: ");
|
|
Serial.print(dividedOne);
|
|
Serial.println();
|
|
|
|
// Version 2
|
|
// Divide length of array by length of a single float byte,
|
|
// to get the "correct count" (how many single `float`s exist in the array)
|
|
// of myNumbers in the relevant array.
|
|
float listLength = sizeof(myNumbers) / sizeof(float);
|
|
float sumTwo = 0;
|
|
|
|
for (int i = 0; i < listLength; i++)
|
|
{
|
|
sumTwo = sumTwo + myNumbers[i];
|
|
}
|
|
|
|
float dividedTwo = sumTwo / listLength;
|
|
|
|
Serial.print("Version 2: ");
|
|
Serial.print((dividedTwo));
|
|
Serial.println();
|
|
|
|
Serial.end();
|
|
}
|
|
|
|
void loop() {}
|