Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Printing a matrix

  1. #1
    Member
    Join Date
    Feb 2014
    Posts
    58
    Thanks
    25
    Thanked 0 Times in 0 Posts

    Default Printing a matrix

    Please help.. I have an assignment that states that we have to print out a matrix of random 0s and 1s, the size of n-by-n with n coming from user input. With that being said, I know I have to use Math.random() to get the 0s and 1s. My problem is trying to get the actual matrix because I've looked around and people say to use printf, but I've tried a lot of variations of that and I always end up with tons of errors or just several lines of decimals. I left a couple things out which I'll annotate with '???'.
    //User inputs an integer and program displays matrix of random
    //0's and 1's
     
    import java.util.Scanner;
     
    public class Matrix {
    	public static void main(String[] args) {
     
    		Scanner input = new Scanner(System.in);
     
    	//User inputs integer
    		System.out.print("Please enter an integer: ");
    		int n = input.nextInt();
    		printMatrix(n);	
    	}
     
    public static void printMatrix(int n) {
     
    	while (n > 0) {
    		System.out.println(" " + Math.random() * 2);
    			n--;
    		while (n > 0) {
    			System.out.printf( ???, Math.random());
    			n--;
    		}
    	System.out.println(???);	
    		}
     
    	}
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Printing a matrix

    Show the real code that gets lots of errors and/or lines of decimals be sure to post the errors so that we know what you need help with. Ask specific questions.

    If the code that gets lots of errors is the code above, then you should complete it, attempt to compile and run it, and then post the errors, ask questions, etc.

    It might help to write down with pencil on paper the simple steps to accomplish what you've described:

    - get user input, matrixOrder (looks like you have this part)
    - print a matrix of matrixOrder rows by matrixOrder columns
    - etc. . . .

    Start simple: Do the second step printing a square matrix of only asterisks. After you've done that, you'll know you have the looping part figured out. Then you can can the step that prints asterisks to randomly print either 0 or 1.

    Hints: the first loop controls the number of rows, the inside loop the columns. The printing is done in the inside loop and does not include a <return> after each character printed.

  3. #3
    Member
    Join Date
    Feb 2014
    Posts
    58
    Thanks
    25
    Thanked 0 Times in 0 Posts

    Default Re: Printing a matrix

    This is what I have for my method <printMatrix>. I'm not sure how to make it so that the input can be used in the method. Also, not sure if both for loops should have n as the variable.
    public static void printMatrix(int n) {
    	//first loop = rows, second loop = columns
    	for(n = 3; n > 0; n--) {
    		for(n = 3; n > 0; n--) {
    			System.out.println("*");
    		}
    		}
    	}
    }
    This is my result, the 3 asterisks are there because I wasn't sure how to transfer user input over for this one, so I put <n = 3;> for the loops
    Please enter an integer: 3
    *
    *
    *

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Printing a matrix

    The user's input is passed to the method in the method call:

    printMatrix( n );

    Yes, n is appropriate as limit in both loops, because the matrix is a square, n x n, matrix. However, your for loop construction is odd. The typical construction for this would be:

    for ( int i = 0 ; i < n ; i++ ) {}

    and the same for the nested loop but using a different control variable. (I would usually use 'j'. Use whatever you'd like.)

    See what happens when you DO NOT add a linefeed in the nested loop (as in my hint). Use print() instead of println().

  5. The Following User Says Thank You to GregBrannon For This Useful Post:

    Elyril (March 12th, 2014)

  6. #5
    Member
    Join Date
    Feb 2014
    Posts
    58
    Thanks
    25
    Thanked 0 Times in 0 Posts

    Default Re: Printing a matrix

    Ah.. I see. I thought that was how it was supposed to be, but I kept doubting myself. and now this is my code
    public static void printMatrix(int n) {
    	//first loop = rows, second loop = columns
    	for(int i = 0; i < n; i++) {
    		for(int j = 0; j < n; j++) {
    			System.out.print("* ");
    		}
    		}
    	}
    and my result being
    Please enter an integer: 3
    * * * * * * * * *
    which has the right number of asterisks, but they don't create a n x n matrix

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Printing a matrix

    The code needs to print a newline character at the end of every row.

    ALSO the }s are not in the correct columns.
    If you don't understand my answer, don't ignore it, ask a question.

  8. The Following User Says Thank You to Norm For This Useful Post:

    Elyril (March 12th, 2014)

  9. #7
    Member
    Join Date
    Feb 2014
    Posts
    58
    Thanks
    25
    Thanked 0 Times in 0 Posts

    Default Re: Printing a matrix

    Ah. Got it! Thanks! and I didn't copy all the }s, so that's why they seemed out of place.

  10. #8
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Printing a matrix

    Some of these discoveries are left to the wannabe programmer. If you find you're printing 9 asterisks in a row instead of 3 asterisks in 3 rows, you should ask yourself, "How do I break up the row of 9 asterisks into three rows?" And you or someone who knows that 9 / 3 = 3 might answer, "Add a line feed after printing 3 asterisks." And then you'd play with your code to figure out how to do that.

    Now that you've figured that out, print random 0s and 1s instead of asterisks.

  11. The Following User Says Thank You to GregBrannon For This Useful Post:

    Elyril (March 12th, 2014)

Similar Threads

  1. Printing the following Matrix
    By justfun87 in forum Loops & Control Statements
    Replies: 2
    Last Post: June 23rd, 2013, 09:31 AM
  2. Printing the following Matrix
    By justfun87 in forum The Cafe
    Replies: 1
    Last Post: June 22nd, 2013, 04:19 PM
  3. Knapsack problem , check if matrix can fill list of smaller matrix list.
    By ofirattia in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 8th, 2012, 01:20 PM
  4. Replies: 1
    Last Post: September 28th, 2011, 07:29 AM
  5. [SOLVED] Printing Array without printing empty elements
    By CarlMartin10 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 12th, 2010, 02:41 AM

Tags for this Thread