I'm wondering how you do this?

This is a discussion on I'm wondering how you do this? within the C Programming forums, part of the General Programming Boards category; Still learning C programming, but however, here is my code (below) that I wrote ... a multiplication table from 1*1 ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    40

    I'm wondering how you do this?

    Still learning C programming, but however, here is my code (below) that I wrote ... a multiplication table from 1*1 to 9*9 and you can enter # of columns of data you want (from 1-5) and it'll display the same data for the number of columns you chose.


    What is an easy way to rewrite my program's code so that does the multiplication table (same math output as my code below) but you can choose any # for for the number of columns you want the data displayed in? Not really good to use the if a==1, if a==2, etc

    Hopefully you understand what I'm asking (maybe you want to run my code to see what it does and what I'm asking). Thanks! (Also, since I am using Microsoft Visual Studio C++ Express Edition to compile my code, I am required to have the stdafx.h header, else VS gives an error and fails to compile the code)


    Code:
    #include "stdafx.h"
    #include <stdio.h>
    
    int main()
    {
    int a, h, i, j, k, m, n, z;
    
    printf("Enter # of columns: ");
    scanf ("%d", &a);
    
    if (a==1)
    {
    for (i=1; i<10; i++)
    {
    	for (j=1; j<10; j++)
    	{
    		printf("%d x %d = %d", i, j, i*j); 
    		printf("\n");
    	}
    
    	printf("\n");
    }
    }
    
    if (a==2)
    {
    	for (h=1; h<6; h++) 
    	{
    		for (i=1; i<10; i++)
    			{
    				j= 2*h-1;
    				k= 2*h;
    				printf("%d x %d = %d", j, i, j*i);
    			if (k<10)
    				printf("\t%d x %d = %d\n", k, i, k*i);
    			else
    				if (k=10)
    					printf("\n");
    			}
    		printf("\n");
    	}
    }
    
    if (a==3)
    {
    	for (h=0; h<3; h++) 
    	{
    		for (i=1; i<10; i++)
    			{
    				j= 3*h+1;
    				k= j+1;
    				m= k+1;
    
    				printf("%d x %d = %d", j, i, j*i);
    				printf("\t%d x %d = %d", k, i, k*i);
    				printf("\t%d x %d = %d\n", m, i, m*i);
    			}
    		printf("\n");
    
    	}
    }
    if (a==4)
    {
    	for (h=0; h<3; h++) 
    	{
    		for (i=1; i<10; i++)
    			{
    				j= 4*h+1;
    				k= 4*h+2;
    				m= 4*h+3;
    				n= 4*h+4;
    
    			if (j!=9)
    				printf("%d x %d = %d", j, i, j*i);
    			else if (j=9)
    				printf("%d x %d = %d\n", j, i, j*i);
    
    			if (k!=10)
    				printf("\t%d x %d = %d", k, i, k*i);
    						
    			if (m!=11)
    				printf("\t%d x %d = %d", m, i, m*i);
    			
    			
    			if (n!=12)
    				printf("\t%d x %d = %d\n", n, i, n*i);
    			}
    		
    		printf("\n");
    
    	}
    }
    
    if (a==5)
    {
    	for (h=0; h<2; h++) 
    	{
    		for (i=1; i<10; i++)
    			{
    				j= 5*h+1;
    				k= 5*h+2;
    				m= 5*h+3;
    				n= 5*h+4;
    				z= 5*h+5;
    				printf("%d x %d = %d", j, i, j*i);
    				printf("\t%d x %d = %d", k, i, k*i);
    				printf("\t%d x %d = %d", m, i, m*i);
    				printf("\t%d x %d = %d", n, i, n*i);
    			if (z<10)
    				printf("\t%d x %d = %d\n", z, i, z*i);
    			else
    				if (z=10)
    					printf("\n");
    			}
    		
    		printf("\n");
    
    	}
    }
    }
    Last edited by x2x3i5x; 10-21-2009 at 01:19 AM.

  2. #2
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    what i can understand with you logic you want to print tables

    like

    2 X 1 = 2
    2 X 2 = 4

    and so on

    may be this little bit piece of code can help you out

    Code:
    #include <stdio.h>
    
    void print_table(const int number) {
      int index = 1;
      for (; index <= 10; ++index)
        printf("%d\t X %d\t == %d\n", number, index, number * index);
      printf("=======================================================\n\n\n");
      sleep(2);
    }
    
    int main() {
      int number = 0;
      int index  = 1;
      printf("Please enter the column\n");
      scanf("%d", &number);
      for (; index <= number; ++index)
        print_table(index);
    
      return 0;
    }

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    40
    Quote Originally Posted by RockyMarrone View Post
    what i can understand with you logic you want to print tables

    like

    2 X 1 = 2
    2 X 2 = 4

    and so on

    may be this little bit piece of code can help you out

    Code:
    #include <stdio.h>
    
    void print_table(const int number) {
      int index = 1;
      for (; index <= 10; ++index)
        printf("%d\t X %d\t == %d\n", number, index, number * index);
      printf("=======================================================\n\n\n");
      sleep(2);
    }
    
    int main() {
      int number = 0;
      int index  = 1;
      printf("Please enter the column\n");
      scanf("%d", &number);
      for (; index <= number; ++index)
        print_table(index);
    
      return 0;
    }
    What does the sleep (2) do?

  4. #4
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    My intent was to show table of numbers one by one with 2 seconds gap

    do man sleep on your shell or google it

    in normal term sleep will halt the execution for specified number of seconds

  5. #5
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    My intent was to show table of numbers one by one with 2 seconds gap

    do man sleep on your shell or google it

    in normal term sleep will halt the execution for specified number of seconds

  6. #6
    Registered User
    Join Date
    Aug 2006
    Posts
    99
    sleep is completely unnecessary, and just plain silly.

    Don't use tabs to separate your columns. Use specific field width specifiers in your printf calls.

    Code:
    printf("%5d %5d %5d", my_num1, my_num2, my_num3);
    And, the simplest way to accomplish what you are trying to do, is just use two nested loops... The outer loop (assuming you want vertical columns of results) should just run from 1 to 9. The inner loop should run from 1 to your selected number.

    The inner loop will print numbers in rows, the outer loop will print a line feed, and start the inner loop again.

  7. #7
    Registered User
    Join Date
    Sep 2009
    Posts
    40
    but then how do you write the multiplication table part of the code so it will use your loop? My original code's not good for that .....

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The sleep() function was OF COURSE added just to help the OP understand how the table was being printed.

    X2, somehow, you'll want to use two for loops, nested. Whether you like the Rocky's style of having the inner for loop in another function, or not, is up to you.

    Rd's idea might be easier for you to use, and I agree with him about using field width specifiers, to easily align your numbers in the table.

    Code:
    printf(" %6d ", number);
    Will print the variable "number", inside a fixed field width of 6 char's, with the number aligned to the right hand side of the field. The extra spaces before and after the print specifiers, will also be printed, if present.

    so the total width of the print statement above, will be 6 + 2 = 8 digits wide.

  9. #9
    Registered User
    Join Date
    Sep 2009
    Posts
    40
    Ok, I understand your logic here but I was wondering how you get it right?

    So I wanted

    Code:
    1x1=1
    1x2=2
    1x3=3 
    etc
    when you input one into the place where it asks you to enter column number

    and then
    Code:
    1x1=1       2x1 =2, etc
    when you input two into the place where it asks you to enter column number question

    and so on.... and eventually when you enter 9 into the column number, I think it should display the multiplication table into a 1*9 sized table. At least that's what I want it to do (assuming cmd has enough space to put it on one line, else I'll have it say not enough space, so 9 not valid)

    I am really sorry if I'm still asking the same question as I did earlier, still sort of stuck on the matter. :-S

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I thought you were pretty much done, sorry.

    You want columns like:

    Code:
    1x1=1    2x1=2   3x1=3
    1x2=2    2x2=4   3x2=6
    etc. And you want to be able to specify the number of columns you want, across the screen?
    Is that all?

    ez-sqeezy.

    You need to calculate the width of your device line of char's, minus the width of any borders you want (margins), to get a usable line length number of chars.

    From that, you can calculate, after you get the user # for how many columns he wants, and just do the arithmetic on it. You need to figure out the largest number that will be in your table, so we can leave enough room for it.

    That will give you the proper spacing between the columns that can be supported, and whether the number of columns the user has requested, can be accommodated successfully.

    Do you want to work it out, or see a sample? Any questions on the above. It's important to see the line length number of spaces, and work with that. Forget the other stuff, for now.

    Details!!

  11. #11
    Registered User
    Join Date
    Sep 2009
    Posts
    40
    @Adak, thanks for being nice to me


    I'm gonna work through your suggestion to see if I can get it working!!

    I'll basically try making a simple program to see if I can get the right kind of display of the characters. Then I'll add in the multiplication for loop into it.

    If I try for long and still confuse myself, I'll probably just reply to this post with my new questions! Thanks!

    (or if you're willing to send a sample ... I wouldn't mind ... learning from some expert wouldn't hurt ...)
    Last edited by x2x3i5x; 10-21-2009 at 07:21 PM.

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I'm no expert, believe me.

    This is an example of what I was suggesting. It could be shortened, but this is pretty clear I believe.

    Code:
    #include <stdio.h>
    #define Lines 80
    
    int main(void) {
      int columns, field, i, j, lastcol, maxx, spc, totalspc;
      char last;
    //  int c[] = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 };
      maxx = 0;
      field = 3;
      spc = 1;
    
      maxx = Lines / (field + spc);
      printf("\n\n Maximum number of columns available is: %d", maxx);
      do {
        printf("\n How many columns would you like? ");
        scanf("%d", &columns);
        //columns = 6;
        if(columns > maxx)
          printf("That's more than can fit."); 
      }while(columns > maxx);
    
      totalspc = 80 -(field * columns);
    
      printf("\n Do you want the last column to have a space after it [y/n]? ");
      scanf(" %c", &last);  //the space before the % clears the way :)
      if(last == 'Y' || last == 'y') 
        lastcol = 0;
      else
        lastcol = 1;
      spc = totalspc/(columns - lastcol); //last column may not need a space
      printf("\n Our spaces between columns will be %d wide\n", spc);
    
      for(i = 0; i < columns; i++) {
        printf("123");
        for(j = 0; j < spc; j++)
          putchar(' ');
      }
    
      printf("\n\n\t\t\t     press enter when ready");
      while((spc = getchar()) != '\n');
      spc = getchar(); 
    
      return 0;
    }
    I fixed the number field width at 3 digits, but it could also be chosen by the user.

    EDIT:
    ==== Be careful about the words max or Line or line. Those are reserved words in C, and will throw errors like crazy.
    Last edited by Adak; 10-21-2009 at 11:10 PM.

  13. #13
    Registered User
    Join Date
    Sep 2009
    Posts
    40
    I try a different way, a little easier for me to look at (see below code) but I'm having a little problem getting the multiplication to work to do the columns like the nested for loops for the rows and columns are supposed to be working. (I marked problem spot in red).

    Code:
    #include "stdio.h"
    #include <stdio.h>
    
    int main()
    {
    	int column, total, row_value, xrow, xcol, i, j;
    
    	i=1;
    	
    	printf("Enter column #: ");
    	scanf("%d", &column);
    
    	printf("Enter max value: ");
    	scanf("%d", &total);
    	
    
    	row_value = row(total, column);
    	
    	
    	for(xrow=0; xrow<row_value; xrow++)
    			{
    				for(xcol=0; xcol<column; xcol++)
    						{					
    							for (i=1; i<10; i++)
    								{
    									for (j=1; j<10; j++)
    									{
    										printf("%d x %d = %d\t", i, j, i*j); 
    										
    									}
    										
    									}
    												
    						
    				}
    }
    			
    }
    
    	int row(int total, int column)
    		{
    			int x, y;
    
    			x = total / column;
    			y = column * x;
    
    			if (y==total)
    				return x;
    
    			else 
    				return x+1;
    		}

  14. #14
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    This is a simpler logic than I was thinking of earlier. Very much like what you're working with:

    Code:
      num = 0;
      j = 1;
      while(num < 20) {    //20 rows: 1x1 to 1x20
      i = 1;
      for(c = 0; c < columns; c++) {
        printf("%2d x %2d = %3d", i, j, i * j);
        if(c < columns-1 ) {
          for(k = 0; k < spc; k++)
            putchar(' ');
        }
        i++;
      }
        ++j;  
        num++;
        putchar('\n');
      }
    One question and one request:

    1Q) Why do you have stdio.h included twice for your program. Either the stdio.h file is in the default folder (so you use <stdio.h>, or it's in one of the other folders that your compiler automatically searches through to find it, so you use "stdio.h". You don't need both.

    1R) Please cut down your tabs to 6 or 7 spaces (and have your editor use spaces, instead of just a tab char). Your code braces are all over the bloody page, and can't begin to line up. That's very hard to debug.

    I used my program above to write and test this bit of code. I have deleted from that program, all the code related to the last column having a space of char's after it, or not, and improved the line arithmetic, as well.
    Last edited by Adak; 10-24-2009 at 03:41 AM.

  15. #15
    Registered User
    Join Date
    Sep 2009
    Posts
    40
    Should have said #include "stdafx.h" NOT "stdio.h" ... Sorry and yes, I'll fix the code braces. I noticed it happened and yeah, writing good looking code is important.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. switch/case - Just wondering
    By Snip in forum C Programming
    Replies: 3
    Last Post: 05-22-2005, 08:04 AM
  2. just wondering
    By sreetvert83 in forum C++ Programming
    Replies: 3
    Last Post: 04-26-2003, 09:24 PM
  3. wondering about people here
    By moemen ahmed in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 07-08-2002, 10:33 PM
  4. wondering
    By xlordt in forum A Brief History of Cprogramming.com
    Replies: 35
    Last Post: 03-01-2002, 07:05 AM

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