I've been looking over your code and you have a number of issues going on.
- Don't declare your variables at the top of the code. They should be declared as close as you can to their use to enable easier refactoring and readability of your code.
- You declared
p
as a variable, but that's the name of your function - so it causes a conflict in your code. You don't need this variable.
- You declared
s
& n
as uint
and the method p
with the signature int p(int s, int n)
. You should declare your variables as int
too.
- You're using
Math.Round(..., 4)
to limit the results to at most four decimal places, but I suspect you really want to ensure that there are four decimal places in your output. There is a difference. So Math.Round(1.5, 4).ToString()
becomes 1.5
, but you want 1.500
to make your columns line up. You should use (1.5).ToString("0.000")
instead as this gives 1.500
.
- Using
Math.Round(..., 4)
also can introduce an error in the value of your "Sum of Recip".
- You need to pad out the length of the shape name to ensure your columns line up.
- You need to use
fileOut.WriteLine
when you want to start a new line for the next write, and fileOut.Write
when you want the next write to start on the current line.
Here's your code cleaned up:
fileOut.WriteLine();
fileOut.WriteLine(" Polygon Sum of");
fileOut.WriteLine("s Name n=1 n=2 n=3 n=4 n=5 n=6 n=7 n=8 n=9 Recip");
fileOut.WriteLine("--- --------------- ------ ------ ------ ------ ------ ------ ------ ------ ------ ------");
for (int s = 3; s <= 24; s++)
{
string shapeName = fileIn.ReadLine().Trim();
fileOut.Write("{0,3} {1}", s, shapeName.PadRight(16));
double sum = 0.0;
for (int n = 1; n <= 9; n++)
{
double r = 1.0 / p(s, n);
Console.Write("{0:0.0000} ", r);
sum += r;
}
fileOut.WriteLine("{0:0.0000}", sum);
}
This gives me an output like this:
Polygon Sum of
s Name n=1 n=2 n=3 n=4 n=5 n=6 n=7 n=8 n=9 Recip
--- --------------- ------ ------ ------ ------ ------ ------ ------ ------ ------ ------
3 Triangle 1.0000 0.3333 0.1667 0.1000 0.0667 0.0476 0.0357 0.0278 0.0222 1.8000
4 Pentagon 1.0000 0.2500 0.1111 0.0625 0.0400 0.0278 0.0204 0.0156 0.0123 1.5398
5 1.0000 0.2000 0.0833 0.0455 0.0286 0.0196 0.0143 0.0109 0.0085 1.4107
6 1.0000 0.1667 0.0667 0.0357 0.0222 0.0152 0.0110 0.0083 0.0065 1.3323
7 1.0000 0.1429 0.0556 0.0294 0.0182 0.0123 0.0089 0.0068 0.0053 1.2793
8 1.0000 0.1250 0.0476 0.0250 0.0154 0.0104 0.0075 0.0057 0.0044 1.2411
9 1.0000 0.1111 0.0417 0.0217 0.0133 0.0090 0.0065 0.0049 0.0038 1.2121
10 1.0000 0.1000 0.0370 0.0192 0.0118 0.0079 0.0057 0.0043 0.0034 1.1894
11 1.0000 0.0909 0.0333 0.0172 0.0105 0.0071 0.0051 0.0038 0.0030 1.1711
12 1.0000 0.0833 0.0303 0.0156 0.0095 0.0064 0.0046 0.0035 0.0027 1.1560
13 1.0000 0.0769 0.0278 0.0143 0.0087 0.0058 0.0042 0.0032 0.0025 1.1434
14 1.0000 0.0714 0.0256 0.0132 0.0080 0.0054 0.0039 0.0029 0.0023 1.1326
15 1.0000 0.0667 0.0238 0.0122 0.0074 0.0050 0.0036 0.0027 0.0021 1.1234
16 1.0000 0.0625 0.0222 0.0114 0.0069 0.0046 0.0033 0.0025 0.0019 1.1154
17 1.0000 0.0588 0.0208 0.0106 0.0065 0.0043 0.0031 0.0023 0.0018 1.1083
18 1.0000 0.0556 0.0196 0.0100 0.0061 0.0041 0.0029 0.0022 0.0017 1.1021
19 1.0000 0.0526 0.0185 0.0094 0.0057 0.0038 0.0027 0.0021 0.0016 1.0966
20 1.0000 0.0500 0.0175 0.0089 0.0054 0.0036 0.0026 0.0020 0.0015 1.0916
21 1.0000 0.0476 0.0167 0.0085 0.0051 0.0034 0.0025 0.0019 0.0014 1.0871
22 1.0000 0.0455 0.0159 0.0081 0.0049 0.0033 0.0023 0.0018 0.0014 1.0830
23 1.0000 0.0435 0.0152 0.0077 0.0047 0.0031 0.0022 0.0017 0.0013 1.0793
24 1.0000 0.0417 0.0145 0.0074 0.0044 0.0030 0.0021 0.0016 0.0012 1.0759
I started with this file:
3 Triangle
5 Pentagon
This highlights that you're not actually reading the first number. So there's a better way to do this.
Try this code:
var header = new []
{
" Polygon Sum of",
"s Name n=1 n=2 n=3 n=4 n=5 n=6 n=7 n=8 n=9 Recip",
"--- --------------- ------ ------ ------ ------ ------ ------ ------ ------ ------ ------",
};
var query =
from line in File.ReadAllLines(@"file.txt")
let space = line.IndexOf(' ')
let s = int.Parse(line.Substring(0, space))
let shapeName = line.Substring(space + 1)
let ns = Enumerable.Range(1, 9).Select(n => 1.0 / p(s, n)).ToArray()
let sum_text = ns.Sum().ToString("0.0000")
let ns_text = String.Join(" ", ns.Select(n => n.ToString("0.0000")))
select String.Format("{0, 3} {1} {2} {3}", s, shapeName.PadRight(15), ns_text, sum_text);
File.WriteAllLines(@"output.txt", header.Concat(query));
So, if I start with this file:
3 Triangle
4 Rectangle
20 Icosagon
...I get this output:
Polygon Sum of
s Name n=1 n=2 n=3 n=4 n=5 n=6 n=7 n=8 n=9 Recip
--- --------------- ------ ------ ------ ------ ------ ------ ------ ------ ------ ------
3 Triangle 1.0000 0.3333 0.1667 0.1000 0.0667 0.0476 0.0357 0.0278 0.0222 1.8000
4 Rectangle 1.0000 0.2500 0.1111 0.0625 0.0400 0.0278 0.0204 0.0156 0.0123 1.5398
20 Icosagon 1.0000 0.0500 0.0175 0.0089 0.0054 0.0036 0.0026 0.0020 0.0015 1.0916