Taking average value every 2 seconds [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
i have a problem with arrays or something missed in these text..
my program works every 500ms and i want to read first 4 double values and take average of these values and then get next 4 double values and so on... i write something about this and can you pls look on this??
if (u_dcbus_pv_act[i] > 0 && i != 0)
{
u_dcbus_pv = u_dcbus_pv_act[i];
p_dcbus_pv = p_dcbus_pv_act[i];
}
if (i >= 3)
{
for (int j = 0; j < 4; j++)
{
total_u += u_dcbus_pv;
total_p += p_dcbus_pv;
}
average_u = total_u / 4;
average_p = total_p / 4;
u_dcbus_target = average_u;
p_dcbus_pv_avg = average_p;
}

from what I understand of your description, I would do it something like this:
/* add current samples to totals */
total_u += u_dcbus_pv_act[i];
total_p += p_dcbus_pv_act[i];
/* every fourth tick, calc average and reset totals */
if (i % 4 == 0)
{
average_u = total_u / 4;
average_p = total_p / 4;
total_u = 0;
total_p = 0;
}
u_dcbus_target = average_u;
p_dcbus_pv_avg = average_p;
i++;

Related

Generate random sequence of AlphaNumeric [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I want a code to generate sequence of Alphanumeric character like LLNNLLNNLL where L is the Letter and N is the number. For example if the length is 5 the sequence will be LLNNL and if the length is 6 the sequence will be LLNNLL and if 7 it would be LLNNLLN.
string alphabets = "ABCDEFGHIJKLMNPQRSTUVWX";
int length = model.VALUE_INT;
string result = "";
for (int i = 0; i < length; i++)
{
int next = _random.Next(23);
result += alphabets.ElementAt(next);
result += _randomNum.Next(1, 9);
}
This is what I have tried but condition is not fulfilling
For each position (value of i) of your output string you need to check if you need a character or an integer.
Since the pattern repeats every 4 positions, you can achieve this using the modulo operator:
for (int i = 0; i < length; i++)
{
switch (i % 4)
{
case 0:
case 1:
int next = _random.Next(23);
result += alphabets.ElementAt(next);
break;
case 2:
case 3:
result += _randomNum.Next(1, 9);
break;
}
}
Or another possibility: Add blocks of LLNN and then truncate the result to the needed length...
for (int i = 0; i <= (length/4); i++)
{
result += alphabets.ElementAt(_random.Next(23));
result += alphabets.ElementAt(_random.Next(23));
result += _randomNum.Next(1, 9);
result += _randomNum.Next(1, 9);
}
result = result.Substring(0,length);
If you want to improve these lines, you can use a StringBuilder

Change from iterative to recursive method [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
int arraySum (int [] a, int n)
{
int sum = 0;
n = a.size();
for (int i = 1; i < n; i++)
sum += a[i];
return sum;
}
I want to convert this code from iterative to recursive.
C# Version:
int arraySum ( int [] a, int sum = 0, int i = 0 ) /*i = 0, technically means this code is logically different from yours, however it will count every element and is just a default :)*/
{
if( i < a.Length )
return arraySum( a, sum + a[i], ++i );
return sum;
}
You need:
1- Recursive definition like: sum(n) = n + sum(n-1)
2- You need to specify where should you stop so the recursion does not last forever.
for example: if (n == 0) return 0;
based on this you can code at any language.
C++ Example:
int arraySum (int a[], int n)
{
if(n==1)
return a[n-1];
else
return a[n-1] + arraySum (a, n-1);
}

C# using loops to generate a table with correct arithmetic relationship [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
The following is the ShowTab() method, how to apply dynamic numbers and result in to the table?
using System;
const int MAX = 4;
int cage = 500/total;
int month = 1;
int adults = 1;
int babies = 0;
int total = 1;
Console.WriteLine("Month\tAdults\tBabies\tTotal");
Console.WriteLine("{0, -10}{1, -10}{2, -10}{3, -10}", month, adults, babies, total);
for(int i = 0; i < MAX; i++) {
Console.writeLine(
}
Maybe I missed something ; but if it's only about formatting somehow ; something like this should do the job :
int month = 1;
int adults = 1;
int babies = 0;
int total = 1;
Console.WriteLine ("header row"); // optional (if needed)
while (/* there is still cages to hold them */)
{
// print current state (-10 width chosen for example, negative for left align)
Console.WriteLine ($"{month, -10}{adults, -10}{babies, -10}{total, -10}");
// do the maths to update values
month = /* ... */;
adults = /* ... */;
babies = /* ... */;
total = /* ... */;
}
Here is a dummy exemple which illustrate why I choose to use width formatting specifier rather than tabulation (as hinted in one comment link).

abcpdf content front of header and footer [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am trying to create an structure using abcpdf that suppose to be like this:
header
content
footer
P.S: content needs to be "front of" header and footer
In other words, I need to have my content starting from y = 0 to y = 100%; My header starting from y = 0 to y = 200; My footer starting from y = 700 to y = 100%. (Values here are just samples)
I hope I am being clear.
At the moment I have those methods:
private void CreateDocHeader(Doc theDoc, int theCount, string content)
{
theDoc.Rect.String = "0 660 600 825";
theDoc.MediaBox.String = theDoc.Rect.String;
for (int i = 1; i <= theCount; i++)
{
theDoc.PageNumber = i;
theDoc.AddImageHtml(content);
}
}
private void CreateDocFooter(Doc theDoc, int theCount, string content)
{
theDoc.Rect.String = "0 200 600 10";
theDoc.MediaBox.String = theDoc.Rect.String;
for (int i = 1; i <= theCount; i++)
{
theDoc.PageNumber = i;
theDoc.AddImageHtml(content);
}
}
private void CreateDocContent(Doc theDoc, int theID, string theContent, ref int theCount)
{
theDoc.Rect.String = "0 800 600 10";
theDoc.MediaBox.String = theDoc.Rect.String;
theID = theDoc.AddImageHtml(theContent);
while (theDoc.Chainable(theID))
{
theDoc.Page = theDoc.AddPage();
theID = theDoc.AddImageToChain(theID);
}
theCount = theDoc.PageCount;
}
Anyone?
Use Hpos and Vpos properties to do that :-
For header, do this:
theDoc.HPos = 0.5;
theDoc.VPos = 1.0;
this will keep your header in left alignment.
For footer, do this:
theDoc.HPos = 1.0;
theDoc.VPos = 0.5;
this will keep your footer in right alignment.
Hope this helps.
Addition:
For having the Z position, that is have the different tiers for the header/footer and content, use the Layer property. This property alongwith the LayerCount property, you can have the solution.
For more reference go through this documentation:-
http://www.websupergoo.com/helppdfnet/source/5-abcpdf/doc/2-properties/layer.htm
http://www.websupergoo.com/helppdfnet/source/5-abcpdf/doc/2-properties/layercount.htm

for loop: add seconds each time a loop is made [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
How can I add 300 seconds to this for loop each time an iteration is made?
what I need is simple but I cant focus.
my initial seconds = 14400 and the next loop I need to add 300 seconds.. so
loop 1: seconds = 14400
loop 2: seconds = 14700
loop 3: seconds = 15000
and so on...
for (int i = 0; i < 145; i++)
{
int seconds = 14400;
TimeSpan t = TimeSpan.FromSeconds(seconds);
string time = string.Format("{0:D2}:{1:D2}:{2:D2}", t.Hours, t.Minutes, t.Seconds);
Chart1.Series["TARGET"].Points.AddXY(time, 0);
}
Put this int seconds = 14400; outside for loop. Per your post below code will add 300 seconds in each iteration.
int seconds = 14400;
for (int i = 0; i < 145; i++)
{
seconds+= 300;
}
Are you looking for TimeSpan.AddSeconds(300) ?
t.AddSeconds(300);
Here:
void example()
{
var t = TimeSpan.FromSeconds(14400);
for(int i = 0; i < 145; i++)
{
t.Add(TimeSpan.FromSeconds(300));
string time = string.Format("{0:D2}:{1:D2}:{2:D2}", t.Hours, t.Minutes, t.Seconds);
Chart1.Series["TARGET"].Points.AddXY(time, 0);
}
}

Categories