Author Topic: Could anyone help with C-language?  (Read 2167 times)

0 Members and 1 Guest are viewing this topic.

Offline Jeryko

  • 26
Could anyone help with C-language?
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!
JBX-Phoenix

 

Offline WMCoolmon

  • Purveyor of space crack
  • 213
Re: Could anyone help with C-language?
printf("%d", num1);

Use printf to build the table, you don't need math.h to multiply stuff.
-C

  

Offline Jeryko

  • 26
Re: Could anyone help with C-language?
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?
JBX-Phoenix

 

Offline Setekh

  • Jar of Clay
  • 215
    • Hard Light Productions
Re: Could anyone help with C-language?
I'd use for-loops, increment each row differently as the multiples increase. :)
- Eddie Kent Woo, Setekh, Steak (of Steaks), AWACS. Seriously, just pick one.
HARD LIGHT PRODUCTIONS, now V3.0. Bringing Modders Together since January 2001.
THE HARD LIGHT ARRAY. Always makes you say wow.

 

Offline Jeryko

  • 26
Re: Could anyone help with C-language?
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|
JBX-Phoenix

 

Offline Setekh

  • Jar of Clay
  • 215
    • Hard Light Productions
Re: Could anyone help with C-language?
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.
- Eddie Kent Woo, Setekh, Steak (of Steaks), AWACS. Seriously, just pick one.
HARD LIGHT PRODUCTIONS, now V3.0. Bringing Modders Together since January 2001.
THE HARD LIGHT ARRAY. Always makes you say wow.

 

Offline Jeryko

  • 26
Re: Could anyone help with C-language?
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;
JBX-Phoenix

 

Offline Setekh

  • Jar of Clay
  • 215
    • Hard Light Productions
Re: Could anyone help with C-language?
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--". :)
- Eddie Kent Woo, Setekh, Steak (of Steaks), AWACS. Seriously, just pick one.
HARD LIGHT PRODUCTIONS, now V3.0. Bringing Modders Together since January 2001.
THE HARD LIGHT ARRAY. Always makes you say wow.

 

Offline Jeryko

  • 26
Re: Could anyone help with C-language?
Woo ok, got everything to work.

Thanks guys!
JBX-Phoenix

 

Offline ZylonBane

  • The Infamous
  • 29
Re: Could anyone help with C-language?
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.
ZylonBane's opinions do not represent those of the management.

 

Offline Jeryko

  • 26
Re: Could anyone help with C-language?
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;
JBX-Phoenix

 

Offline ZylonBane

  • The Infamous
  • 29
Re: Could anyone help with C-language?
Dude, you forgot about the postincrement operator already?
ZylonBane's opinions do not represent those of the management.

 

Offline Anaz

  • 210
Re: Could anyone help with C-language?
isn't postincrement (and preincrement) C++ things, and not C?
Arrr. I'm a pirate.

AotD, DatDB, TVWP, LM. Ph34r.

You WILL go to warpstorm...

 

Offline ZylonBane

  • The Infamous
  • 29
Re: Could anyone help with C-language?
I'm going to have to hurt you now.
ZylonBane's opinions do not represent those of the management.

 

Offline Jeryko

  • 26
Re: Could anyone help with C-language?
My memory is not so good.  Sorry.
JBX-Phoenix

 

Offline Setekh

  • Jar of Clay
  • 215
    • Hard Light Productions
Re: Could anyone help with C-language?
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.
- Eddie Kent Woo, Setekh, Steak (of Steaks), AWACS. Seriously, just pick one.
HARD LIGHT PRODUCTIONS, now V3.0. Bringing Modders Together since January 2001.
THE HARD LIGHT ARRAY. Always makes you say wow.

 
Re: Could anyone help with C-language?
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++
'And anyway, I agree - no sig images means more post, less pictures. It's annoying to sit through 40 different sigs telling about how cool, deadly, or assassin like a person is.' --Unknown Target

"You know what they say about the simplest solution."
"Bill Gates avoids it at every possible opportunity?"
-- Nuke and Colonol Drekker

 

Offline WMCoolmon

  • Purveyor of space crack
  • 213
Re: Could anyone help with C-language?
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++;
-C

 

Offline Setekh

  • Jar of Clay
  • 215
    • Hard Light Productions
Re: Could anyone help with C-language?
"Free the mallocs!"

I mean...

"Free the mallocs!";
- Eddie Kent Woo, Setekh, Steak (of Steaks), AWACS. Seriously, just pick one.
HARD LIGHT PRODUCTIONS, now V3.0. Bringing Modders Together since January 2001.
THE HARD LIGHT ARRAY. Always makes you say wow.

 
Re: Could anyone help with C-language?
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.
'And anyway, I agree - no sig images means more post, less pictures. It's annoying to sit through 40 different sigs telling about how cool, deadly, or assassin like a person is.' --Unknown Target

"You know what they say about the simplest solution."
"Bill Gates avoids it at every possible opportunity?"
-- Nuke and Colonol Drekker