How can a group of values calculated in a function be retrieved via a Dynamic Array Class in C++?

How can a group of values calculated in a function be retrieved via a Dynamic Array Class in C++?

Below is an example emulating the basic idea of what I'm trying to accomplish.

Notice that the printf from the mFunction shows that values load in the array. How can they be retrieved _tmain ?

printf Results
---------------
From mFunction 1.0 <-- Desired Result.
From mFunction 1.5
From mFunction 2.0
From mFunction 3.0
From mFunction 4.0
From mFunction 5.5
From _tmain -431602080.0 <-- ????? How retrieve the same results.
From _tmain -431602080.0
From _tmain -431602080.0
From _tmain -431602080.0
From _tmain -431602080.0
From _tmain -431602080.0

Code
------------
#include "stdafx.h"
#include <iostream>

using namespace std;

int mFunction (int iLen, int iCount);

class CMyArray
{
private:
float* m_pnInternalArray;
int m_nNumElements;

public:
CMyArray (int nNumElements);
~CMyArray ();

// a subscript operator
float& operator [] (int nIndex);
};

float& CMyArray::operator [] (int nIndex)
{
return m_pnInternalArray [nIndex];
}

CMyArray::CMyArray (int nNumElements)
{
m_pnInternalArray = new float [nNumElements];
m_nNumElements = nNumElements;
}

CMyArray::~CMyArray ()
{
delete [] m_pnInternalArray;
}

int _tmain(int argc, _TCHAR* argv[])
{

int iReturnValue;
iReturnValue = mFunction(2, 3);

CMyArray mArray (6);

for (int nIndex = 0; nIndex < 6; ++ nIndex)
printf("%12.1f\n",mArray [nIndex]);

return 0;
}

int mFunction (int iLen, int iCount)
{
float zLoad0 = 1.0;
float zLoad1 = 1.5;
float zLoad2 = 2.0;
float zLoad3 = 3.0;
float zLoad4 = 4.0;
float zLoad5 = 5.5;

int iMaxIndex = (iLen * iCount);

CMyArray mArray (iMaxIndex);

mArray [0] = zLoad0;
mArray [1] = zLoad1;
mArray [2] = zLoad2;
mArray [3] = zLoad3;
mArray [4] = zLoad4;
mArray [5] = zLoad5;

for (int nIndex = 0; nIndex < iMaxIndex; ++ nIndex)
printf("%12.1f\n",mArray [nIndex]);

return 0;
}
closed account (o1vk4iN6)
You haven't initialized your date in your main function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int _tmain(int argc, _TCHAR* argv[])
{

     int iReturnValue;
     iReturnValue = mFunction(2, 3);

     CMyArray mArray (6); // array contains uninitialized data

     // mArray[0] = 1.0f;

     for (int nIndex = 0; nIndex < 6; ++ nIndex)
          printf("%12.1f\n",mArray [nIndex]);

     return 0;
}
You can make the function return the array

CMyArray mFunction (int iLen, int iCount)

return mArray;

Then in _tmain

1
2
CMyArray mArray (6);
mArray = mFunction(2, 3);
Last edited on
Topic archived. No new replies allowed.