Hard Light Productions Forums

Off-Topic Discussion => General Discussion => Topic started by: Jeryko on November 10, 2005, 07:56:17 pm

Title: Could anyone help with C-language?
Post by: Jeryko on November 10, 2005, 07:56:17 pm
I'm working on a project for my CompSci class and I can't get one part down.

I need the program to be able to scan in a numer (num1) from the user (I have got this part no prob)
Then I need the program to print a multiplication table with dimensions num1 x num1 (height x width)

This is basic 'C' programming, not C++ and I CAN'T use the <math.h> library.

Anyone think they can give me a hand?

Thanks!
Title: Re: Could anyone help with C-language?
Post by: WMCoolmon on November 10, 2005, 08:40:32 pm
printf("%d", num1);

Use printf to build the table, you don't need math.h to multiply stuff.
Title: Re: Could anyone help with C-language?
Post by: Jeryko on November 10, 2005, 09:41:43 pm
Yeah figured math.h had nothing to do with it.

So the easiest way to do this is to use a bunch of while-loops then I guess?
Title: Re: Could anyone help with C-language?
Post by: Setekh on November 10, 2005, 09:50:50 pm
I'd use for-loops, increment each row differently as the multiples increase. :)
Title: Re: Could anyone help with C-language?
Post by: Jeryko on November 10, 2005, 10:13:13 pm
Wow, ok, didn't even think of for-loops.

Here is what I have:

         case 'M':
         case 'm':
            printf("Please enter the maximum multiplier/multiplicand\n");
            printf("you would like to see in the mulitplication table: \n");
            scanf("%d", &table);
            for(i = 0; i < 100; i = i + 1)
            {
               if(i == table)
               {
                  break;
               }
               printf("%d", i);
            }
            break;
(and that works how its supposed to)

How do I make it so it

printf("%d", i);

'table' number of times on successive lines and have dashes under the first row and column?  ( \n goes in there I know)


Ie.

         1    2    3    4    5    6    7
         ------------------------------
1|     1    2    3    4    5    6    7
2|     2    4    6    8   10  12  14
3|
4|
5|          etc. etc. etc.
6|
7|
Title: Re: Could anyone help with C-language?
Post by: Setekh on November 10, 2005, 10:20:16 pm
You know instead of saying "i = i + 1", you can just say "i++"? :)

Also, you shouldn't use 100 there - it's a magic number. You probably want to define it as i * i instead, otherwise if you use a number larger than 10, you're going to be in trouble.
Title: Re: Could anyone help with C-language?
Post by: Jeryko on November 10, 2005, 10:40:58 pm
Ok here is what I have now.  What this does is print an endless loop of 0's no matter what value I put in.  Any idea what I've got wrong. I have the while loop in there to keep it printing un til the tblCount reaches 0.  At that point, the table should stop printing at the user specified number...  grr it all make sense in my head.

         case 'M':
         case 'm':
            printf("Please enter the maximum multiplier/multiplicand\n");
            printf("you would like to see in the mulitplication table: \n");
            scanf("%d%c", &table, &newline);
            tblCount = table;
            while(tblCount > 0)
            {
               for(i = 0; i < 10; i = i++)
               {
                  if(i == table)
                  {
                     break;
                  }
                  printf("%d", i);
               }
               tblCount = tblCount - 1;
            }
            break;
Title: Re: Could anyone help with C-language?
Post by: Setekh on November 10, 2005, 10:42:47 pm
Better; but it shouldn't be "i = i++", it should be just "i++". Read it as "increment i once". Same with "tblCount = tblCount - 1"; just replace the whole line with "tblCount--". :)
Title: Re: Could anyone help with C-language?
Post by: Jeryko on November 11, 2005, 09:00:15 am
Woo ok, got everything to work.

Thanks guys!
Title: Re: Could anyone help with C-language?
Post by: ZylonBane on November 11, 2005, 09:23:13 am
Y'know, you really should have just used two nested loops for this. Something like:

Code: [Select]
for (i=1; i<=table; i++) {
     for (t=1; t<=table; t++) {
          printf("%d", i*t);
     }
     printf("\n");
}

This way the code mirrors what's actually happening, versus your code, which seems to be flailing around a bit.
Title: Re: Could anyone help with C-language?
Post by: Jeryko on November 11, 2005, 11:04:01 am
I ended up doing this:

            printf("  ");
            for(a = 1; a <= table; a = a + 1)
            {
               printf(" %3d", a);
            }
            printf("\n");
            for(b = 1; b <= table; b = b + 1)
            {
               printf("%2d", b);
               for(a = 1; a <= table; a = a + 1)
               {
                  printf(" %3d", b * a);
               }
               printf("\n");
            }
            break;
Title: Re: Could anyone help with C-language?
Post by: ZylonBane on November 11, 2005, 01:18:29 pm
Dude, you forgot about the postincrement operator already?
Title: Re: Could anyone help with C-language?
Post by: Anaz on November 11, 2005, 03:18:10 pm
isn't postincrement (and preincrement) C++ things, and not C?
Title: Re: Could anyone help with C-language?
Post by: ZylonBane on November 11, 2005, 03:55:02 pm
I'm going to have to hurt you now.
Title: Re: Could anyone help with C-language?
Post by: Jeryko on November 11, 2005, 10:21:39 pm
My memory is not so good.  Sorry.
Title: Re: Could anyone help with C-language?
Post by: Setekh on November 12, 2005, 03:31:46 am
Those increment operators are essential when you get to messing with pointers, lists and arrays. The basic difference between C and C++ is that the latter is designed to be OO; stuff like increment operators are much older than C++. :)

Zylon has another point, though. You could learn a lot from his design - what you probably need to do is sit down and think about your design a little more carefully before putting finger to keyboard. All your real thinking time should be spent coming up with an algorithmically-sound program, and less with the specifics of syntax and such.
Title: Re: Could anyone help with C-language?
Post by: Descenterace on November 12, 2005, 04:26:25 am
Some people should not be allowed to write in unmanaged languages.

What's going to happen when this program requires dynamic memory allocation? :p

MEMORYLEAK++
Title: Re: Could anyone help with C-language?
Post by: WMCoolmon on November 12, 2005, 04:39:21 am
Some people should not be allowed to write in unmanaged languages.

What's going to happen when this program requires dynamic memory allocation? :p

MEMORYLEAK++

MEMORYLEAK++;
Title: Re: Could anyone help with C-language?
Post by: Setekh on November 12, 2005, 04:42:27 am
"Free the mallocs!"

I mean...

"Free the mallocs!";
Title: Re: Could anyone help with C-language?
Post by: Descenterace on November 13, 2005, 09:42:18 am
Some people should not be allowed to write in unmanaged languages.

What's going to happen when this program requires dynamic memory allocation? :p

MEMORYLEAK++

MEMORYLEAK++;

Since that was intended only as a clause in a larger (omitted) expression, it didn't need a semicolon.

Semicolons are only for line endings.
Title: Re: Could anyone help with C-language?
Post by: ZylonBane on November 13, 2005, 10:11:47 am
Zylon has another point, though. You could learn a lot from his design
I think he did. Compare the most recent code he posted with the second-most-recent. It seems like pretty much the optimal way to produce the required formatted output.