Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3352268
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T02:00:33+00:00 2026-05-18T02:00:33+00:00

Hey guys, im working through the Introduction to Programming in Java book and one

  • 0

Hey guys, im working through the Introduction to Programming in Java book and one of the exercises is this:

Empirical shuffle check. Run
computational experiments to check
that our shuffling code works as
advertised. Write a program
ShuffleTest that takes command-line
arguments M and N, does N shuffles of
an array of size M that is initialized
with a[i] = i before each shuffle, and
prints an M-by-M table such that row i
gives the number of times i wound up
in position j for all j. All entries
in the array should be close to N/M.

Now, this code just outputs a block of zeros…

public class ShuffleTest2 {
  public static void main(String[] args) {
    int M = Integer.parseInt(args[0]);
    int N = Integer.parseInt(args[1]); 
    int [] deck = new int [M];

    for (int i = 0; i < M; ++i)
      deck [i] = i;

    int [][] a = new int [M][M];

    for (int i = 0; i < M; i++) {
      for (int j = 0; j < M; j++) {
        a[i][j] = 0 ;

        for(int n = 0; n < N; n++) {
          int r = i + (int)(Math.random() * (M-i));
          int t = deck[r];
          deck[r] = deck[i];
          deck[i] = t;

          for (int b = 0; b < N; b++)
          {
            for (int c = 0; c < M; c++)
              System.out.print(" " + a[b][c]);
            System.out.println();
          }
        }
      }
    }
  }
}

What am i doing wrong? 🙁

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T02:00:34+00:00Added an answer on May 18, 2026 at 2:00 am

    So a is like a history? As you are now it is always filled with zeroes just like you initialized, you never assign to it! After the “shuffling” for loop you need to set

    A[i][POSITION] = CARD_VALUE
    

    Meaning that after i-th shuffle, card CARD_VALUE is in position POSITION. I don’t want to give you all the specifics, but it will take another for loop, and the nested for-loop for printing needs to be independent of any other loop, occuring when everything else is done.

    Looks like you have a few things concerning the for-loops that you need to look over carefully. Trace the program flow manually or with a debugger and you’ll notice that some of those braces and code blocks need to be moved.

    –TRY THIS–

    public class ShuffleTest2 {
    
      public static void main(String[] args) {
        int M = Integer.parseInt(args[0]);
        int N = Integer.parseInt(args[1]); 
        int [] deck = new int [M];
    
        int [][] a = new int [M][M]; 
    
        for (int i = 0; i < M; i++) {  //initialize a to all zeroes
          for (int j = 0; j < M; j++) {
            a[i][j] = 0 ; 
          }
        }
    
        for(int i = 0; i < N; i++)   //puts the deck in order, shuffles it, and records. N times
        {
            for (int j = 0; j < M; j++)  //order the deck
              deck[j] = j;
    
            for(int j = 0; j < M; j++) {       //shuffle the deck (same as yours except counter name)
              int r = j + (int)(Math.random() * (M-j));
              int t = deck[r];
              deck[r] = deck[j];
              deck[j] = t;
            }
    
           for(int j = 0; j < M; j++)   //record status of this deck as described
           {
               int card_at_j = deck[j];  //value of card in position j
               a[card_at_j][j]++;        //tally that card_at_j occured in position j
           }
        }  //big loop ended
    
        for (int b = 0; b < M; b++)  //print loop.  a is MxM, so limit of N was wrong.
        {
            for (int c = 0; c < M; c++)
            {
               System.out.print(" " + a[b][c]);
               System.out.println();
            }
        }  //print loop ended
      }  //main() ended
     } //class ended
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

hey guys im working on a task to make my story's links like this
Hey guys, I'm working on a status-Updater. It works, there is just one problem,
Hey guys, here's my issue. Working with the iPort... one of the commands to
Hey guys, ive been working on this drop down for wayyy to long now.
Hey all, my Computational Science course this semester is entirely in Java. I was
Hey guys. I'm creating a rails app and being one of the first times
Hey guys I'm currenlty working on something and I want if I click a
Hey guys, working on an event calendar. I'm having some trouble getting my column
hey i guys i want help from you. i am working on website project,
hey guys, i'm getting an exception on the following inner exception: {Value cannot be

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.