Locker room question generalized to n people - repeating error









up vote
-2
down vote

favorite












I have the following code:



import java.util.Scanner;
import java.util.Arrays;
public class Program11

public static void main(String args)
Scanner input = new Scanner(System.in);
System.out.print("Number of lockers:");
int number = input.nextInt();
System.out.print("Show stages [y/n]?");
String show = input.next();
if(show.equals("y"))

for (char lockers : getStages(number))

for (char c : lockers)

System.out.print("" + c);

System.out.println();




public static char getStages(int n)
char lockers = new char[n];
char arrayLockers = new char[n];
for (int i = 0; i < n; i++)
lockers[i] = 'O';

for (int i = 0; i<1;i++)
arrayLockers[i] = lockers;

for(int i = 2; i<=n; i++)
for(int z = 1; z<n; z++)
for (int w = i-1; w <= n; w += i)
lockers[w] = 'X';

arrayLockers[z] = lockers;



return arrayLockers;




The output is for n = 10:



> run Program11
Number of lockers: 10
Show stages [y/n]? y
OXXXXXXXXX
OXXXXXXXXX
OXXXXXXXXX
OXXXXXXXXX
OXXXXXXXXX
OXXXXXXXXX
OXXXXXXXXX
OXXXXXXXXX
OXXXXXXXXX
OXXXXXXXXX
>


The locker room problem goes as follows:
There are n students and n lockers. The first student opens all the lockers. The second student closes lockers 2,4,6,8,.... the third student closes lockers 3,6,9,12,... This patterns repeats itself until all n students have gone.



My task:



I am supposed to show the lockers at each stage for a given n. 'X' represents closed and 'O' represents open. I am supposed to use arrays only. Clearly this is repeating the same array over and over again which is incorrect. I used the method getStages to return a multi-dimensional array "arrayLockers" to store each stage.
Could anyone tell me where I have gone wrong?



Expected output is:



OOOOOOOOOO 
OXOXOXOXOX
OXXXOXXXXX
OXXXOXXXXX
OXXXXXXXXX
OXXXXXXXXX
OXXXXXXXXX
OXXXXXXXXX
OXXXXXXXXX
OXXXXXXXXX









share|improve this question























  • arrayLockers[z] = lockers; Are you assigning one array to another here? Instead, use System.arraycopy() function.
    – kiner_shah
    Nov 11 at 7:00











  • Correct, this is where i make my multi-dimensional array.
    – the boy 88
    Nov 11 at 7:03






  • 1




    that is not the expected output but thank you for trying.
    – the boy 88
    Nov 11 at 7:36






  • 1




    Then what is the expected outcome? Is there a pattern/algorithm or should it be random which ones are opened?
    – Joakim Danielson
    Nov 11 at 7:39






  • 1




    Thanks @kiner_shah, that is correct. I will compare my code to see where my logic went wrong.
    – the boy 88
    Nov 11 at 7:44














up vote
-2
down vote

favorite












I have the following code:



import java.util.Scanner;
import java.util.Arrays;
public class Program11

public static void main(String args)
Scanner input = new Scanner(System.in);
System.out.print("Number of lockers:");
int number = input.nextInt();
System.out.print("Show stages [y/n]?");
String show = input.next();
if(show.equals("y"))

for (char lockers : getStages(number))

for (char c : lockers)

System.out.print("" + c);

System.out.println();




public static char getStages(int n)
char lockers = new char[n];
char arrayLockers = new char[n];
for (int i = 0; i < n; i++)
lockers[i] = 'O';

for (int i = 0; i<1;i++)
arrayLockers[i] = lockers;

for(int i = 2; i<=n; i++)
for(int z = 1; z<n; z++)
for (int w = i-1; w <= n; w += i)
lockers[w] = 'X';

arrayLockers[z] = lockers;



return arrayLockers;




The output is for n = 10:



> run Program11
Number of lockers: 10
Show stages [y/n]? y
OXXXXXXXXX
OXXXXXXXXX
OXXXXXXXXX
OXXXXXXXXX
OXXXXXXXXX
OXXXXXXXXX
OXXXXXXXXX
OXXXXXXXXX
OXXXXXXXXX
OXXXXXXXXX
>


The locker room problem goes as follows:
There are n students and n lockers. The first student opens all the lockers. The second student closes lockers 2,4,6,8,.... the third student closes lockers 3,6,9,12,... This patterns repeats itself until all n students have gone.



My task:



I am supposed to show the lockers at each stage for a given n. 'X' represents closed and 'O' represents open. I am supposed to use arrays only. Clearly this is repeating the same array over and over again which is incorrect. I used the method getStages to return a multi-dimensional array "arrayLockers" to store each stage.
Could anyone tell me where I have gone wrong?



Expected output is:



OOOOOOOOOO 
OXOXOXOXOX
OXXXOXXXXX
OXXXOXXXXX
OXXXXXXXXX
OXXXXXXXXX
OXXXXXXXXX
OXXXXXXXXX
OXXXXXXXXX
OXXXXXXXXX









share|improve this question























  • arrayLockers[z] = lockers; Are you assigning one array to another here? Instead, use System.arraycopy() function.
    – kiner_shah
    Nov 11 at 7:00











  • Correct, this is where i make my multi-dimensional array.
    – the boy 88
    Nov 11 at 7:03






  • 1




    that is not the expected output but thank you for trying.
    – the boy 88
    Nov 11 at 7:36






  • 1




    Then what is the expected outcome? Is there a pattern/algorithm or should it be random which ones are opened?
    – Joakim Danielson
    Nov 11 at 7:39






  • 1




    Thanks @kiner_shah, that is correct. I will compare my code to see where my logic went wrong.
    – the boy 88
    Nov 11 at 7:44












up vote
-2
down vote

favorite









up vote
-2
down vote

favorite











I have the following code:



import java.util.Scanner;
import java.util.Arrays;
public class Program11

public static void main(String args)
Scanner input = new Scanner(System.in);
System.out.print("Number of lockers:");
int number = input.nextInt();
System.out.print("Show stages [y/n]?");
String show = input.next();
if(show.equals("y"))

for (char lockers : getStages(number))

for (char c : lockers)

System.out.print("" + c);

System.out.println();




public static char getStages(int n)
char lockers = new char[n];
char arrayLockers = new char[n];
for (int i = 0; i < n; i++)
lockers[i] = 'O';

for (int i = 0; i<1;i++)
arrayLockers[i] = lockers;

for(int i = 2; i<=n; i++)
for(int z = 1; z<n; z++)
for (int w = i-1; w <= n; w += i)
lockers[w] = 'X';

arrayLockers[z] = lockers;



return arrayLockers;




The output is for n = 10:



> run Program11
Number of lockers: 10
Show stages [y/n]? y
OXXXXXXXXX
OXXXXXXXXX
OXXXXXXXXX
OXXXXXXXXX
OXXXXXXXXX
OXXXXXXXXX
OXXXXXXXXX
OXXXXXXXXX
OXXXXXXXXX
OXXXXXXXXX
>


The locker room problem goes as follows:
There are n students and n lockers. The first student opens all the lockers. The second student closes lockers 2,4,6,8,.... the third student closes lockers 3,6,9,12,... This patterns repeats itself until all n students have gone.



My task:



I am supposed to show the lockers at each stage for a given n. 'X' represents closed and 'O' represents open. I am supposed to use arrays only. Clearly this is repeating the same array over and over again which is incorrect. I used the method getStages to return a multi-dimensional array "arrayLockers" to store each stage.
Could anyone tell me where I have gone wrong?



Expected output is:



OOOOOOOOOO 
OXOXOXOXOX
OXXXOXXXXX
OXXXOXXXXX
OXXXXXXXXX
OXXXXXXXXX
OXXXXXXXXX
OXXXXXXXXX
OXXXXXXXXX
OXXXXXXXXX









share|improve this question















I have the following code:



import java.util.Scanner;
import java.util.Arrays;
public class Program11

public static void main(String args)
Scanner input = new Scanner(System.in);
System.out.print("Number of lockers:");
int number = input.nextInt();
System.out.print("Show stages [y/n]?");
String show = input.next();
if(show.equals("y"))

for (char lockers : getStages(number))

for (char c : lockers)

System.out.print("" + c);

System.out.println();




public static char getStages(int n)
char lockers = new char[n];
char arrayLockers = new char[n];
for (int i = 0; i < n; i++)
lockers[i] = 'O';

for (int i = 0; i<1;i++)
arrayLockers[i] = lockers;

for(int i = 2; i<=n; i++)
for(int z = 1; z<n; z++)
for (int w = i-1; w <= n; w += i)
lockers[w] = 'X';

arrayLockers[z] = lockers;



return arrayLockers;




The output is for n = 10:



> run Program11
Number of lockers: 10
Show stages [y/n]? y
OXXXXXXXXX
OXXXXXXXXX
OXXXXXXXXX
OXXXXXXXXX
OXXXXXXXXX
OXXXXXXXXX
OXXXXXXXXX
OXXXXXXXXX
OXXXXXXXXX
OXXXXXXXXX
>


The locker room problem goes as follows:
There are n students and n lockers. The first student opens all the lockers. The second student closes lockers 2,4,6,8,.... the third student closes lockers 3,6,9,12,... This patterns repeats itself until all n students have gone.



My task:



I am supposed to show the lockers at each stage for a given n. 'X' represents closed and 'O' represents open. I am supposed to use arrays only. Clearly this is repeating the same array over and over again which is incorrect. I used the method getStages to return a multi-dimensional array "arrayLockers" to store each stage.
Could anyone tell me where I have gone wrong?



Expected output is:



OOOOOOOOOO 
OXOXOXOXOX
OXXXOXXXXX
OXXXOXXXXX
OXXXXXXXXX
OXXXXXXXXX
OXXXXXXXXX
OXXXXXXXXX
OXXXXXXXXX
OXXXXXXXXX






java arrays multidimensional-array






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 8:23









Deadpool

3,1432324




3,1432324










asked Nov 11 at 6:55









the boy 88

255




255











  • arrayLockers[z] = lockers; Are you assigning one array to another here? Instead, use System.arraycopy() function.
    – kiner_shah
    Nov 11 at 7:00











  • Correct, this is where i make my multi-dimensional array.
    – the boy 88
    Nov 11 at 7:03






  • 1




    that is not the expected output but thank you for trying.
    – the boy 88
    Nov 11 at 7:36






  • 1




    Then what is the expected outcome? Is there a pattern/algorithm or should it be random which ones are opened?
    – Joakim Danielson
    Nov 11 at 7:39






  • 1




    Thanks @kiner_shah, that is correct. I will compare my code to see where my logic went wrong.
    – the boy 88
    Nov 11 at 7:44
















  • arrayLockers[z] = lockers; Are you assigning one array to another here? Instead, use System.arraycopy() function.
    – kiner_shah
    Nov 11 at 7:00











  • Correct, this is where i make my multi-dimensional array.
    – the boy 88
    Nov 11 at 7:03






  • 1




    that is not the expected output but thank you for trying.
    – the boy 88
    Nov 11 at 7:36






  • 1




    Then what is the expected outcome? Is there a pattern/algorithm or should it be random which ones are opened?
    – Joakim Danielson
    Nov 11 at 7:39






  • 1




    Thanks @kiner_shah, that is correct. I will compare my code to see where my logic went wrong.
    – the boy 88
    Nov 11 at 7:44















arrayLockers[z] = lockers; Are you assigning one array to another here? Instead, use System.arraycopy() function.
– kiner_shah
Nov 11 at 7:00





arrayLockers[z] = lockers; Are you assigning one array to another here? Instead, use System.arraycopy() function.
– kiner_shah
Nov 11 at 7:00













Correct, this is where i make my multi-dimensional array.
– the boy 88
Nov 11 at 7:03




Correct, this is where i make my multi-dimensional array.
– the boy 88
Nov 11 at 7:03




1




1




that is not the expected output but thank you for trying.
– the boy 88
Nov 11 at 7:36




that is not the expected output but thank you for trying.
– the boy 88
Nov 11 at 7:36




1




1




Then what is the expected outcome? Is there a pattern/algorithm or should it be random which ones are opened?
– Joakim Danielson
Nov 11 at 7:39




Then what is the expected outcome? Is there a pattern/algorithm or should it be random which ones are opened?
– Joakim Danielson
Nov 11 at 7:39




1




1




Thanks @kiner_shah, that is correct. I will compare my code to see where my logic went wrong.
– the boy 88
Nov 11 at 7:44




Thanks @kiner_shah, that is correct. I will compare my code to see where my logic went wrong.
– the boy 88
Nov 11 at 7:44












1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










There were some problems with your code. Firstly, you were not allocating memory to arrayLockers before copying. Secondly, you were complicating the logic by using three for loops (nested). It could be done with just two nested loops. Also, you were using array assignment which I am not sure whether it works; so I changed it with System.arraycopy() which I use often (or you can do it manually using a for loop).



import java.util.*;
import java.lang.*;
import java.io.*;

class Program11

public static void main(String args)
Scanner input = new Scanner(System.in);
System.out.print("Number of lockers:n");
int number = input.nextInt();
System.out.print("Show stages [y/n]?n");
String show = input.next();
if(show.equals("y"))

for (char lockers : getStages(number))

for (char c : lockers)

System.out.print("" + c);

System.out.println();




public static char getStages(int n)
char lockers = new char[n];
char arrayLockers = new char[n];
for (int i = 0; i < n; i++)
lockers[i] = 'O';

arrayLockers[0] = new char[lockers.length];
System.arraycopy(lockers, 0, arrayLockers[0], 0, lockers.length);
int cnt = 2;
for (int i = 1; i < n; i++)
for (int j = i; j < n; j += cnt)
lockers[j] = 'X';

arrayLockers[i] = new char[lockers.length];
System.arraycopy(lockers, 0, arrayLockers[i], 0, lockers.length);
cnt++;

return arrayLockers;







share|improve this answer




















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53246507%2flocker-room-question-generalized-to-n-people-repeating-error%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote



    accepted










    There were some problems with your code. Firstly, you were not allocating memory to arrayLockers before copying. Secondly, you were complicating the logic by using three for loops (nested). It could be done with just two nested loops. Also, you were using array assignment which I am not sure whether it works; so I changed it with System.arraycopy() which I use often (or you can do it manually using a for loop).



    import java.util.*;
    import java.lang.*;
    import java.io.*;

    class Program11

    public static void main(String args)
    Scanner input = new Scanner(System.in);
    System.out.print("Number of lockers:n");
    int number = input.nextInt();
    System.out.print("Show stages [y/n]?n");
    String show = input.next();
    if(show.equals("y"))

    for (char lockers : getStages(number))

    for (char c : lockers)

    System.out.print("" + c);

    System.out.println();




    public static char getStages(int n)
    char lockers = new char[n];
    char arrayLockers = new char[n];
    for (int i = 0; i < n; i++)
    lockers[i] = 'O';

    arrayLockers[0] = new char[lockers.length];
    System.arraycopy(lockers, 0, arrayLockers[0], 0, lockers.length);
    int cnt = 2;
    for (int i = 1; i < n; i++)
    for (int j = i; j < n; j += cnt)
    lockers[j] = 'X';

    arrayLockers[i] = new char[lockers.length];
    System.arraycopy(lockers, 0, arrayLockers[i], 0, lockers.length);
    cnt++;

    return arrayLockers;







    share|improve this answer
























      up vote
      0
      down vote



      accepted










      There were some problems with your code. Firstly, you were not allocating memory to arrayLockers before copying. Secondly, you were complicating the logic by using three for loops (nested). It could be done with just two nested loops. Also, you were using array assignment which I am not sure whether it works; so I changed it with System.arraycopy() which I use often (or you can do it manually using a for loop).



      import java.util.*;
      import java.lang.*;
      import java.io.*;

      class Program11

      public static void main(String args)
      Scanner input = new Scanner(System.in);
      System.out.print("Number of lockers:n");
      int number = input.nextInt();
      System.out.print("Show stages [y/n]?n");
      String show = input.next();
      if(show.equals("y"))

      for (char lockers : getStages(number))

      for (char c : lockers)

      System.out.print("" + c);

      System.out.println();




      public static char getStages(int n)
      char lockers = new char[n];
      char arrayLockers = new char[n];
      for (int i = 0; i < n; i++)
      lockers[i] = 'O';

      arrayLockers[0] = new char[lockers.length];
      System.arraycopy(lockers, 0, arrayLockers[0], 0, lockers.length);
      int cnt = 2;
      for (int i = 1; i < n; i++)
      for (int j = i; j < n; j += cnt)
      lockers[j] = 'X';

      arrayLockers[i] = new char[lockers.length];
      System.arraycopy(lockers, 0, arrayLockers[i], 0, lockers.length);
      cnt++;

      return arrayLockers;







      share|improve this answer






















        up vote
        0
        down vote



        accepted







        up vote
        0
        down vote



        accepted






        There were some problems with your code. Firstly, you were not allocating memory to arrayLockers before copying. Secondly, you were complicating the logic by using three for loops (nested). It could be done with just two nested loops. Also, you were using array assignment which I am not sure whether it works; so I changed it with System.arraycopy() which I use often (or you can do it manually using a for loop).



        import java.util.*;
        import java.lang.*;
        import java.io.*;

        class Program11

        public static void main(String args)
        Scanner input = new Scanner(System.in);
        System.out.print("Number of lockers:n");
        int number = input.nextInt();
        System.out.print("Show stages [y/n]?n");
        String show = input.next();
        if(show.equals("y"))

        for (char lockers : getStages(number))

        for (char c : lockers)

        System.out.print("" + c);

        System.out.println();




        public static char getStages(int n)
        char lockers = new char[n];
        char arrayLockers = new char[n];
        for (int i = 0; i < n; i++)
        lockers[i] = 'O';

        arrayLockers[0] = new char[lockers.length];
        System.arraycopy(lockers, 0, arrayLockers[0], 0, lockers.length);
        int cnt = 2;
        for (int i = 1; i < n; i++)
        for (int j = i; j < n; j += cnt)
        lockers[j] = 'X';

        arrayLockers[i] = new char[lockers.length];
        System.arraycopy(lockers, 0, arrayLockers[i], 0, lockers.length);
        cnt++;

        return arrayLockers;







        share|improve this answer












        There were some problems with your code. Firstly, you were not allocating memory to arrayLockers before copying. Secondly, you were complicating the logic by using three for loops (nested). It could be done with just two nested loops. Also, you were using array assignment which I am not sure whether it works; so I changed it with System.arraycopy() which I use often (or you can do it manually using a for loop).



        import java.util.*;
        import java.lang.*;
        import java.io.*;

        class Program11

        public static void main(String args)
        Scanner input = new Scanner(System.in);
        System.out.print("Number of lockers:n");
        int number = input.nextInt();
        System.out.print("Show stages [y/n]?n");
        String show = input.next();
        if(show.equals("y"))

        for (char lockers : getStages(number))

        for (char c : lockers)

        System.out.print("" + c);

        System.out.println();




        public static char getStages(int n)
        char lockers = new char[n];
        char arrayLockers = new char[n];
        for (int i = 0; i < n; i++)
        lockers[i] = 'O';

        arrayLockers[0] = new char[lockers.length];
        System.arraycopy(lockers, 0, arrayLockers[0], 0, lockers.length);
        int cnt = 2;
        for (int i = 1; i < n; i++)
        for (int j = i; j < n; j += cnt)
        lockers[j] = 'X';

        arrayLockers[i] = new char[lockers.length];
        System.arraycopy(lockers, 0, arrayLockers[i], 0, lockers.length);
        cnt++;

        return arrayLockers;








        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 11 at 7:47









        kiner_shah

        1,13321323




        1,13321323



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53246507%2flocker-room-question-generalized-to-n-people-repeating-error%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            How to read a connectionString WITH PROVIDER in .NET Core?

            Node.js Script on GitHub Pages or Amazon S3

            Museum of Modern and Contemporary Art of Trento and Rovereto