Results 1 to 1 of 1

Math Help - Application to solve the classic Locker Problem

  1. #1
    Bar0n janvdl's Avatar
    Joined
    Apr 2007
    From
    Meh
    Posts
    1,630
    Thanks
    6

    Application to solve the classic Locker Problem

    I have programmed an application to solve this problem and will also include the source code for it.

    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    
    namespace Lockers
    {
        public partial class frmLockers : Form
        {
            int[] Lockers;
    
            public frmLockers()
            {
                InitializeComponent();
            }
    
            private void frmLockers_Load(object sender, EventArgs e)
            {
                txtLockers.Focus();
            }
    
            private void btnRun_Click(object sender, EventArgs e)
            {
                try
                {
                    int iLockers = Convert.ToInt32(txtLockers.Text) + 1;
    
                    rtbOutput.Clear();
    
                    Simulate(iLockers);
                    DisplayOutput(iLockers);
                }
                catch
                {
                    MessageBox.Show("Your input was not in integer format!");
                }
                
            }
    
            private void Simulate(int iLockers)
            { 
                Lockers = new int[iLockers];
    
                prgLockers.Maximum = iLockers - 1;
    
    
                for (int i = 1; i <= iLockers - 1; i++)
                {
                    Lockers[i] = -1;
                }
    
    
                for (int iLearner = 1; iLearner <= iLockers - 1; iLearner++)
                {
                    for (int iLockerNum = 1; iLockerNum <= iLockers - 1; iLockerNum++)
                    {
                        if ((iLockerNum) % (iLearner) == 0)
                        {
                            Lockers[iLockerNum] = Lockers[iLockerNum] * (-1);
                        }
                    }
                    prgLockers.Value = iLearner;
                    Application.DoEvents();
                }
            }
    
            private void DisplayOutput(int iLockers)
            {
    
                string sOutput = "";
                int iLockersOpen = 0;
    
                for (int i = 1; i <= iLockers - 1; i++)
                {
                    if (Lockers[i] == 1)
                    {
                        sOutput += i.ToString() + " is open.\n";
                        iLockersOpen++;
                    }
                    else
                    {
                        sOutput += i.ToString() + " is closed.\n";
                    }
    
                }
    
                rtbOutput.Text = "Lockers open: " + iLockersOpen.ToString() + "\n\n" + sOutput;
            }
    
            private void btnExit_Click(object sender, EventArgs e)
            {
                Application.Exit();
            }
    
        }
    }
    Attached Files Attached Files
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. Replies: 2
    Last Post: September 23rd 2011, 04:05 AM
  2. A classic geometry problem
    Posted in the Math Challenge Problems Forum
    Replies: 11
    Last Post: April 5th 2010, 08:23 PM
  3. Classic Diophantine problem
    Posted in the Number Theory Forum
    Replies: 1
    Last Post: March 27th 2009, 03:26 AM
  4. The Locker Problem
    Posted in the Math Challenge Problems Forum
    Replies: 3
    Last Post: February 15th 2007, 08:06 PM
  5. Geometric Series & A Classic Problem
    Posted in the Pre-Calculus Forum
    Replies: 1
    Last Post: January 30th 2007, 01:15 AM

Search Tags


/mathhelpforum @mathhelpforum