Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have float array here
0 0 0 0 0 120 145 154 254 254 156 0 0 0 0 0 0 14 15 12 85 65 85 78 0 0 0 0 0 0 0 965 985 785 123 654 784 965 325 0 0 0 0 0 0 0 0.........


In the above case I have to take average of 120 145 154 254 254 156 and save as Number-1
and same for the other set for 14 15 12 85 65 85 78 and save as number-2 and so on.

What is logic behind this or code in C# maybe which can help me?

What I have tried:

didnt try anything...stuck here.
Posted
Updated 14-Jun-16 8:13am
Comments
Kenneth Haugland 14-Jun-16 13:18pm    
Read some books about C# first, as you give the impression that you don't know much about it.
Patrice T 14-Jun-16 14:18pm    
Definitely HomeWork
Harshal Patil 14-Jun-16 14:49pm    
Not homework
project work and i m not good in c# for sure.
BillWoodruff 14-Jun-16 18:44pm    
Your data is rather "odd:" the number of zeroes separating the "groups" are different in each case. Why is that ?

1 solution

This has a lot of a "homework" smell about it, so no code!
Start by looking at the data: at a guess you want to sum the values between zeros.
So the first step is to identify the data.
That's pretty simple: set up a List to hold your averages, and create a bool so you can tell if you are assembling averages or not, and two values: a total, and a count.
Set up a loop to look at each value in the array.
In the loop, look at the value.
If it's zero, then look at the "assembling" bool.
If you are assembling, then divide the total by the count, and add that value to the list. Reset the total and count, and set the bool to "not assembling". Otherwise, ignore it.
If the value isn't zero, add the value to the total, and increment the count. Set the bool to "assembling".

At the end of the loop, add any final value to the list.
Your list now contains each of the averages as separate numbers.
 
Share this answer
 
Comments
Harshal Patil 14-Jun-16 14:36pm    
i Really appreciate but still seems difficult for me.
Can you give me code for only one Set
OriginalGriff 14-Jun-16 14:48pm    
No - this is your homework!
Your tutor wants what you think, what you can do, not what I can do.
Give it a try, this really isn't difficult at all if you just think about it.
Harshal Patil 14-Jun-16 15:57pm    
public void Averages()
{
DataAnalyse da = new DataAnalyse();
float T1 = 0, T2 = 0;
int count = 0;
bool assembling= true;

for (int j = 0; j < disp.Length; j++)
{
if (disp[j] == 0)
{
assembling = true;
T1 += disp[j];
count++;
da.total = T1 / count;
T1 = 0;
count = 0;
assembling = false;
}
if (disp[j] != 0)
{
T1 += disp[j];
count++;
da.total = T1 / count;
assembling = true;
}
ObjectData.Add(da);

}



whats wrong with this one?
OriginalGriff 15-Jun-16 2:57am    
I dunno. What does it do when you run it?
Mind you, checking "assembling" instead of just setting it all the time might help it work a bit better...
Harshal Patil 15-Jun-16 14:24pm    
Are you sure whatever you said above gonna give me three separate average.
its giving me only one average of all

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900