Finding average of hardcoded values

I am trying to wrote a code to find the average of hardcoded values without hardcoding the number of vaules. This is the code i have but it gives me the average of the vaules to be 20 which is incorrect.

int a = 4;

int b = 6;

int c = 2;

int d = 8;

int num={a, b, c, d};

int numAVG = (a + b + c + d);

void setup()
{
Serial.begin(9600);
Serial.println(numAVG);
}

void loop()
{
for (int i = 0; i < numAVG ; i++)
{
Serial.println(numAVG);
}
}

Your code is working fine. You need to finish the equation: int numAVG = ((a + b + c + d)/4);

What & why are you doing this ?
At first glance, it appears pointless, unless it’s simply a learning exercise.

its supposed to be a learning excersize. Im not supposed to hardcode a 4. I tried int numAVG = ((a+b+c+d)/ sizeof (num)) but i just got a 2.

Check the sizeof(num)...
You may be surprised.

i dont understand what you mean by check the sizeof(num)

Have a look at the reference page for sizeof(), particularly the "Notes and Warnings" section.

I wonder what in the world are they trying to teach with an assignment like that?

-jim lee

fdrwhy:
i dont understand what you mean by check the sizeof(num)

“check” as in “verify that the value is what you expect it to be”

Please remember to use code tags when posting code

I would declare an array of values, use a loop to add up the values, and then use the macro

#define ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0]))

to give me the number of elements of the array.

That should be enough of a hint for you to work it out.

@fdrwhy, sorry if we seem a bit obtuse, but we’re trying to teach you what’s going on... not ‘tell’ you.

sizeof() could be confusing for a beginner, because the language can be used on many different processor architectures - the beauty of a high-level language, but you really need to understand what the instructions ‘do’... not just .print(), or .begin()

How big is an int() on your processor? How big is a byte()...? floats, longs... are all different lengths on different types of cpu (well almost).

Extra points...
Is an int the same ‘size’ as an unsigned int ?
What’s the difference ?

fdrwhy:
its supposed to be a learning excersize. Im not supposed to hardcode a 4. I tried int numAVG = ((a+b+c+d)/ sizeof (num)) but i just got a 2.

The number of elements in an array is obtained like this:

sizeof(num)/sizeof(num[0])

as in

#define arraycount(a) (sizeof(a) / sizeof(a[0]))

My take on the problem:

int a = 4;
int b = 6;
int c = 2;
int d = 8;

int num[]={a, b, c, d};

int numAVG = 5;  // hard code the average! 

// you could do this if you define the macro arraycount:
int numAVG = (a+b+c+d)/arraycount(num) ;

But its probably fragile as when you later change the code you've got to remember
to update num and numAVG in step.

The easiest solution is probably to write a function that computes the average of an array of N elements:

template <size_t N>
int average(const int (&numbers)[N]) {
    long sum = 0;
    for (int number : numbers)
        sum += number;
    long rounding = sum >= 0 ? (N / 2) : -(N / 2);
    return (sum + rounding) / long(N);
}

int numbers[] = {-1, -2, -3, -4, -5, -6, -7, -8, -9, -10};
int average_number = average(numbers);

Live demo: Compiler Explorer

The compiler will automatically update the average when you add or remove elements from the array.

Pieter

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