# Thread: some help, i get strange number.

1. ## some help, i get strange number.

Code:
```float average (float x[], int n)
{
float sum =0.0;
int  i;
float average;

for (i=0; i<n; i++)
sum +=x[i];
return average =(sum/float(n));
}```
and then i get the following as output

Code:
```Enter file name ->      nums.txt
x[0] = 1
x[1] = 2
x[2] = 3
x[3] = 76
x[4] = 56
x[5] = 45

The average is: 0x00401028
Press any key to continue```
any suggestions?

2. How about show us the code that calls the function and prints the returned value.

3. Code:
```#include <fstream.h>
#include <stdlib.h>
// Math library
#include <math.h>
float average (float x[], int n)
{
float sum =0.0;
int  i;
float average;

for (i=0; i<n; i++)
sum +=x[i];
return average =(sum/float(n));
}

const int FileNameLen = 30, ArrayLen = 6;

// The class array
class array
{
public: // public functions
array(void);
//Inline functions
inline int getsize() {return(size);}
void write(void);
void     draw(void);
private:

int x[ArrayLen];
int size;
};

array::array(void)

{
size = 0;
}

{
size = 0;
do {
cout << "Enter a value\t?";
cin >> x[size++]; // Read the integers
} while (x[size-1] != 0 && size < ArrayLen);

// Array indices from 0 to n-1
size = (size == ArrayLen)? size - 2: size - 1;
}

void array::write(void)
{
//CREATE A FILE

ofstream outfile; // file object
char filename[FileNameLen]; // Store the file name

// as a character string

cout << "Enter file name ->\t";
cin >> filename;

// Open the file
outfile.open(filename);
// If you can't open the file, print an error message
// and quit
if (!outfile) {
cerr << "Cannot open " << filename << endl;
exit(1);
}

for (int i = 0;  i < size;  i++)
outfile << "x[" << i << "] = " << x[i] << '\n';

}

void array::draw(void)
{
ifstream infile; // file object
char filename[FileNameLen]; // Store the file name
char inchar; // as a character string

cout << "Enter file name ->\t";
cin >> filename;

infile.open(filename);

if (!infile) {
cerr << "Cannot open " << filename << endl;
exit(1);
}

while (!infile.eof())
{
inchar = infile.get();

cout << inchar; //Display it on screen

}

infile.close();  //close the file

}

int main()
{

//display file of screen
const int FileNameLen = 30; // constant integer
ifstream infile;   //file object

//initialize array
array myarray;
myarray.draw();

cout << "\nThe average is: " << average << endl;

return(0);
}```
if u need to run the prog ull have to create a txt file that contains the array values exactly as in the output that i posted above.

4. Code:
`cout << "\nThe average is: " << average << endl;`
You forgot to put in the parenthesis and supply the arguments to that function. Bottom line is you are not calling the function properly. Used in this manner, I suspect that the result you are seeing as output is the address of the function, which would explain the hexadecimal value.

5. how do i fix it?

6. Originally Posted by hk_mp5kpdw
You forgot to put in the parenthesis and supply the arguments to that function.
Maybe putting in the parentheses and supplying the arguments to that function.

7. i still dont understand lol, on all the progs ive done so far all i had to do to get the right result was to cout the average, now this works different cuz its an array so where am i missing a parentheses and the arguments to the function?

8. You have a class array and an object of that class called myarray. Internally, that myarray object has an array of ints that it stores and which is private to that object. You also have an outside function called average. I assume you want to display the average of the values in the int array that is contained within the myarray object. Personally, I might make the average function a member function of the class, that would simplify/elimiinate any need to pass parameters:

Code:
```class array
{
public: // public functions
array(void);
//Inline functions
inline int getsize() {return(size);}
void write(void);
void draw(void);
float average(void);
private:

int x[ArrayLen];
int size;
};

float array::average()
{
float sum =0.0;
int  i;
for (i=0; i<size; i++)
sum += x[i];
return sum/float(size);
}```
Then when you are ready to call the function:

Code:
`cout << "\nThe average is: " << myarray.average() << endl;`

9. oooh i see, you need to use the dot operator when u calling functions that are in a class right?