# Need to use mod operator to complete printSquare program

• March 21st, 2013, 12:08 PM
mccoyca
Need to use mod operator to complete printSquare program
I need to make a program that will accept max and min integers and print a square of lines of increasing numbers.

printSquare(3,6) needs to print:

3456
4563
5634
6345

Below is what I have so far and its current output:

public static void printSquare(int i, int j) {
int x = 3;
int y = 6;

for( i = x; i <= y; i++) {
for( j = i; j <= y; j++) {

System.out.print(j);

}

System.out.println();

}

Ouput:

3456
456
56
6

How can I use the mod function to complete this program?
• March 21st, 2013, 12:19 PM
Chris.Brown.SPE
Re: Need to use mod operator to complete printSquare program
I'm not seeing how the mod function will come into play at all in this application.

Questions:
1. Am i right thinking you are asking about the mod (aka modulus) function that returns the remainder of a division? 135 % 20 = 15
2. Do i understand your problem correctly. You give a min and max number then print every combination of those numbers using ascending order.
printSquare(1,3)
123
231
312
printSquare(4,9)
456789
567894
678945
789456
894567
945678

--- Update ---

Assuming this is a homework assignment, is your teacher requiring you to use the mod function?
• March 21st, 2013, 12:26 PM
mccoyca
Re: Need to use mod operator to complete printSquare program
Yes that is the mod function I am asking about, and yes you are understanding the program correctly. My professor suggested that I use the mod operator, but does not require that I do so.
• March 21st, 2013, 12:34 PM
Chris.Brown.SPE
Re: Need to use mod operator to complete printSquare program
My initial suggestion is to find a better mechanic for creating your output then displaying all of your output at the end of the program. Teachers will make things more complicated until this is pretty much required. But that's just something to look at in the future.

I really dont see how mod would fit in here. The way you're going about it is prolly the same way I would. You're very close to being done. Stating what your program does in words can be very beneficial to working this problem out. Right now you're looping through each number and then looping from that number to the END printing each number. You can see that in your output and it is working...now what's next? You have all the pieces and your code works, you just need to hash out the second half of the algorithm. Mull it over for a few minutes and get back to me.
• March 21st, 2013, 01:36 PM
mccoyca
Re: Need to use mod operator to complete printSquare program
Is it a problem that I am using the ++ operator in my for loop? The other thought I had was that I could initialize an array to print 4 numbers on each line.
• March 21st, 2013, 01:40 PM
Chris.Brown.SPE
Re: Need to use mod operator to complete printSquare program
Ok back up a second. Nothing in your current code is incorrect. You are just missing the piece of the story After number to max, print from min to the current number". You've already done "Print from current number to max". Just like you can tell the story in English, your code should flow just like the story. Find the missing piece and write it.
• March 21st, 2013, 01:40 PM
Norm
Re: Need to use mod operator to complete printSquare program
How are you going to have the digits wrap around to the starting digit when the end digit is shown on a line?
• March 21st, 2013, 01:47 PM
Chris.Brown.SPE
Re: Need to use mod operator to complete printSquare program
I'm not going to spoon feed you norm ;)

The program isnt doing a println until the end of the main for loop. Just add the code to print the numbers from "min" to "i" after the code that prints from "i" to "max".
• March 21st, 2013, 01:55 PM
Norm
Re: Need to use mod operator to complete printSquare program
@Chris.Brown.SPE We posted at the same time. My post was to the OP, not you.
• March 22nd, 2013, 01:15 PM
mccoyca
Re: Need to use mod operator to complete printSquare program
Is this code on the right track to completing the program:

for( i = y; i >= x; i++);
for( j = i; j >= y; j++);
• March 22nd, 2013, 01:28 PM
Norm
Re: Need to use mod operator to complete printSquare program
One problem with the for loops is the ; which ends the statement making for two empty loops.

Otherwise what are the loops supposed to do?
• March 22nd, 2013, 01:49 PM
mccoyca
Re: Need to use mod operator to complete printSquare program
They are supposed to wrap the range of numbers around so that after 6 it goes back to 3. I want to print from min to the current number, and that's what I was trying to do because I have already printed the other half--from the current number to max.
• March 22nd, 2013, 01:55 PM
Norm
Re: Need to use mod operator to complete printSquare program
Have you compiled and executed the code to see what it does?
• March 22nd, 2013, 02:05 PM
mccoyca
Re: Need to use mod operator to complete printSquare program
I tried to run it, but I don't think I am printing it correctly because it didn't result in any output.
• March 22nd, 2013, 02:07 PM
Norm
Re: Need to use mod operator to complete printSquare program
What you posted did NOT have a print statement in it. You need to call a print() method to print anything out.
• March 22nd, 2013, 03:21 PM
mccoyca
Re: Need to use mod operator to complete printSquare program
I tried to run it, but I don't think I am printing it correctly because it didn't result in any output.

--- Update ---

I really appreciate the questions, but I have not executed the program correctly yet and it is due soon. Do you mind helping me figure it out?
• March 22nd, 2013, 03:22 PM
Norm
Re: Need to use mod operator to complete printSquare program
Hard to say anything without seeing any code.

A suggestion: The variable names should reflect what values they hold and how they are used. i,j,x,y have no meanings. min, max row and column carry some meaning and make the code easier to read and understand.
• March 22nd, 2013, 03:33 PM
mccoyca
Re: Need to use mod operator to complete printSquare program
If you look at the first post in this thread, I have written the code that I have so far, and the output that is resulting from it. It is pretty close. Also, I assigned values to x and y, x = 3 (min) and y = 6 (max)
• March 22nd, 2013, 03:36 PM
Norm
Re: Need to use mod operator to complete printSquare program
What code do you have now? What is the current problem you are having?

Quote:

values to x and y, x = 3 (min) and y = 6 (max)
One change I recommend is to change the variable names to something related to the data that they hold. X and y have no meaning when reading the code.
• March 22nd, 2013, 03:47 PM
mccoyca
Re: Need to use mod operator to complete printSquare program
The problem is still the same--I can't figure out how to make the numbers wrap around so that 4 numbers are printed out on 4 lines in every combination in ascending order. Right now my output is:

3456
456
56
6

and it needs to be:

3456
4563
5634
6345
• March 22nd, 2013, 03:54 PM
Norm
Re: Need to use mod operator to complete printSquare program
Look at what is printed on each row.
The first digit is the row+min
followed by the digits up through the max digit
following the max digit is the min digit and print the digits for the rest of that row

The above requires a counter to know how many digits to print.
• March 25th, 2013, 07:43 AM
Chris.Brown.SPE
Re: Need to use mod operator to complete printSquare program
mccoya, has this been resolved? Sorry for being AFK, work got busy.