I am trying to come up with a good design of the logic below
Review the example below. I am evaluating the equation below from 10.00 to 15.00 in increments .01 or 10 nano meters. 10.00, 10.01, 10.02 ........... 15.00
Find Y values from a of range 10.00 to 15.00 nano meters. I am asking how to produce the logic. There will be over two hundred values. The limit is from 10.00 to 15.00
Y = X + 3
I am trying yo pruduce the Y output limit is from 10.00 to 15.00 by 10 nanometers or.01
Example: Y is my output.
X | Y
-----------------
10.00 | 10.03
10.01 | 10.04
10.02 | 10.05
10.03 | 10.06
15.00 | 15.03
Loading
David SmithPosted Dec 14, 2011, 2:50 PM
VulpesPosted Dec 14, 2011, 5:04 AM
Using the decimal type avoids this, though it is much slower than double and uses twice as much memory.
Incidentally, don't be frightened to use a simple for statement in C#. It's marginally faster than foreach and (usually) much faster than LINQ which has a lot of hidden overhead.
David SmithPosted Dec 13, 2011, 3:53 PM
public void CalculateExpression()
{
double y = 0.0;
Dictionary
coordinatesDictionary.Clear();
for (double x = 3.0; x <= 14.0; x = x + 0.01)
{
y = x + 3;
coordinatesDictionary.Add(x, y);
}
string fn = @"c:\ArrayInfo20.csv";
File.AppendAllLines(fn, coordinatesDictionary.Select(kvp => String.Format("{0:F2} | {1:F2}", kvp.Key, kvp.Value)));
Process.Start(fn);
}
VulpesPosted Dec 13, 2011, 1:01 PM
David SmithPosted Dec 13, 2011, 12:38 PM
Your output is producing
The output should produce this below instead for the equation below.
Dictionary
coordinatesDictionary = Enumerable.Range(10, 15).ToDictionary(i => i/100m, i => (i + 3)/100m);
Wrong output
X | Y
-----------------
10.01 | 10.03
10.011 | 10.04
10.012 | 10.05
10.013| 10.06
Y = X + 3
Correct Out using logic below
Dictionary
double[] arrayRangeInfo = Enumerable.Range(0, 100).Select(i => (i * .01)).ToArray();
foreach (int i in Enumerable.Range(10, 15))
{
foreach( var itemX in arrayRangeInfo)
{
string xOutput = String.Format("{0:F2}", (i + itemX));
decimal X = Convert.ToDecimal(xOutput);
decimal Y = (X + 3);
coordinatesDictionary.Add(X, Y);
}
}
X | Y
-----------------
10.00 | 10.03
10.01 | 10.04
10.02 | 10.05
10.03 | 10.06
VulpesPosted Dec 13, 2011, 9:35 AM
Dictionary
coordinatesDictionary = Enumerable.Range(1000, 501).ToDictionary(i => i/100m, i => (i + 3)/100m);
David SmithPosted Dec 13, 2011, 9:11 AM
Dictionary
double[] arrayRangeInfo = Enumerable.Range(0, 100).Select(i => (i * .01)).ToArray();
foreach (int i in Enumerable.Range(10, 15))
{
foreach( var itemX in arrayRangeInfo)
{
string xOutput = String.Format("{0:F2}", (i + itemX));
decimal X = Convert.ToDecimal(xOutput);
decimal Y = (X + 3);
coordinatesDictionary.Add(X, Y);
}
}
VulpesPosted Dec 13, 2011, 8:42 AM
To generate a range of decimals directly, with a step of .01, I'd check out my article on dealing with ranges of numbers in C#:
http://www.c-sharpcorner.com/uploadfile/b942f9/dealing-with-ranges-of-numbers-in-C-Sharp/
David SmithPosted Dec 13, 2011, 7:49 AM
foreach (int i in Enumerable.Range(10 ,15))
{
decimal x = i / 1000m;
string xOutput = String.Format("{0:F2} | {1:F2}", x, x + .03m);
sw.WriteLine(xOutput);
}
Karthik AgarwalPosted Dec 13, 2011, 7:40 AM
string xOutput = String.Format("{0:F2} | {1:F2}", x, x + .03m);
That should do the job?
David SmithPosted Dec 13, 2011, 7:25 AM
Below everying is iterating through the foreach by 1's how to iterate by 10 nano meters or .01
for example below i use Enumerable.Range(10 ,15). I want to be able to iterate like this within the range. 10.00, 10.01, 10.02, 10.03, 10.04, 10.05. 10.06, 10.07, 10.08, 10.09, 10.10 all the way to 15.00 instead 10, 11, 12, 13, 14, 15
StreamWriter sw = new StreamWriter("darnell2.txt");
sw.WriteLine(" X | Y");
sw.WriteLine("-------------");
foreach (int i in Enumerable.Range(10 ,15))
{
decimal x = i / 1000m;
string xOutput = String.Format("{0:F2} | {1:F2}", x, x + .03m);
sw.WriteLine(xOutput);
}
sw.Close();
David SmithPosted Dec 13, 2011, 6:38 AM
VulpesPosted Dec 13, 2011, 6:28 AM
You could do it using the LINQ method, Enumerable.Range, like this:
using System;
using System.IO;
using System.Linq;
class Program
{
static void Main()
{
StreamWriter sw = new StreamWriter("darnell2.txt");
sw.WriteLine(" X | Y");
sw.WriteLine("-------------");
foreach(int i in Enumerable.Range(1000, 501))
{
decimal x = i / 100m;
sw.WriteLine(String.Format("{0:F2} | {1:F2}", x, x + .03m));
}
sw.Close();
}
}
You could also avoid using the temporary variable, x, by changing the foreach statement to:
David SmithPosted Dec 13, 2011, 6:09 AM
thanks you guys, I actually design the logic using the for loop like you guys did above, I was trying to come up with new way to simple give two limits and evaluate the Y value between the limits, like you guys did above. Is there another way in c sharp to do this without using the for loop to produce this logic. Is there a way to produce this logic with a foreach or something?
VulpesPosted Dec 13, 2011, 5:10 AM
Here it is as a console app though, as there are 501 pairs of numbers, I've written them to a file rather than to the console:
Karthik AgarwalPosted Dec 13, 2011, 5:02 AM
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace interest_calculation
{
class Program
{
static void Main(string[] args)
{
double X = 0.00F;
double Y = 0.00F;
String Dummy_x = null; //EDITED
String Dummy_y = null;
Console.Write("Please Enter X value : ");
X = Convert.ToDouble(Console.ReadLine());
Console.WriteLine();
while (true)
{
Y = X + 0.03;
if(Y < 15)
{
Dummy_x = String.Format("{0:0.00}", X); //EDITED
Dummy_y = String.Format("{0:0.00}", Y);
Console.WriteLine("X is : " + Dummy_x + " || " + "Y is : " + Dummy_y); //EDITED
X = Y;
}
}
}
}
}
This is the exact code which you asked for i guess. For further assistance post a reply.
Karthik AgarwalPosted Dec 13, 2011, 4:30 AM