-2

I am working on the Edabit challenge: Get arithmetic mean of the given array. Now I have code like that:

#include <iostream>
int data;
using namespace std;
int mean(int data);
int main()
{
    int data[] = { 1, 2, 3, 4 };
    cout << mean(data);
}
int mean(int data)
{
    double mean = 0;
    for (int i = 0; i < sizeof(data) / sizeof(data[0]); i++)
    {
        mean += data[i];
    }
    mean /= sizeof(data) / sizeof(data[0]);
}

and I am stuck. I use Visual Studio 2019 on Windows 7 Professional, and I have underlined 3 characters ( data[i], and 2x data[0]). For this x Visual Studio says expression must have pointer-to-object type (Error E0142) and I have no idea what it means with this. I only know what pointer is.


In Visual studio I added the return statement, but while shortening the code here for publishing I forgot to add it. Otherwise, this wasn't the actual problem. Now I mustn't add it in the question because the comments would be wrong. The comments are related to the upper question, but my real question (for future readers stuck on this problem) is rather:

How to pass array as an argument in the function.

Now, that I am more proficient in C++, I know the terminology and how to state it, and I also know that this isn't so clear to a total beginner: you can't just write f(int[] arr) (you can't pass arrays), but you have to write f(int* arr) (you can pass a pointer that points on that array). At that time I couldn't just search it because I didn't know much of C++ terminology.

E_net4
  • 27,810
  • 13
  • 101
  • 139
User123
  • 476
  • 7
  • 22
  • 4
    It's always a good idea to learn from [a good textbook](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – R Sahu Feb 23 '20 at 20:05
  • The parmeter of `int mean(int data)` is a single integer, not an array. Also `sizeof` doesn't work on array parameters (see [this](https://stackoverflow.com/questions/1975128/why-isnt-the-size-of-an-array-parameter-the-same-as-within-main)), and you're missing a `return` in `mean`. – HolyBlackCat Feb 23 '20 at 20:07
  • @R Sahu Is there any textbook available on-line? Is learning from the textbook better than learning from mistakes and with small projects? – User123 Feb 23 '20 at 20:08
  • You should pass data array by reference to mean function to access its elements by indexing it. Return statement is also missing. – Ardahan Kisbet Feb 23 '20 at 20:08
  • @HolyBlackCat What type is an array with integers? Do I have to specify array like that: array int x? (Something like that ... But of course, not the way I used to do here.) – User123 Feb 23 '20 at 20:11
  • What's the reason for the global `data`? Don't use raw arrays, use `std::array` or `std::vector`. – Werner Henze Feb 23 '20 at 20:27
  • You can pass your array as a pointer, like this: `int mean(int* data) {` or if you don't want to change its value: `int mean(const int* data) {` – Farbod Ahmadian Feb 23 '20 at 20:30
  • Please don't fix errors relevant to the question in your code. It invalidates previous answers. I have reverted your last edits. – walnut Feb 23 '20 at 20:37
  • What is wrong with my question? Why is so downvoted? If I am a beginner in C++, then this is a problem in me and not in the question, so, please, when downvote, explain your reasons here. – User123 Feb 23 '20 at 20:40
  • @walnut I have edited because I posted the code right at the middle of debugging and I didn't find out. – User123 Feb 23 '20 at 20:44
  • @User123 You edited your question after ThomasMatthews posted his answer and your edits invalidate most of his points. When I first saw the question and hist answer it didn't make any sense to me and I had to look at the edit history to figure out what he meant. That is not ideal. – walnut Feb 23 '20 at 20:48
  • @walnut I get it now. Thank you. – User123 Feb 23 '20 at 20:49
  • I have changed a little bit, and now I haven't got any errors! But now program calculates mean not right way. – User123 Feb 23 '20 at 20:56
  • 1
    As for downvotes: Your code makes it seem as if you haven't even read the relevant chapters of a textbook or some other structured instructional material. Similarly there are *many* answers to be found on this site if you search for the error message. For these reasons the "does not show any research effort" reason for downvote seems to apply, imo. SO is not meant as a replacement for a good textbook or to teach you. Questions and answers should be helpful *to other readers* as well, see https://meta.stackoverflow.com/questions/252677/when-is-it-justifiable-to-downvote-a-question. – walnut Feb 23 '20 at 20:57
  • @walnut I had nice code until I haven't started debugging. And so I edited, but you rejected it (I know why). I was looking at various websites. – User123 Feb 23 '20 at 21:01
  • 4
    Then highly recommend that you stop looking at various websites and start reading a [good textbook](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) instead. It is well-known that most C++ tutorial sites out there are terrible. – walnut Feb 23 '20 at 21:04
  • @walnut *(About the last comment up there ...)* Thank you! I started to read C++ Primer and now I fully understand vectors. This was a really bad mistake that I started to learn advanced things, but I haven't understood some pretty basic things. Now I have gone to WxWidgets. I also appreciate the answer to this question (if there is anyone using WxWidgets): https://stackoverflow.com/questions/60866025/how-can-i-execute-the-function-when-button-in-wxyes-no-is-pressed. – User123 Mar 26 '20 at 11:44

2 Answers2

1

Your mean function, well, is mean.
1. It doesn't return a value; there is no return statement.
2. It uses variable name the same as the function (not a recommended coding style).
3. There is a global variable data that is hidden by a local variable data inside main.
4. You're confusing the compiler and the reader: the global data variable is a single int. The local variable in main is an array of int.

You should have the last line be:
return mean;

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
  • And how can I do array of int? I said at the beginning that I am a beginner. – User123 Feb 23 '20 at 20:33
  • You have an array of `int`: `int data[] = { 1, 2, 3, 4};`. The variable here is an array of int. The variable at the top of the code is not, but has the same name. – Thomas Matthews Feb 23 '20 at 20:38
  • And what is the difference? – User123 Feb 23 '20 at 20:42
  • Difference between arrays and single variables or difference between local and global variables? – Thomas Matthews Feb 23 '20 at 20:44
  • Between single and array, please. – User123 Feb 23 '20 at 20:45
  • Same difference between one egg and a carton of eggs. An `int` variable holds **only** one integer. An array is a *container* the holds more than one `int`. Also, with an array, the *slots* are contiguous in memory. Your `data` array in `main` contains 4 integers. The `int data` statement declares a variable, `data`, the contains **one** integer. The `int data[]` statement declares an array, many integers. – Thomas Matthews Feb 23 '20 at 20:49
  • Ok, I get the point (I know JavaScript, Python and C#, too), but I didn't know how can I define such an array in C++. – User123 Feb 23 '20 at 20:51
1

Arrays decay into pointers (an int* in this case) when passed as argument to functions. Your mean function only accepts one single int.

When the array decays into a pointer the size information is lost. You can however prevent that by defining a function that accepts arrays of the exact type and size you need. This can be done with templates:

#include <iostream>

template<typename T, size_t N>
double mean(const T (&data)[N]) {
    double sum = 0;
    for (size_t i = 0; i < N; ++i)
    {
        sum += data[i];
    }
    return sum / N;
}

int main()
{
    int input[] = { 1, 2, 3, 4, 5, 6, 7 };
    std::cout << mean(input) << '\n';
}

If you don't want to use templates (or only accept arrays of a certain size), you need to pass the size information on to the function manually:

#include <iostream>
#include <iterator> // std::size

double mean(const int* data, size_t N)
{
    double sum = 0;
    for (size_t i = 0; i < N; ++i)
    {
        sum += data[i];
    }
    return sum / N;
}

int main()
{
    int input[] = { 1, 2, 3, 4, 5, 6, 7 };
    std::cout << mean(input, std::size(input)) << '\n';
}
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108