Member Avatar for kobalt

Hello All,

I am desperate for help. I have spent the last 2 days staring at this code and not getting anywhere (please see attached project files).
The project requires a number sequence to be output as a text file.
Starting number is input by user (number is in sequence ie. 1 2 3 4)
the last digit then increments to 45 (1 2 3 45)
then the proceeding digit increments by 1 and the sequence starts again (1 2 4 5) with the last digit going up to 45 (1 2 3 4 6 45) this repeats until (1 2 44 45) and then the 2nd digit starts incrementing (1 3 4 5) final output will be (1 43 44 45)
no digit is ever less than the one proceeding it.
The project should be able to take any number up to 40 41 42 43 (which will finish 40 43 44 45)

this is an example of the output I am getting (in red is whats missing):
38 39 40 41
38 39 40 42
38 39 40 43
38 39 40 44
38 39 40 45
38 39 41 42
38 39 41 43
38 39 41 44
38 39 41 45
38 39 42 43
38 39 42 44
38 39 42 45
38 39 43 44
38 39 43 45
38 39 44 45
38 40 41 42
38 40 41 43
38 40 41 44
38 40 41 45
38 40 42 43
38 40 42 44
38 40 42 45
38 40 43 44
38 40 43 45
38 40 44 45
38 41 42 43
38 41 42 44
38 41 42 45
38 41 43 44
38 41 43 45
38 42 43 44
38 42 43 45
38 43 44 45

If someone could help it would be greatly appreciated! Thanks in advance!

Recommended Answers

All 2 Replies

Try this:

using System;
using System.Text;
using System.Windows.Forms;

namespace Counter
{
  public partial class Counter : Form
  {
    const int MAX_INPUT = 40;
    const int MAX_A = 42;
    const int MAX_B = 43;
    const int MAX_C = 44;
    const int MAX_D = 45;
    //a.b.c.d
    const string fmt_line = @"{0:F0} {1:F0} {2:F0} {3:F0}";

    private static string GetFormattedLine(int a, int b, int c, int d)
    {
      return string.Format(fmt_line, a, b, c, d);
    }

    public Counter()
    {
      InitializeComponent();
    }

    private void btnQuit_Click(object sender, EventArgs e)
    {
      this.Close();
    }

    private void txtInput_TextChanged(object sender, EventArgs e)
    {
      int value;

      if (string.IsNullOrEmpty(txtInput.Text))
      {
        txtCalcNumber.Text = string.Empty;
      }
      else if (!int.TryParse(txtInput.Text, out value))
      {
        txtCalcNumber.Text = "Invalid number";
      }
      else if (value > MAX_INPUT)
      {
        txtCalcNumber.Text = string.Format("Maximum allowed: {0:F0}", MAX_INPUT);
      }
      else
      {
        txtCalcNumber.Text = GetFormattedLine(value, value + 1, value + 2, value + 3);
      }
    }

    private void btnCount_Click(object sender, EventArgs e)
    {
      int value;
      if (!int.TryParse(txtInput.Text, out value) || (value > MAX_INPUT))
        return;
      StringBuilder sb = new StringBuilder();
      for (int a = value; a <= MAX_A; a++)
      {
        for (int b = (a + 1); b <= MAX_B; b++)
        {
          for (int c = (b + 1); c <= MAX_C; c++)
          {
            for (int d = (c + 1); d <= MAX_D; d++)
            {
              sb.AppendLine(GetFormattedLine(a, b, c, d));
            }
          }
        }
      }
      txtOutput.Text = sb.ToString().Trim();
    }
  }
}
commented: clear and concise, i like :) +1
Member Avatar for kobalt

Many thanks Scott,
I eventually succeeded in working it out - after scrapping the Arrays though.

Thanks again for your time and effort

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.