Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jul 5th, 2007, 6:50 PM   #1
357mag
Hobbyist Programmer
 
Join Date: Mar 2005
Posts: 148
Rep Power: 4 357mag is on a distinguished road
My output does not look right

I've got this program(I wrote it in C++ now transferring it to Java) that has three columns. Under the first column is a number(1, 2, 3, 4, 5) and under the second column is the square, and under the third column is the cube. But for some reason, the numbers under the Cube column don't look right. This is what the output looks like:

Value     Square     Cube
  1          1          1
  2          4          8
  3          9          27
  4         16           64
  5         25           125

I want the numbers under the Cube column to print more like they are under the Square column, so they will look more like:

Cube
  1
  8
  27
  64
  128

Beats me why it's doing that. Here is my program:

public static void main(String args[]) {
        
        System.out.println("Value     Square     Cube");
        
        for (int n = 1; n <= 5; n++)
        {
            System.out.print("  ");
            System.out.print(n);
            System.out.print("         ");
            System.out.print(square(n));
            System.out.print("          ");
            System.out.print(cube(n));
            System.out.println();
        }
               
    }
    
    static int square(int x)
    {
        return x * x;
    }
    
    static int cube(int y)
    {
        return y * y * y;
    } 
}
357mag is offline   Reply With Quote
Old Jul 5th, 2007, 6:55 PM   #2
Ben.Dougall
Programmer
 
Ben.Dougall's Avatar
 
Join Date: Jul 2007
Location: London, Ontario, Canada
Posts: 34
Rep Power: 0 Ben.Dougall is on a distinguished road
Send a message via MSN to Ben.Dougall
because of the space seperators... i would use a \t instead of " ". Once the squares hit 2 digits, it makes another space in the line of output.

Heres what could pose a better solution

public static void main(String args[]) 
{
        
    System.out.println("Value\tSquare\tCube");
       
    for (int n = 1; n <= 5; n++)
    {
        System.out.println( " " + n + "\t" + square(n) + "\t" + cube(n) );
    }
               
}
    
    static int square(int x)
    {
        return x * x;
    }
    
    static int cube(int y)
    {
        return y * y * y;
    } 
}
Ben.Dougall is offline   Reply With Quote
Old Jul 5th, 2007, 7:08 PM   #3
357mag
Hobbyist Programmer
 
Join Date: Mar 2005
Posts: 148
Rep Power: 4 357mag is on a distinguished road
I'll try your suggestion and hopefully it will work. But it doesn't explain why when we get to the Cube column, specifically when the number is 64 and then 125 that the output shifts over to the right even more. There are some numbers under the Square column that also have two digits in them, yet they print just fine.
357mag is offline   Reply With Quote
Old Jul 5th, 2007, 7:13 PM   #4
Ben.Dougall
Programmer
 
Ben.Dougall's Avatar
 
Join Date: Jul 2007
Location: London, Ontario, Canada
Posts: 34
Rep Power: 0 Ben.Dougall is on a distinguished road
Send a message via MSN to Ben.Dougall
Heres a quick C++ version of your code as i don;t have a Java SDK installed. The \t character can be used in either language.

#include <iostream>

int Square ( int &num  ) 
{
	return num * num;
}

int Cube ( int &num )
{
	return num * num * num;
}

int main ( void ) 
{
	std::cout << "Num\tSquare\tCube" << std::endl;

	for ( int i = 0; i < 6; ++i ) 
		std::cout << i << "\t" << Square( i ) << "\t" << Cube( i ) << std::endl;
	
	std::cout << std::endl;
	return 0;
}

Which when run produces:

Num     Square  Cube
0       0       0
1       1       1
2       4       8
3       9       27
4       16      64
5       25      125

Press any key to continue . . .

::::::::::::::

Quote:
I'll try your suggestion and hopefully it will work. But it doesn't explain why when we get to the Cube column, specifically when the number is 64 and then 125 that the output shifts over to the right even more. There are some numbers under the Square column that also have two digits in them, yet they print just fine.
AS for that, I have no clue, because indeed the square column should be bumped over as well.
Ben.Dougall is offline   Reply With Quote
Old Jul 5th, 2007, 7:20 PM   #5
357mag
Hobbyist Programmer
 
Join Date: Mar 2005
Posts: 148
Rep Power: 4 357mag is on a distinguished road
Well here is what I did. I wanted the numbers to print under the centers of the words Value, Square, and Cube, instead of at the left:

System.out.println( "  " + n + "\t" + "  " + square(n) + "\t" + " " + cube(n) );

But when I look at the headings Value, Square, and Cube, the word Cube is spaced closer to the word Square, than Square is to Value. I don't know why that is. I want it to look evenly spaced between the words:

Value     Square     Cube

But instead it's looking more like this:
Value     Square   Cube

I got to try and fix it so it looks evenly spaced.
357mag is offline   Reply With Quote
Old Jul 5th, 2007, 7:45 PM   #6
Ben.Dougall
Programmer
 
Ben.Dougall's Avatar
 
Join Date: Jul 2007
Location: London, Ontario, Canada
Posts: 34
Rep Power: 0 Ben.Dougall is on a distinguished road
Send a message via MSN to Ben.Dougall
A quick fix would be add one or two spaces after the second \t... You could use if's and do checks to make it better, but for a minor program, I would use the quick fix.
Ben.Dougall is offline   Reply With Quote
Old Jul 5th, 2007, 9:34 PM   #7
357mag
Hobbyist Programmer
 
Join Date: Mar 2005
Posts: 148
Rep Power: 4 357mag is on a distinguished road
Yeah I guess that would work. Does anyone know why the words are not evenly spaced just by using the "\t" character? Why does the word Cube appear closer to the word Square, than Square does to Value? Seems to me all three words should be evenly spaced.
357mag is offline   Reply With Quote
Old Jul 5th, 2007, 9:38 PM   #8
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Use formatted output, space it as you like.
    public static void main (String[] args)
    {
        System.out.println("Value    Square     Cube");
        
        for (int n = 1; n <= 5; n++)
        {
            System.out.format ("%3d%10d%10d\n", n, n*n, n*n*n);
        }
               
    }

Output:
Value    Square     Cube
  1         1         1
  2         4         8
  3         9        27
  4        16        64
  5        25       125
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Jul 5th, 2007, 11:03 PM   #9
357mag
Hobbyist Programmer
 
Join Date: Mar 2005
Posts: 148
Rep Power: 4 357mag is on a distinguished road
What if I really like the idea of calling the methods to do the work? Is there a way to combine calling the Square and the Cube method, and still using the format function?
357mag is offline   Reply With Quote
Old Jul 5th, 2007, 11:23 PM   #10
357mag
Hobbyist Programmer
 
Join Date: Mar 2005
Posts: 148
Rep Power: 4 357mag is on a distinguished road
I got it figured out. I did this:

System.out.format("%3d", n);
	            System.out.format ("%10d", square(n));
	            System.out.format("%10d", cube(n));
	            System.out.println()

My way is more work, but I really wanted to use the methods.
357mag is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
advanced user output l2u C++ 6 May 9th, 2007 8:16 PM
Implementation of CRC32 grimpirate Other Web Development Languages 2 Mar 3rd, 2007 2:13 PM
Change the name of output file jazz C 4 Jun 28th, 2006 2:54 AM
Numerical data output to a file can342man C++ 4 Jan 27th, 2006 4:21 PM
It's giving me "Press any key to continue" and no other output.. Insomniac C 15 Jun 5th, 2005 7:07 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:17 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC