Passing arrays to function

Hello everyone, I am trying to create an array of 200 random numbers and find the max value within specified upper and lower limits using a programmer defined functions. I get a debug error pop up on the screen which stops the program halfway through. The array is run but I cannot find the max. I'm wondering if I'm calling the function wrong. Below is the code I'm trying to run followed by the functions and library.

/*This program will create an array of random numbers between 500 and 0*/
/*This program will calculate the result of programmer defined functions*/
#include <iostream>
#include <cstdlib>
#include "Statfunctions.h"

using namespace std;

//Define variable names

int main()
{
int thearray[200], upper = 500, lower = 0, i;
for (i = 0; i <= 200; i++)
{
thearray[i] = rand() % (upper - lower + 1) + lower;
cout << thearray[i] << endl;
}
double max;
max = maxval(0, i);
}

-------------
#include <cmath>
using namespace std;

/*---------------------------------------------------------*/
/* This function returns the maximum */
/* value in teh array x with n elements. */

double maxval(const double x[], int n)
{
// Declare local objects.
double maxVal;

// Determine maximum value in the array.
maxVal = x[0];
for (int k = 1; k<n; ++k)
{
if (x[k] > maxVal)
maxVal = x[k];
}

// Return maximum value.
return maxVal;
}
/*---------------------------------------------------------*/
/* This function returns the minimum */
/* value in teh array x with n elements. */

double minval(const double x[], int n)
{
// Declare local objects.
double min_x;

// Determine minimum value in the array.
min_x = x[0];
for (int k = 1; k <= n - 1; ++k)
{
if (x[k] < min_x)
min_x = x[k];
}

// Return minimum value.
return min_x;
}
/*---------------------------------------------------------*/
/* This function returns the average or */
/* mean value of an array with n elements. */

double mean(const double x[], int n)
{
// Declare and initialize objects.
double sum(0);

// Determine mean value.
for (int k = 0; k<n; ++k)
{
sum += x[k];
}

// Return mean value.
return sum / n;
}
/*---------------------------------------------------------*/
/* This function returns the median */
/* value in an array x with n elements. */
/* The values in x are assumed to be ordered. */

double median(const double x[], int n)
{
// Declare objects.
double median_x;
int k;

// Determine median value.
k = floor(double(n) / 2);
if (n % 2 != 0)
median_x = x[k];
else
median_x = (x[k - 1] + x[k]) / 2;

// Return median value.
return median_x;
}
/*---------------------------------------------------------*/
/* This function returns the variance */
/* of an array with n elements. */

// Function prototype.
double mean(const double x[], int n);

double variance(const double x[], int n) // Function header.
{
// Declare objects.
double sum(0), mu;

// Determine variance.
mu = mean(x, n);
for (int k = 0; k<n; ++k)
{
sum += (x[k] - mu)*(x[k] - mu);
}

// Return variance.
return sum / (n - 1);
}
/*---------------------------------------------------------*/
/* This function returns the standard deviation */
/* of an array with n elements. */

// Function prototype.
double variance(const double x[], int n);

double std_dev(const double x[], int n) // Function header.
{

// Return standard deviation.
return sqrt(variance(x, n));
}
/*---------------------------------------------------------*/

-------------------
#include <cmath>
double maxval(const double x[], int n);
double minval(const double x[], int n);
double mean(const double x[], int n);
double median(const double x[], int n);
double variance(const double x[], int n);
double std_dev(const double x[], int n);
1
2
3
4
//Function is defined as
double maxval(const double x[], int n)
//You calling it as
max = maxval(0, i);

That is the problem.
Ok, well, how do I call the function then because nothing I've tried works...
You've defined the function to take an array as its first argument. Why not try, y'know, passing in an array? Instead of 0, which will be interpreted as a NULL pointer.
Last edited on
Topic archived. No new replies allowed.