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

Karthik AgarwalRank #2,270 · 620 reputationPosted Dec 12, 2011, 11:30 PM EST
Karthik AgarwalRank #2,270 · 620 reputationPosted Dec 13, 2011, 12:02 AM EST
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.
VulpesRank #55 · 29.2K reputationPosted Dec 13, 2011, 12:10 AM EST
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:
David SmithRank #999 · 2K reputationPosted Dec 13, 2011, 1:09 AM EST
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?
VulpesRank #55 · 29.2K reputationPosted Dec 13, 2011, 1:28 AM EST
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 SmithRank #999 · 2K reputationPosted Dec 13, 2011, 1:38 AM EST
David SmithRank #999 · 2K reputationPosted Dec 13, 2011, 2:25 AM EST
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();
Karthik AgarwalRank #2,270 · 620 reputationPosted Dec 13, 2011, 2:40 AM EST
string xOutput = String.Format("{0:F2} | {1:F2}", x, x + .03m);
That should do the job?
David SmithRank #999 · 2K reputationPosted Dec 13, 2011, 2:49 AM EST
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);
}
VulpesRank #55 · 29.2K reputationPosted Dec 13, 2011, 3:42 AM EST
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 SmithRank #999 · 2K reputationPosted Dec 13, 2011, 4:11 AM EST
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);
}
}
VulpesRank #55 · 29.2K reputationPosted Dec 13, 2011, 4:35 AM EST
Dictionary
coordinatesDictionary = Enumerable.Range(1000, 501).ToDictionary(i => i/100m, i => (i + 3)/100m);
David SmithRank #999 · 2K reputationPosted Dec 13, 2011, 7:38 AM EST
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
VulpesRank #55 · 29.2K reputationPosted Dec 13, 2011, 8:01 AM EST
David SmithRank #999 · 2K reputationPosted Dec 13, 2011, 10:53 AM EST
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);
}
VulpesRank #55 · 29.2K reputationPosted Dec 14, 2011, 12:04 AM EST
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 SmithRank #999 · 2K reputationPosted Dec 14, 2011, 9:50 AM EST