0

i am in tricky situation here , details are explained below , please guide me

there is an array like one :- [position] = element [0]=1 [1]=2 [2]=3 [3]=4 [4]=5 and so on .... please note that i didn't posted actual array(dynamic memory allocation to be precise) so elements of actual array are obviously different now the thing is i have to calculate average till each point separately like :- [0]=1 //cant be calculated for 0th position [1]=2//avg = (1+2)/2 (where 2 is total number of numbers added) [2]=3//avg = (1+2+3)/3 (where 3 is total number of numbers added), [3]=4//avg = (1+2+3+4)/4((where 4 is total number of numbers added)) [4]=5 // avg =(1+2+3+4+5)/5((where is total number of numbers added)) and so on... please note that its a dynamic memory allocation so there can be any number of times loop needs to run and load these results in separate array (dynamic memory allocation to precise ) solutions using C are preferable , C++ will work as well i've been told by dad to use #define offsetof() , that i don't understand in any case , if you want to take a look at code (which is very messy and buggy like its writer ) here is the link : https://docs.google.com/document/d/1WkH1yxi0REp5i7mLSPWuZnGcT3bzLJXwVIJmKmWZ9vU/edit?usp=sharing

5th Dec 2017, 7:31 AM
Hridyansh Thakur
Hridyansh Thakur - avatar
5 ответов
+ 5
Woah, quite a large code just to get average of numbers from an array. Ill try to help though. Can you tell me what are the arrays division and multiplication for? When you read from a binary file (.dat), shouldn't you use 'rb' instead of 'r' to read your numbers? Or are you treating the dat file as a text file? And, in the end, you just need to make a program for this, or correct your own?
5th Dec 2017, 12:39 PM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 5
https://code.sololearn.com/cml9mz8VPUpH/?ref=app Here is something I have tried to do. Edit : Now converted to C.
6th Dec 2017, 4:19 PM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 3
@zeke williams , that was a recommendation, that i don't know how to use , @kinshuk vasisht i am using dat file as plain text file , and multiplication and division is used for mixing and mashing numbers and combination to find curves and patterns in data , you may fully ignore them , i just need a hint how to calculate average till each point , can you break down my code into functions?
6th Dec 2017, 3:35 PM
Hridyansh Thakur
Hridyansh Thakur - avatar
+ 2
Firstly, here is a function you may try: float* avg_arr(float* myarr, int size) { float* result = (float*) malloc( sizeof(float) * size ); for(int i=0;i<size;i++) { result[i] = 0; for(int j=0;j<i;j++) { result[i] += myarr[j]; } result[i] /= i+1; } return result; } Ill look at your code again and try dividing it into functions for you to use.
6th Dec 2017, 3:46 PM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 1
Can't you just have a running sum and then calculate the average using the sum at each point? And what exactly do you need help with? Just knowing how to use #define offsetof() ?
6th Dec 2017, 12:02 AM
Zeke Williams
Zeke Williams - avatar