-2

My code is about getting numbers from the user and finding the mean of the combined numbers. What I have done is create function called mean that does exactly that. In my main code I have the array and the a variable which ask the user before hand how many numbers they are going to input. I then use that variable in a for loop. I finally end the code with calling the function and dividing it by the number of entries which was given in the begging of the code as stated above. My problem is why isn't the user entries not going into the array.

#include <iostream>
using namespace std;
float mean(float x, float y...) {
  float answer = (x + y);
  return answer;
}
int main() {
  int ask;
  float numbers[] = {};
  cout << "how many number do you want to add";
  cin >> ask;
  for (int i = 0; ask; i++) {
    cout << "please enter your" << i << "number";
    cin >> numbers
  }
  cout << mean(numbers) / ask
}
user475
  • 1
  • 4
  • 1
    Does your code compile? Have you got any textbooks? – prehistoricpenguin Jul 27 '21 at 02:37
  • Hint: make the array *after* asking for the number of elements – Brady Dean Jul 27 '21 at 03:04
  • there are many missing `;`s in your code. And there are so many basic errors. Please learn how to code properly from a book [The Definitive C++ Book Guide and List](https://stackoverflow.com/q/388242/995714). StackOverflow isn't a good place to learn basic things – phuclv Jul 27 '21 at 04:47

1 Answers1

0

Your code does not compile. The code has many errors, you are missing alot of commas. I believe you should use a vector of floats not array of floats because you don't know the size of the array before you ask for it. Also

float mean(float x, float y...) {
  float answer = (x + y);
  return answer;
}

doesn't actually add anything together. the function name is also confusing because you aren't getting the mean only the sum. I would use accumulate to add up every element in the vector easily.

This is something that does exactly what you want except it doesn't use arrays.

#include <iostream>
#include <vector>
#include <numeric>   
using namespace std;
float sum(vector<float> &v) 
{
    int initial_sum  = 0; 
    return accumulate(v.begin(), v.end(), initial_sum);
}
int main() {
  int ask;
  cout << "how many number do you want to add";
  cin >> ask;
  vector<float> numbers;
  for (int i = 0; i < ask; i++) {
    cout << "please enter your" << i << ": number";
    float x;
    cin >> x;
    numbers.push_back(x);
  }
  cout << sum(numbers) / ask;
}

Here is a solution using arrays.

#include <iostream>
#include <numeric>   
using namespace std;
float sum(float numbers[], int n) 
{
    int initial_sum  = 0; 
    return accumulate(numbers,numbers+n, initial_sum);
}
int main() {
  int ask;
  cout << "how many number do you want to add";
  cin >> ask;
  float numbers[ask];
  for (int i = 0; i < ask; i++) {
    cout << "please enter your" << i << ": number";
    cin >> numbers[i];
  }
  cout << sum(numbers, ask) / ask;
}
Snake
  • 113
  • 2
  • 10
  • im still in a beginner course where we didn't take vectors. why cant i just give the size of the array 100. i also switched the name to sum and have my last line divide the sum by the number of numbers. – user475 Jul 27 '21 at 02:58
  • you actually don't need an size of 100, You can just declare the array with size of ask after you ask for the number of numbers you want to add. – Snake Jul 27 '21 at 03:04
  • you can't cin >> numbers because you are inputting a float or integer into an array of floats you should do something like cin >> numbers[i]; – Snake Jul 27 '21 at 03:06
  • #include using namespace std; float sum(float x, float y...) { float answer = (x + y); return answer; } int main() { int ask; float numbers[] = {}; cout << "how many number do you want to add"; cin >> ask; for (int i = 0; ask; i++) { cout << "please enter your" << i << "number"; cin >> numbers [i]; } cout< – user475 Jul 27 '21 at 03:14
  • your function is not recognized because ... is not a parameter and you are only putting in numbers the array not any floats. – Snake Jul 27 '21 at 03:16
  • how do i make it parameter – user475 Jul 27 '21 at 03:20
  • im sorry im just exhausted because im stuck on this for three days – user475 Jul 27 '21 at 03:24
  • you make you array of floats a parameter by putting float numbers[] inside the function parameters like float sum(float numbers[], int n) . Int n is a parameter that holds how many numbers are inside the array of floats. – Snake Jul 27 '21 at 03:25
  • This explains parameters and functions pretty well- https://www.cplusplus.com/doc/tutorial/functions/ – Snake Jul 27 '21 at 03:26
  • #include using namespace std; float sum(float x, float y...) { float answer = (x + y); return answer; } int main() { int ask; float numbers[] = {}; cout << "how many number do you want to add"; cin >> ask; for (int i = 0; ask; i++) { cout << "please enter your" << i << "number"; cin >> numbers [i]; } cout< – user475 Jul 27 '21 at 03:32
  • Your function is this float sum(float x, float y...), my function is this float sum(float numbers[], int n). The compiler can't convert a array of floats into individual float parameters. You need a corresponding parameter for every variable or array you pass into the function. – Snake Jul 27 '21 at 03:34