multiplication table

This is a discussion on multiplication table within the C Programming forums, part of the General Programming Boards category; I am having trouble getting an asterisk in the corner of my table... I'm allowing a user to input an ...

  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
    22,313
    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.)
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    22,313
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    yat
    yat is offline
    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
    32,855
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 06:38 PM
  3. Multiplication table (need guide)
    By naspek in forum C Programming
    Replies: 21
    Last Post: 07-23-2009, 07:26 AM
  4. Writing array, to file
    By zootreeves in forum C Programming
    Replies: 9
    Last Post: 09-08-2007, 06:06 PM
  5. multiplication table
    By SpEkTrE in forum C Programming
    Replies: 2
    Last Post: 12-09-2003, 04:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21