for loop: add seconds each time a loop is made [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 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);
}
}

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

How to Zip two numbers using c# [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 6 years ago.
Improve this question
string A = "1234"
string B = "567890"
I want to zip the numbers. Out put should display as "1526374890"
what is the best way to achieve this using C# code.
You can do that with the following code. It does not make any assumptions about which string is longer.
string A = "1234";
string B = "567890";
char[] chars = new char[A.Length + B.Length];
int charsIndex = 0;
for (int i = 0; i < A.Length || i < B.Length; i++)
{
if(i < A.Length)
chars[charsIndex++] = A[i];
if(i < B.Length)
chars[charsIndex++] = B[i];
}
string result = new string(chars);
Console.WriteLine(result);

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).

Having trouble counting in c# [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 9 years ago.
Improve this question
I am having trouble with this. How can I count a length I want input to.
Lets say I ask a user an input. He enters 10. It counts 1,2,3,4,etc... 10.
Or if users enter 5, 1,2,3,4,5 is output
Thanks.
EDIT:
I am sorry. This isn't homework. School doesn't start till next week and I am practicing.
Sorry, I should have given code.
This is what I had that does work
Console.WriteLine("Enter Length");
int length = int.Parse(Console.ReadLine());
for (int i = 0; i < length; i++)
{
Console.WriteLine(i);
}
I am just assuming since I am new that I did some sloppy code and am looking for maybe something cleaner. Or another point of view for it.
update your code with <=
Console.WriteLine("Enter Length");
int length = int.Parse(Console.ReadLine());
for (int i = 0; i <= length; i++)
{
Console.WriteLine(i);
}
You just need to change the '<' operator to '<=':
for (int i = 0; i <= length; i++)
{
Console.WriteLine(i);
}
string length;
Console.Write("Enter Length: ");
length= Console.ReadLine();
for (int i = 1; i <= Int32.Parse(length); i++)
{
Console.WriteLine(i);
}
Console.ReadKey();

Taking average value every 2 seconds [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 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++;

Categories