# Thread: Arrays: mean, largest value, function count,

1. ## Arrays: mean, largest value, function count,

Hi everyone, I need a bit of help in this assignment I need to hand in for university. Can anyone help me? here is the problem:-

"Read a program that will read in a file of integers into an array. Hence, writing your own functions, compute their mean, the largest value, and a function count to count how many times an integer occurs int he data. Output your results to a file aswell as the screen."

*Apparently these arays must also be read from a text file*

If anyone has any ideas I will be very greatful for your comments and help.

I know that something like this should be in it (I wrote most of this down, my lecturer edited it for me), but can anyone by any chance give me the codes to help complete this assignment.

PHP Code:
```  #include <iostream> using namespace std; float mean(float A[], int Size); int main() {     float myArray[100];     int last, i;     for (i=0; i < 99; i++) // i = i + 1 is the same as i++     {         mean(myArray, i) // what variable is "Size"?     }     last =i-1;     for (i=0; i <= last; i++)     {         cout << X[i] << " "; // don't quote around variables     } //end loop     return 0; } float mean (float A[], int Size)      // what variable is Size? {     float m;     float sum = 0;     for (int i=0; i < Size; i++) // wrong semi-colon,     {         sum = sum + A[i];     }             m = sum/Size;     return m; }  ```
I have declared also where I am also confused, imk not even sure what variable 'Size' is.

THANKS AND KIND REGARDS

ARYAN

2. ## Re: Arrays: mean, largest value, function count,

Moved from the CodeBank

3. ## Re: Arrays: mean, largest value, function count,

Your mean function returns the mean of the array. In a loop you are finding the mean of the first element, then the mean of the first two elements, etc. And you ignore the result of the function. The comment about "what variable is "Size"? " is probably given because the function expects size to contain the size of the array (i.e. 100), but you call it in a loop.

To write to a file you can use:
Code:
```#inlcude <fstream>
std::ofstream file("filename.txt");
file << "something"; // use file just like cout```
To read things from a file you can use ifstream:
Code:
```float things[100];
std::ifstream file("input filename.txt");
for (int i = 0 ; i < 100 ; ++i) {
file >> things[i]; // read the ith thing from the file, and store in array
}```

4. ## Re: Arrays: mean, largest value, function count,

THANK YOU TWANVL, just one question, where abouts would this go in my solution?

5. ## Re: Arrays: mean, largest value, function count,

Somewhere in the main function, unless you split your program into several functions. You can open the files at the start. Reading is probably the first thing you want to do. The output to the file can be done everytime you also write to cout, that way the file will contian the same as what is written to the screen.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

Featured