Hard Light Productions Forums
Off-Topic Discussion => General Discussion => Topic started 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!
-
printf("%d", num1);
Use printf to build the table, you don't need math.h to multiply stuff.
-
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?
-
I'd use for-loops, increment each row differently as the multiples increase. :)
-
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|
-
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.
-
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;
-
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--". :)
-
Woo ok, got everything to work.
Thanks guys!
-
Y'know, you really should have just used two nested loops for this. Something like:
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.
-
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;
-
Dude, you forgot about the postincrement operator already?
-
isn't postincrement (and preincrement) C++ things, and not C?
-
I'm going to have to hurt you now.
-
My memory is not so good. Sorry.
-
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.
-
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++
-
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++;
-
"Free the mallocs!"
I mean...
"Free the mallocs!";
-
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.
-
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.