0

I have an input file that has 24 rows with names of polygons in it. In my code under the writeline heading of s I will list the polygon sides then under polygon name the name of the shape. For example:

s polygon name

2 triangle

3 rectangle

Next I have to use my final function of static int p () which is the formula to calculate the section of my writeline of n=1, n=2 etc. But I am stuck on how to incorporate that into my inner for loop to do those calculations based on my first column of s and then the number of sides.

static void GenReport()
{


  uint s, n, p;
  string shapeName;


  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 (s = 3; s <= 24; s++)
  {
   shapeName = fileIn.ReadLine().Trim();
   fileOut.WriteLine("{0,3} {1}", s, shapeName);
    for (n = 1; n <= 9; n++)
    {
      fileOut.WriteLine("{0}");
      Math.Round(1.0/p(s,n),4)
    }

  }

}

static int p(int s, int n)
{
  return (n * n * (s - 2) - n * (s - 4)) / 2;
}

The above static int p is the formula for calculating the reciprocals for n=1 n=2 etc. I need help getting that into my inner for loop to have the calculations done.

Thanks, Jessie

  • 1
    `For example if a triangle has 1 side what is the reciprocal` This seems to be the crux of your question, and knowing what a polygon reciprocal is or *how* to calculate it is not a programming matter. If you can define what that is exactly (I don't know without googling), and need help understanding how to *program* the reciprocal calculation, you might find help here. – Eric J. Jan 27 '16 at 00:47
  • @EricJ. the calculation to calculate it the very last function. The static int p() that returns the formula divided by 2. I suppose it boils down to how do I integrate that return statement into my inner for loop. So if s = 3 then run that formula and do the calculation. Does that make sense? –  Jan 27 '16 at 01:53

2 Answers2

1

I've been looking over your code and you have a number of issues going on.

  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. Using Math.Round(..., 4) also can introduce an error in the value of your "Sum of Recip".
  6. You need to pad out the length of the shape name to ensure your columns line up.
  7. 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
Enigmativity
  • 113,464
  • 11
  • 89
  • 172
  • Are you saying that the reciprocal should only be computed for the sum? Otherwise for each value of `n` only `p(s, n)`, and not `1.0 / p(s, n)`, should be computed? – Enigmativity Jan 28 '16 at 00:10
0

EDIT: I had to fix a couple of things with your original code... I should have tested first.

  1. You have to use consistent variable types. You had declared uint s,n,p at the top, but you were trying to pass s and n to p as int, not uint. Do you know why you're using uint? I think int is probably fine.

  2. If you're declaring a method p, don't also declare a variable p; you don't need a variable for the return. If you were trying to declare a variable to hold the return in to use it later, name it something different.

These two problems caused the errors you were getting.

I think maybe this is what you're trying to do:

static void GenReport()
{
    uint s, n;
    string shapeName;


    Console.WriteLine();
    Console.WriteLine("       Polygon                                          Sum of ");
    Console.WriteLine(" s      Name        n=1 n=2 n=3 n=4 n=5 n=6 n=7 n=8 n=9 Recip  ");
    Console.WriteLine("--- --------------- --- --- --- --- --- --- --- --- --- ------ ");
    for (s = 3; s <= 24; s++)
    {
        shapeName = Console.ReadLine().Trim();
        Console.WriteLine("{0,3} {1}", s, shapeName);
        double sum = 0; // declare a variable outside the loop for the sum
        for (n = 1; n <= 9; n++)
        {
            double currentNumber = Math.Round(1.0/p(s, n), 4);
            Console.Write("{0} ", currentNumber);
            sum += currentNumber;
        }
        Console.Write("{0}", sum);
    }
}

static uint p(uint s, uint n)
{
    return (n * n * (s - 2) - n * (s - 4)) / 2;
}

You have to declare a variable outside of your inner loop, to save the cumulative outputs from your 'p' method (consider naming that something more decriptive along with it's parameters, BTW).

Note the calls to Console.Write instead of Console.WriteLine, which allow you to tack on to the same line later.

You should also check out this answer and the official documentation for a better way to format your table output.

Community
  • 1
  • 1
DrewJordan
  • 5,266
  • 1
  • 25
  • 39
  • Did you test this code? I get "CS0266 Cannot implicitly convert type 'double' to 'decimal'. An explicit conversion exists (are you missing a cast?)". – Enigmativity Jan 27 '16 at 02:41
  • right, so, there's a couple of things in your original code that need to be fixed. Hold on, I'll post a complete sample. – DrewJordan Jan 27 '16 at 03:10