0

So I'm new to C++ and am writing a program to calculate the mean, median, standard deviation, min, and max of a program. The issues I'm having are that I don't know how to correctly call functions from other files in the project, and when I try to print(c out <<) my array, it returns a weird value like 0 x 0017 c 530 (disregard the spaces). If anyone could help me correctly return these functions, and print the list correctly, I would be very grateful! Here's my code (not including the .h file):

#include <iostream>
using namespace std;
#include "stats.h"

int main()
{
    double nums[10000] = {};
    int n;

    cout << "Enter number of values: ";
    cin >> n;

    for (int i = 1; i <=n; i++) {
        cout << "Enter number " << i << ": ";
        cin >> nums[i-1];
    }

    // me trying to print the nu ms list
    cout << nums << endl;

    // me trying to call the function
    double mean(double nums[], int n);

    return 0;

}

stats. cpp

#include "stats.h"


double mean(double nums[], int n)
{
    double sum = 0;
    double average;

    for (int i = 0; i > n; i++) {
        sum += nums[i];
    }

    average = sum / n;

    return average;
}
songyuanyao
  • 169,198
  • 16
  • 310
  • 405
jnestor
  • 29
  • 5
  • 1
    The current answers haven't pointed out that your loop termination condition in `mean` is the wrong way around. You want to use `i < n`. – paddy Apr 07 '16 at 02:40

2 Answers2

0

Instead of

cout << nums << endl;

Just like you have a loop to enter one value at a time in an array, you also need a similar loop to print one value at a time from the array.

To call a function from another translation unit, you would typically declare the function in a header file, your stats.h would be an excellent candidate:

double mean(double nums[], int n);

And then just invoke it from your main:

std::cout << mean(nums, n) << std::endl;

That's it.

Also:

using namespace std;

You need to have someone help you to get an amnesia, and completely forget that C++ has anything like this. This is bad programming practice.

Community
  • 1
  • 1
Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148
  • Okay, so I've used std::cout << mean(nums, n) << std::endl; .... and it's working :)! But.. there's a still a problem. It isn't printing the average value into the command prompt. – jnestor Apr 07 '16 at 02:53
  • I have no idea what "not outputting into the command prompt" means. In any case, spend some time learning how to use your compiler's debugger, so that you can step through your code, one line at a time, examine the values of all variables, and see for yourself what your code is doing right, and what it's doing wrong. – Sam Varshavchik Apr 07 '16 at 02:59
0

double mean(double nums[], int n); is declaration of function, not invoking of function. You should

double mean_value = mean(nums, n);
cout << mean_value << endl;

And cout << nums << endl; won't print out the elements of the array, just the address of the array. You need to loop the array to print out all the elements.

songyuanyao
  • 169,198
  • 16
  • 310
  • 405