Thread: multiplication tables using for statements

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    20

    multiplication tables using for statements

    I'm having severe difficulty creating a multiplication table. I need to create a multiplication table that looks like this by multiplying i*j

    1*1= 1 1* 2=2 1* 3=3 1* 4=4 1* 5=5
    2*1= 2 2* 2=4 2* 3=6 2* 4=8 2* 5=10
    3*1= 3 3* 2=6 3* 3=9 3 * 4=12 3* 5=15
    4*1= 4 4* 2=8 4*3=12 4* 4=16 4* 5=20
    5 1= 5 5* 2=10 5*3=15 5* 4=20 5* 5=25
    This is what I have. I can get a table of i*j I just need to figure out how to put a for statement that could increment itself 5 times for j and start over and then have i increment by 1.
    Code:
    #include <stdio.h>
    #include "conio.h"
    
    const int num_rows = 5;
    const int num_columns = 5;
    
    int main(void)
    {
    	int row, column, i, j;
    	
    	printf("i*j\t", '*');
    	for (i= 1; i<=5; i++)
    		if (i <= 5)
    			break;
    	printf( "%d", i);
    	
    	for (column = 1; column <num_columns; column++)
    		printf("i*j\t", column);
    	putchar('\n');
    	for (i= 1; i<=5; i++)
    		if (i <= 5)
    			break;,md
    	
    	
    	
    	for (row =1; row <num_rows; row++)
    	{
    		printf("i*j\t", row);
    
    				for(column =1; column < num_columns; column++)
    		{
    			printf("i*j\t", row *column);
    		}
    		printf("\n");
    	}
    	_getch();
    	return 0;
    }
    Thanks everyone.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Here are some warnings to think about.
    Code:
    $ gcc -std=c99 -Wall baz.c
    baz.c: In function ‘main’:
    baz.c:10: warning: too many arguments for format
    baz.c:17: warning: too many arguments for format
    baz.c:25: warning: too many arguments for format
    baz.c:29: warning: too many arguments for format
    baz.c:8: warning: unused variable ‘j’
    They refer to most of your printf statements, which are not doing what you think they are.

    You only need the last pair of loops, if you do it right.
    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.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    10
    wow your code is very long, and not vary readable, here is what I would do:
    Code:
        int i;
        for(i=0; i<25; i++){
            printf(" %-1d*%-1d=%-2d %c", (i/5)+1, (i%5)+1, ((i/5)+1)*((i%5)+1), (i%5-4)?' ':'\n');
        }

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    It's a lot more readable than that one-line cryptic horror.
    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.

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    10
    Code:
    const int num_rows = 5;
    it is better practice to use #define 5 N

    Code:
    printf("i*j\t", column);
    printf("i*j\t", row *column);
    I don't know what u expect this to do.

    Code:
    break;,md
    or this

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    10
    Quote Originally Posted by Salem View Post
    It's a lot more readable than that one-line cryptic horror.
    hhhhhhhhh cryptic horror, just wanted to show that it doesn't have to be so long.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    And you think yours will be any less, when you replace all those literals with the symbolic names in the original?

    Or what you would do, if you had to do anything else other than just print the answer (basically delete the code and start again with for loops).
    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.

  8. #8
    Registered User
    Join Date
    Jun 2011
    Posts
    20
    So should I use the const int for number of rows and columns or can I do this with declaring i and j as int? I'm kinda lost on how I can do that though, I know that I should be able to use two "for" loops but I have no idea how to go about doing this. If I could program this right it would be due to serendipity because I don't really understand these for statements, especially in this application. Thanks everyone!

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    This is all you need.
    Code:
    	for (row =1; row <num_rows; row++)
    	{
    		printf("i*j\t", row);
    
    				for(column =1; column < num_columns; column++)
    		{
    			printf("i*j\t", row *column);
    		}
    		printf("\n");
    	}
    The only thing you need to do is figure out how to fix the inner printf statement so it prints say 5*2=10 rather than just i*j all the time.

    You're pretty much there, you just need to tweak the right bits into the right place.
    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.

  10. #10
    Registered User
    Join Date
    Jun 2011
    Posts
    20
    This is what I have so far
    Code:
    #include <stdio.h>
    #include "conio.h"
    
    
    
    const int num_rows = 5;
    const int num_columns = 5;
    
    int main(void)
    {
    	int row, column, i,j result;
    	
    	result = i * j ;
    	for ( i=1; row < num_rows; row++) //for (row =1; row <num_rows; row++)
    	
    	{
    	
    		printf("%d*%d=%d\t", i,j, result);
    		printf("\n");
    		for (j =1; j < num_columns; j++)
    		
    		{
    			printf("%d*%d=%d\t", i, j, result);
    			printf("\n");
    		}
    		printf("\n");
    	}
    I was hoping changing the for statements and the printf statements around would fix the problem but it hasn't. It keeps telling me i and j are being used without being initialized but I can't figure out why Thanks everyone.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > result = i * j ;
    Move this inside both for loops.
    You only need one print statement printing values.
    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.

  12. #12
    Registered User
    Join Date
    Jun 2011
    Posts
    20
    Yay I sort of got it to work but now the formatting is off and I can't fix it with \n.

    Code:
    #include <stdio.h>
    #include "conio.h"
    
    int main(void)
    {
    int i, j;
    
    for (i = 1; i < 6; i++)
    	{	printf("\t");
    	
    for (j = 1; j < 6; j++)	
    	printf("\t");
    
    printf("%d*%d = %d\t", i, j, (i * j));
    
    }
    }
    
    _getch(); 
    return 0;
    }

  13. #13
    Registered User
    Join Date
    Jun 2011
    Posts
    20
    or \t

  14. #14
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    You are basically there. The loops are still a little overworked. Here is one possible solution:
    Code:
    #include <stdio.h>
    #include "conio.h"
    
    #define NUM_ROWS 5
    #define NUM_COLUMNS 5
    
    int main(void)
    {
    	for (int column = 1; column < NUM_COLUMNS+1; column++){
    		for (int row = 1; row < NUM_ROWS+1; row++){	
    			printf("%d*%d = %d \t", row, column, (row * column)); 
                                                    //A space is between %d and \t
    		}
    		printf("\n");
    	}
    
    	_getch();	
    	return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with tables in C++
    By ryanE in forum C++ Programming
    Replies: 1
    Last Post: 11-10-2010, 01:22 PM
  2. tables
    By kgott in forum C Programming
    Replies: 3
    Last Post: 10-19-2006, 09:39 AM
  3. tables...
    By peanut in forum C Programming
    Replies: 21
    Last Post: 10-17-2006, 03:06 PM
  4. tables
    By Frank_Rye in forum C Programming
    Replies: 2
    Last Post: 10-24-2005, 11:57 AM
  5. tables?
    By jobolikescake in forum C++ Programming
    Replies: 6
    Last Post: 09-04-2002, 09:24 PM