So I have this simple header file and when I was creating sum of the functions to be contained in it. I have an error that repeats whenever I access the array x[]. Wherever I try to access the array it says that the expression must have a class type. I am completely lost on a fix for it.
useful.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#pragma once
#include <array>
double avg(int x[])
{
int sum = 0;
for (int i = 0; i < x.size(); i++)
sum += x[i];
return sum / x.size();
}
double avg(float x[])
{
double sum = 0;
for (int i = 0; i < x.size(); i++)
sum += x[i];
return sum / x.size();
}
For normal arrays, you must have an additional "size" parameter for each your function :
1 2 3 4 5 6 7 8 9 10 11 12 13 14
double avg(int x[], int size)
{
int sum = 0;
for (int i = 0; i < size; i++)
sum += x[i];
return sum / size;
}
double avg(float x[], int size)
{
double sum = 0;
for (int i = 0; i < size; i++)
sum += x[i];
return sum / size;
}