Printing out a Polynomial that is Double Wrapped inside of a Class - c++

I've been tasked with creating a C++ class called Polynomial that applies different mathematical formulas to the Polynomial. I was given 4 separate bits of code that are a requirement to use along with the rest of my program and I don't quite understand how I am suppose go about using these wrappers throughout my functions. Such as in my print function and accessing the polynomial hidden multiple layers in.
Premade 1:
#ifndef __GUARDED_ARRAY_H__
#define __GUARDED_ARRAY_H__
using namespace std;
typedef int ItemType;
const unsigned MAX_LENGTH = 500;
//
// GuardedArray
// - A wrapper class for C++ arrays to make array access safe.
// Specifically, initialization is guaranteed, and assertions are
// in place to detect array index out of bound errors in array member
// accesses.
//
class GuardedArray {
public:
// Purpose: Initializes array elements to zeros
GuardedArray();
// Purpose: Initializes all array elements to a given value
GuardedArray(ItemType x);
// Purpose: Read array element at index i
ItemType read(unsigned i) const;
// Purpose: Write x into array element at index i.
void write(unsigned i, ItemType x);
private:
ItemType array[MAX_LENGTH];
};
#endif
Premade 2:
#include <cassert>
#include "polynomial.h"
#include "guarded_array.h"
#include "managed_array.h"
GuardedArray::GuardedArray() {
for (unsigned i = 0; i < MAX_LENGTH; i++)
array[i] = 0;
}
GuardedArray::GuardedArray(ItemType x) {
for (unsigned i = 0; i < MAX_LENGTH; i++)
array[i] = x;
}
ItemType GuardedArray::read(unsigned i) const {
assert(i < MAX_LENGTH);
return array[i];
}
void GuardedArray::write(unsigned i, ItemType x) {
assert(i < MAX_LENGTH);
array[i] = x;
}
Premade 3:
#ifndef __MANAGED_ARRAY_H__
#define __MANAGED_ARRAY_H__
#include "guarded_array.h"
using namespace std;
//
// ManagedArray
// - A wrapper class for C++ arrays to facilitate insertion and removal of
// array elements.
// - Every instance of ManagedArray has a size that can be increased
// until the maximum capacity MAX_LENGTH is reached.
//
class ManagedArray {
public:
// Purpose: Initializes array to have zero size.
ManagedArray();
// Purpose: Initializes array to a given size. All array elements
ManagedArray(unsigned N);
// Purpose: Initializes array to a given size. All array elements
// are initialized to x.
ManagedArray(unsigned N, ItemType x);
// Purpose: Return the current size of the array.
unsigned size() const;
// Purpose: Read array element at index i
ItemType read(unsigned i) const;
// Purpose: Write x into array element at index i.
void write(unsigned i, ItemType x);
// Purpose: Insert an element into the array.
void insert(unsigned i, ItemType x);
// Purpose: Remove an element from the array.
void remove(unsigned i);
private:
unsigned count;
GuardedArray array;
};
#endif
Premade 4:
#include <cassert>
#include "polynomial.h"
#include "guarded_array.h"
#include "managed_array.h"
ManagedArray::ManagedArray() : array() {
count = 0;
}
ManagedArray::ManagedArray(unsigned N) : array() {
assert(N <= MAX_LENGTH);
count = N;
}
ManagedArray::ManagedArray(unsigned N, ItemType x) : array(x) {
assert(N <= MAX_LENGTH);
count = N;
}
unsigned ManagedArray::size() const {
return count;
}
ItemType ManagedArray::read(unsigned i) const {
assert(i < count);
return array.read(i);
}
void ManagedArray::write(unsigned i, ItemType x) {
assert(i < count);
array.write(i, x);
}
void ManagedArray::insert(unsigned i, ItemType x) {
assert(i <= count && count < MAX_LENGTH);
for (unsigned j = count; j > i; j--)
array.write(j, array.read(j - 1));
array.write(i, x);
count++;
}
void ManagedArray::remove(unsigned i) {
assert(i < count && count > 0);
for (unsigned j = i; j < count - 1; j++)
array.write(j, array.read(j + 1));
count--;
}
And this here is my own code that must be altered to accommodate for the above code:
#include "polynomial.h"
using namespace std;
int main()
{
Polynomial poly1;
poly1.print();
/* Calculate all of these:
The zero polynomial.
The degree of the zero polynomial.
The value of the zero polynomial when x = 1.
The polynomial P(x) = -1 + 3x^2 - 2x^5.
The degree of P(x).
The value of P(1) and P(-2).
The polynomial Q(x) = 1 + x^3 + 2x^5.
The polynomial P(x) +Q(x).
The polynomial P(x) - Q(x).
The polynomial 4P(x).
The polynomial x^2Q(x).
The polynomial P(x)Q(x).
*/
system("pause");
return 0;
}
Header:
#ifndef POLYNOMIAL_H
#define POLYNOMIAL_H
#include <iostream>
#include <cassert>
#include <iomanip>
#include "managed_array.h"
using namespace std;
//
// Polynomial class
//
//Purpose- To hold and calculate specific polynomial equations
//
//Parameter(s)-
// -None OR
// -An array of coefficients and the size of the array OR
// -An instance of Polynomial to copy into a new instance
//Precondition(s)- In the array/array size constructor the last element of the array must be non-zero
//
//Returns- N/A
//
//Side Effect- Holds all functions required to intialize, hold, and calculate specific polynomial equations
//
//
class Polynomial
{
private:
ManagedArray polynomial;
public:
Polynomial(); //Default constructor
/*
second constructor takes an integer array and an integer array size as arguments, and initializes the target Polynomial
instance with coefficients identical to the elements of the array
A precondition for this operation is that the last element of the array must be non-zero, or else the array size is zero.
*/
Polynomial(int array[], const unsigned int size);
/*
A third constructor expects a Polynomial instance as its sole argument, and initializes the target Polynomial
instance with coefficients identical to those in the argument Polynomial.
*/
//Polynomial();
//Purpose- Apply the Polynomial function to an integer argument.That is, compute the value of the Polynomial for a given value of x.
void evaluate();
//Purpose- An arithmetic addition operation that adds one instance of Polynomial to another.
void add(const Polynomial& poly2);
//Purpose- An arithmetic subtraction operation that subtracts one instance of Polynomial by another.
void subtract();
//Purpose- An arithmetic multiplication operation that multiplies one instance of Polynomial by another.
void multiply();
//Purpose- An arithmetic multiplication operation that multiplies an instance of Polynomial by an integer.
//void multiply();
//Purpose- An arithmetic multiplication operation that multiplies an instance of Polynomial by a polynomial of the form x^k for some non-negative integer k
void raiseDegree();
//Purpose- A Boolean operation that compares two instances of Polynomial to determine if they are identical.
bool equal();
//Purpose- Get the degree of a Polynomial instance.
int getDegree();
//Purpose- Retrieve the coefficient of the term x^k in a Polynomial instance, given a non-negative integer k.
int getCoefficient();
//Purpose- Print a Polynomial instance in a user-friendly format.
void print() const;
//The polynomial (-3 + 4x - 7x^3) should be printed in the following format.- 3 + 4 x - 7 x^3
};
#endif
cpp:
#include "polynomial.h"
#include "managed_array.h"
#include "guarded_array.h"
unsigned int i;
Polynomial::Polynomial()
{
ManagedArray();
GuardedArray();
}
Polynomial::Polynomial(int array[], const unsigned int size)
{
ManagedArray(size);
for (i = 0; i < size; i++)
{
int temp = array[i];
GuardedArray(temp);
}
}
//Polynomial::Polynomial()
//{
//
//}
void Polynomial::evaluate()
{
}
void Polynomial::add(const Polynomial& poly2)
{
}
void Polynomial::subtract()
{
}
void Polynomial::multiply()
{
}
//void Polynomial::multiply()
//{
//
//}
void Polynomial::raiseDegree()
{
}
bool Polynomial::equal()
{
return false;
}
int Polynomial::getDegree()
{
return -1;
}
int Polynomial::getCoefficient()
{
return -1;
}
void Polynomial::print() const
{
}

You only need to deal with data member polynomial in class Polynomial, which represents the coefficients of the polynomial.
For example, polynomial.read(2) should return the coefficient of the term x^2
To get you started, in polynomial.cpp
Polynomial::Polynomial()
:polynomial() {}
Polynomial::Polynomial(int array[], const int size)
:polynomial(size)
{
for (int i = 0; i < size; ++i)
polynomial.write(i, array[i]);
}
double Polynomial::evaluate(double x) const
{
double sum = 0;
for (int i = 0; i < polynomial.size(); ++i)
sum += polynomial.read(i) * pow(x, i);
return sum;
}

Related

Matrix - return and pass as parameter c++

I'm trying to use clear functions to do a matrix multiplication with random generated values. Therefore I'm hoping to use a function(mat_def) to generate the matrices and another function(mat_mul) to multiply them when the matrices are sent as parameters.
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
double mat_def(int n) //how to return the matrix
{
double a[n][n];
double f;
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
{
f= rand();
cout<<f ;
a[i][j]=f;
}
}
return 0;
}
double mat_mul( int n, double a[n][n], double b[n][n]) //how to send matrix as parameter
{
return 0;
}
int main()
{
/* initialize random seed: */
srand (time(NULL));
mat_def(10);
}
Here's a nice, standard C++ Matrix template for you.
Matrix.h
#include <vector>
class Matrix
{
class InnerM
{
private:
int ydim;
double* values;
public:
InnerM(int y) : ydim(y)
{
values = new double[y];
}
double& operator[](int y)
{
return values[y];
}
};
private:
int xdim;
int ydim;
std::vector<InnerM> inner;
public:
Matrix(int x, int y) : xdim(x), ydim(y), inner(xdim, InnerM(ydim))
{
}
InnerM& operator[](int x)
{
return inner[x];
}
};
All the memory leaks are there for you but you get the idea. From here you can handle the multiplication by overiding ::operator*() in the Matrix class.
I assume your problem is to define 2-D array and then pass it to mat_mul function to multiply the matrices. And the rest will be quite simple.
Defining the 2-D array(considering memory needs are known at run time):
int rows,cols;
cin >> rows;
cin >> cols;
int **arr = new int*[rows]; // rows X cols 2D-array
for(int i = 0; i < rows; ++i) {
arr[i] = new int[cols];
}
You can define another 2-D array exactly the same way with required rows and column.
now, Passing the 2-D array to function:
void mat_mul(int **arr1, int **arr2, int m, int n, int p, int q){
//define a 2-D array to store the result
//do the multiplication operation
//you could store the result in one of the two arrays
//so that you don't have to return it
//or else the return type should be modified to return the 2-D array
}
example:
void display(int **arr, int row, int col){
for (int i=0; i<row; i++){
for(int j=0;j<col; j++){
cout << arr[i][j] << '\t';
}
cout << endl;
}
}
Delete the memory if not required anymore with the following syntax:
for(int i=0; i<rows; i++){
delete[] array[i];
}
delete[] array;
hope this will be sufficient to get your work done!
there is already an answer on how to return a 2-D array on SO. Check the link below.
https://stackoverflow.com/a/8618617/8038009
Returning the raw allocation is a sucker bet. You need to manage all of the memory allocated yourself and pass it around with the matrix size parameters.
Why suffer? Use a matrix class
template<class Type>
class Matrix{
int rows;
int cols;
std::vector<type> data;
public:
Matrix(int row, int col):rows(row), cols(col), data(rows*cols)
{
// does nothing. All of the heavy lifting was in the initializer
}
// std::vector eliminates the need for destructor, assignment operators, and copy
//and move constructors.
//add a convenience method for easy access to the vector
type & operator()(size_t row, size_t col)
{
return data[row*cols+col];
}
type operator()(size_t row, size_t col) const
{
return data[row*cols+col];
}
};
Usage would be
Matrix<double> mat_mul(const Matrix<double> &a, const Matrix<double> &b)
{
Matrix<double> result;
// do multiplication
return result;
}
int main()
{
/* initialize random seed: */
srand (time(NULL));
Matrix<double> matA(10, 10);
matA(0,0) = 3.14; // sample assignment
matA(9,9) = 2.78;
double x = matA(0,0) * matA(9,9)
Matrix<double> matB(10, 10);
Matrix<double> matC = mat_mul(matA, matB) ;
}
More functionality, such as construction from an initializer list, can be added to the class to make your life easier. You can also specify an operator * overload for Matrix and use that in place of mat_mul if you chose. Read Operator overloading for more on that option.

Hi, Just Want to Know What This Error Means

"error C2660: 'storeInitialValues' : function does not take 1 arguments" shows up in the log of my code when I try to build. I've looked at some past errors posted here and I think it might be some kind of initialization error with either/all the usersize, v, dsize, and/or asize. I just want to see the error on the specific calling of storeInitialValues(usersize, v, dsize, asize); that's it. Thank you very much in advance.
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <ctime>
#include <cstdlib>
using namespace std;
struct vec
{
};
struct arr
{
};
void fillArray(int A[], int size);
void storeInitialValues(int * & arr, int & asize, int & dsize, vector<int>& v, int & usersize);
int main()
{
int usersize, dsize, asize;
vector <int> v;
int * ptr = new int[10];
cout << "How many values in data structures? Please enter values greater than 20." << endl;
cin >> usersize;
while (usersize < 21)
{
cout << "Error, enter values greater than 20!" << endl;
cin >> usersize;
}
cout << "Alright, here are your numbers: " << endl;
storeInitialValues(usersize, v, dsize, asize);
}
// fillArray stores sequential, unique, integer values into an array and
// then randomizes their order
void fillArray(int A[], int size)
{
srand((int)time(0));
for (int i = 0; i < size; i++)
{
A[i] = i + 1;
}
for (int k = size - 1; k>1; k--)
{
swap(A[k], A[rand() % k]);
}
}
// storeInitialValues calls fillArray to produce an array of unique randomly
// organized values and then inserts those values into a dynamically sized
// array and a vector.
void storeInitialValues(int * & arr, int & asize, int & dsize, vector<int>& v, int usersize)
{
int * temp = new int[usersize]; // temporary array for randomized data
fillArray(temp, usersize); // get data
for (int i = 0; i < usersize; i++) // copy data into the dynamic data structures
{
add(arr, asize, dsize, temp[i]);
v.push_back(temp[i]);
}
delete[] temp; // clean up temporary pointer
temp = NULL;
}
void add(int & usersize, int & arr, int & dsize, int & temp[i])
{
}
void remove()
{
}
Nothing about your call to storeInitialValues matches the declaration. I think you might be confused thinking the names of the variables are important. That's not the case. You have to pass variables that match the type of the variables in the function declaration in the correct order, the name are irrelevant.
int * & arr is a very strange declaration. int *arr would be a pointer to an int that you could treat as an array. What exactly are you aiming for with int * &? Mixing * and & requires that you be very careful with your usage. But you are also using vector, which is a very safe way of dealing with arrays. Why not just use vectors? You also declare and allocate ptr in the main function but you don't use it nor do you delete it.

Why is my random generation of numbers input all zeros?

I have a function template that is suppose to take a vector and produce random numbers inside it.
However, when I print entire vector, its all zeros. This method works for arrays however.
code:
#include <vector>
template<class T>
class RandomGenerator
{
public:
// function template for generating random numbers
void genRand(T data[], int size)
{
for (int i = 0; i < size; i++)
{
data[i] = (1 + rand() % size);
}
}
void genRand(std::vector<T> data, int size)
{
genRand(&data[0], size);
}
};
You take the vector by value, thus the argument won't be changed. Take it by reference:
void genRand(std::vector<T>& data, int size)
// ^

Multiplying arrays

How can I get the Matrix overloaded constructor to combine Vector a and Vector b to form a Matrix object as an outer product. And do the same for Vector c and Vector d. The problem is with overloaded constructor and being able to use that create a matrix. At the moment it can only use Vector a and Vector b when it needs to use each. The print member function needs to print the Matrix from the users input values.
#include <iostream>
using namespace std;
const int rows=3;
const int columns=3;
const int elements=3;
class Vector{
private:
double data[elements];
public:
Vector();
void read();
double get_element(int);
};
Vector::Vector(){
int i=0;
while(i<elements){data[i++]=0;}
}
void Vector::read(){
int j=0;
cout<<"Enter "<<elements<<" elements of vector "<<endl;
while(j<elements){cin>>data[j++];}
}
double Vector:: get_element(int n){
while(n<elements)
return data[n];
}
Vector a,b,c,d;
class Matrix {
private:
double data [rows*columns];
public:
Matrix(Vector &, Vector &);
void add (const Matrix &);
void mult (double);
double trace();
double norm();
void print ();
};
Matrix::Matrix(Vector &, Vector &){
int d,f;
for (d=0; d<rows; d++){
for (f=0; f<columns;f++){
data[d*f]=a.get_element(d)*b.get_element(f);
}
}
}
Matrix A (a, b);
Matrix B (c, d);
void Matrix::print(){
cout.setf(ios::showpoint|ios::fixed);
cout.precision(3);
for (int i=0; i<rows; i++) {
cout << endl;
for (int j=0; j<columns; j++) {
cout << " " << data[i*j];
}
}
}
First thing is first, get rid of the using namespace std. You don't want to import all of std into your class just to use cout and cin.
Now lets get to the basics. You have a class which is supposed to encapsulate/hold data of its own. This class can be passed around and be worked on. Each class has its own unique set of data for your vector/matrix class.
In the above, you have your classes using global variables. This is bad because every vector will have the same exact data. They all share the same variables (global)!
Thus we need to put somehow get Vector to contain its own data. We do this by putting the variable inside the vector class itself. We make it private so that it cannot be accessed outside of the class.
Next, we need a way to initialize data. We cannot give it elements anymore because elements is no longer going to be global and constant. Thus we must now dynamically allocate elements amount of doubles in the constructor and delete it in the destructor.
To prevent weird behavior for now, we disallow copying and assigning. You should REALLY take look at a tutorial on classes and encapsulation.. This answer will be pretty incomplete but should help somewhat and fix a couple things..
If you are 100% sure that vector MUST ONLY have 3 elements, nothing more, nothing less, then you may change double* data to double data[3] and remove new data[...] from the constructor and delete[] data from the destructor.
#include <iostream>
class Vector
{
private:
double* data; //will point to an array of elements.
int elements; //amount of elements this vector has.
Vector(const Vector& other); //copying not allowed.
Vector& operator = (const Vector& other); //copy assignment not allowed.
public:
Vector(int elements); //constructor that tells us how large of an array we need.
~Vector(); //destructor to delete the dynamically allocated array when done.
int size() const; //returns the amount of elements.
double get_element(int n) const;
void set_element(double value, int index);
};
//This is our constructor. It stores the amount of elements we allocated within our class.
//It also initialises data to point to the `new` array.
Vector::Vector(int elements_) : elements(elements_), data(new double[elements_]())
{
}
Vector::~Vector()
{
delete[] data; //before the class gets destroyed, we clean up our dynamically allocated array. ALWAYS!
}
double Vector::get_element(int n) const
{
return data[n];
}
void Vector::set_element(double value, int index)
{
data[index] = value;
}
int Vector::size() const
{
return elements;
}
/** We do the same for the matrix class.**/
class Matrix
{
private:
double* data;
int rows, columns; //instead of elements, we have rows and columns.
Matrix (const Matrix &other); //prevent copying.
Matrix& operator = (const Matrix &other); //prevent assignment.
public:
Matrix(const Vector &a, const Vector &b); //constructor takes TWO vectors.
~Matrix();
void add (const Matrix&);
void mult (double);
double trace();
double norm();
void print ();
};
/** Data is initialized to an array of doubles[rows * columns] **/
/** We also store the amount of rows and columns allocated **/
Matrix::Matrix(const Vector &a, const Vector &b) : data(new double[a.size() * b.size()]), rows(a.size()), columns(b.size())
{
int d, f;
for (d = 0; d < rows; d++)
{
for (f = 0; f < columns; f++)
{
data[d * f] = a.get_element(d) * b.get_element(f);
}
}
}
Matrix::~Matrix()
{
delete[] data; //Before the class is destroyed, we must delete the array.
}
void Matrix::print()
{
std::cout.setf(std::ios::showpoint | std::ios::fixed);
std::cout.precision(3);
for (int i = 0; i < rows; i++)
{
std::cout << std::endl;
for (int j = 0; j < columns; j++)
{
std::cout << " " << data[i * j];
}
}
}
int main()
{
Vector a(3); //Now we can a vector.
Vector b(3); //another vector.
Vector c(2); //notice we can choose how many elements the vector can hold.. 2 elements = a point. Nevertheless..
a.set_element(5.0d, 0);
b.set_element(10.0d, 2);
Matrix m(a, b); //Create a matrix from our two vectors.
m.print(); //Print it..
}
As said before, if you are 100% sure that all matrices are 3x3 and all vectors have 3 elements only, then the following is what the class would look like:
#include <iostream>
class Vector
{
private:
static const int elements = 3; //static. All vector instances will have the same amount of elements.
double data[elements]; //stack allocation. 3 elements only.
Vector(const Vector& other);
Vector& operator = (const Vector& other);
public:
Vector();
~Vector();
void read();
int size() const;
double get_element(int n) const;
};
Vector::Vector() : data() {} //nothing in the constructor. data is filled with 0's.
Vector::~Vector() {} //nothing to delete in the destructor..
double Vector::get_element(int n) const
{
return data[n];
}
int Vector::size() const
{
return elements;
}
class Matrix
{
private:
static const int rows = 3, columns = 3; //all matrix instances will have 3 rows, 3 columns.
double data[rows * columns];
Matrix (const Matrix &other);
Matrix& operator = (const Matrix &other);
public:
Matrix(const Vector &a, const Vector &b);
~Matrix();
void add (const Matrix&);
void mult (double);
double trace();
double norm();
void print ();
};
Matrix::Matrix(const Vector &a, const Vector &b) //nothing to allocate in the constructor.
{
for (int d = 0; d < rows; d++)
{
for (int f = 0; f < columns; f++)
{
data[d * f] = a.get_element(d) * b.get_element(f);
}
}
}
Matrix::~Matrix() {} //nothing to delete in the destructor.
void Matrix::print()
{
std::cout.setf(std::ios::showpoint | std::ios::fixed);
std::cout.precision(3);
for (int i = 0; i < rows; i++)
{
std::cout << std::endl;
for (int j = 0; j < columns; j++)
{
std::cout << " " << data[i * j];
}
}
}
int main()
{
Vector a;
Vector b;
Matrix m(a, b);
m.print();
}

Returning an integer array

This is one the methods that I have to make for my assignment
int*mode(int &) – returns an integer array containing the modal value(s) of the
population, the reference parameter should be set to the size of the result
But from searching, you cannot return array's from a function? I can calculate modal value from a given array, put it in an array, but return an integer array? maybe the professor meant something else? I know how to do all other methods except that one. I don't even know what that method means (Coming from java)
Header File
#include <string>
using namespace std;
class population
{
public:
//Default Constructor
population(void);
//Constructor that accepts an integer array object, and the size for that array object
population(int[], int);
//Constructor for creating a deep copy
population (const population &);
//For overloading purposes
population & operator = (const population &);
//Destructor that frees dynamic memory associated with the underlying array
~population(void);
//Method for loading new content into an array
void load(string);
//Method for adding new content into existing array
void add(string);
//Accessors
//Returns the size of the population (The number of values stored in the array)
int size();
//Returns the sum of the popluation (The sum of the contents in the array)
int sum();
//Returns the mean of the population
float mean();
//Returns the median of the population
float median();
//Returns the standard deviation of the population
float stddev();
//Returns an integer array containing the modal values of the popluation
int*mode(int &);
private:
int arraySize;
bool sorted;
int * popArray;
};
CPP
#include "population.h"
//Default Constructor
population::population(void)
{
}
//Constructor to intialize the population object
population::population(int arr[], int s)
{
//Store s into a variable
arraySize = s;
//Declare popArray as a Dynamic array
popArray = new int[arraySize];
//Copy the passed array into the popArray
for ( int i=0; i < s; i++)
{
popArray[i] = arr[i];
};
}
//Constructor for deep copying purposes
population::population (const population & p)
{
}
population::~population(void)
{
}
int population::size(void)
{
//Return size of the array, which is the amount of population in the array.
return arraySize;
}
int population::sum(void)
{
//Variable to hold sum of the array
int sumArray = 0;
//Add all the contents of the array into one variable
for ( int i = 0; i < arraySize; i++)
{
sumArray += popArray[i];
}
//Return the sum of the array
return sumArray;
}
float population::mean(void)
{
//Variable to hold sum and the mean of the array
int sumArray = 0;
float meanArray;
//Add all the contents of the array into one variable
for ( int i=0; i < arraySize; i++)
{
sumArray += popArray[i];
}
//Sum of the array divided by number of contents in the array (Average)
meanArray = (sumArray / arraySize);
//Returns mean value in type float
return meanArray;
}
float population::median ()
{
return 1;
}
float population::stddev()
{
return 1;
}
int population::*mode(int & i)
{
return
}
With a prototype like that, I'm guessing that he wants you to new an array to be returned:
int *population::mode(int & i)
{
// compute the number of modal values you need to return
i = /* whatever the size of the return array will be */;
int * ret = new int[i];
// fill in ret
return ret;
}
Try:
int foo(int arrayBar[])
or
int foo(int* arrayBar)
or
int* foo(int arrayBar[])
If those don't work, make sure your pointer is at the beginning of the array.
Source: Return array in a function