Thread: multiplication table

  1. #1
    Registered User
    Join Date
    Feb 2010
    Location
    Va
    Posts
    3

    multiplication table

    I am having trouble getting an asterisk in the corner of my table... I'm allowing a user to input an integer and the program outputs a multiplication table starting with 1 going to the number the user input. I need an asterisk in the top left corner so instead of looking like:
    1 2 3 4 5
    2 4 6 8 10
    3 6 9 12 15
    4 8 12 16 20
    5 10 15 20 25

    it should look like:

    * 1 2 3 4 5
    1 1 2 3 4 5
    2 2 4 6 8 10
    3 3 6 9 12 15
    4 4 8 12 16 20
    5 5 10 15 20 25



    any help on getting it there? im using for loops to create the table

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    What is your current code? After all, this sounds as easy as printing one extra row of numbers at the top, but starting with an asterisk. (Oh, and an extra column on the left, but that just repeats the first number for the row.)
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2010
    Location
    Va
    Posts
    3
    Code:
     #include <stdio.h>
    
    int input_number (void);
    
    int main(void)
    {
    int a,row,column;
    	a=input_number();
    	for(row = 1; row <= a ; row++ )
    		{			
    			for (column = 1; column <=a; column++)
    			{
    				printf("%6.d",row*column);
    			}
    		printf("\n");
    		}
    	return 0;
    }
    
    int input_number(void)
    {
    int b;
    	printf("Input a positive integer ");
    	scanf("%d",&b);
    	return b;
    }
    the problem with just putting a print line at the top is that the user cant control how many integers are contained in the table

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    I suggest that you indent your code a little more consistently:
    Code:
    #include <stdio.h>
    
    int input_number(void);
    
    int main(void)
    {
        int a,row,column;
        a=input_number();
        for(row = 1; row <= a ; row++ )
        {
            for (column = 1; column <=a; column++)
            {
                printf("%6.d",row*column);
            }
            printf("\n");
        }
        return 0;
    }
    
    int input_number(void)
    {
        int b;
        printf("Input a positive integer ");
        scanf("%d",&b);
        return b;
    }
    Now, as I noted earlier, you need to print one more row at the top. Printing this is roughly the same as printing the first actual row, except that you just need to print an asterisk first. You would need to add one more statement in your current outer loop to print that extra left column.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    After you get "a" you print one line.

    You also need a line inside the outer loop (before the inner loop) to print the first number (the vertical index) in each row.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Registered User
    Join Date
    Feb 2010
    Location
    Va
    Posts
    3
    Thanks a bunch!

  7. #7
    Registered User
    Join Date
    Apr 2011
    Posts
    2
    hey all. i was aasign to implement a multiplication table server based on input from client.
    here is the exam ple of output.
    server:waiting for connection
    server:receiving connection from ...
    client:4
    server:1*4=4
    server:2*4=8
    server:3*4=12

    and so on

    client:1*4=4
    client:2*4=8
    ...
    and so on


    really had no idea bout this. i had search all over the web but most of the answer was about the table of multiplication. plis can u help me with this?

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Beej's Guide to Network Programming for network programming.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please help me as fast as possible
    By Xbox999 in forum C Programming
    Replies: 5
    Last Post: 11-30-2009, 06:53 PM
  2. help with structs and malloc!
    By coni in forum C Programming
    Replies: 20
    Last Post: 09-14-2009, 05:38 PM
  3. Multiplication table (need guide)
    By naspek in forum C Programming
    Replies: 21
    Last Post: 07-23-2009, 06:26 AM
  4. Writing array, to file
    By zootreeves in forum C Programming
    Replies: 9
    Last Post: 09-08-2007, 05:06 PM
  5. multiplication table
    By SpEkTrE in forum C Programming
    Replies: 2
    Last Post: 12-09-2003, 04:46 PM