Basic but complicated problem....Help me Plz!!!

  • Thread starter Freshman_in_C_Programming
  • Start date
F

Freshman_in_C_Programming

Our teacher gave us a problem and it goes this way:

Create a Turbo C Program that will let a user enter two
integer numbers. The
program must display the product of two numbers without
using multiplication(*)
operator.

He also told us to use the "do-while loop."

I don't know how to solve the problem because i'm just a freshman in C
programming. I know there are lots of expert programmers in this group,
so please help me.


P.S. I can't proceed to sophomore years in programming if I can't solve
the problem...

hu hu hu...
 
V

Vladimir S. Oka

Freshman_in_C_Programming said:
Our teacher gave us a problem and it goes this way:

Solving homework is not very popular in these parts...
Create a Turbo C Program that will let a user enter two
integer numbers. The
program must display the product of two numbers without
using multiplication(*)
operator.

He also told us to use the "do-while loop."

Your question is off topic here until you have a specific question
about standard C language (i.e. no Turbo C extensions). Once you give
it a try, do come back and ask if you still have problems.
I don't know how to solve the problem because i'm just a freshman in C
programming. I know there are lots of expert programmers in this group,
so please help me.

It seems that you don't know how to solve this as you're even greener
in maths. Go back and revise "multiplication". If I were you, I
wouldn't bother with C until you've master this.
P.S. I can't proceed to sophomore years in programming if I can't solve
the problem...

And well you shouldn't! How'd you pass maths in the first place?
hu hu hu...

I bet!

Cheers

Vladimir
 
P

Pedro Graca

Freshman_in_C_Programming said:
Our teacher gave us a problem and it goes this way:

Create a Turbo C Program that will let a user enter two
integer numbers. The
program must display the product of two numbers without
using multiplication(*)
operator.

Just remember multiplication is repeated addition:

6 * 4

is

4 + 4 + 4 + 4 + 4 + 4 (6 times)


For extra credit you might want to turn it around and do

6 + 6 + 6 + 6 (4 times)
 
B

Barry

Vladimir S. Oka said:
Solving homework is not very popular in these parts...


Your question is off topic here until you have a specific question
about standard C language (i.e. no Turbo C extensions). Once you give
it a try, do come back and ask if you still have problems.


It seems that you don't know how to solve this as you're even greener
in maths. Go back and revise "multiplication". If I were you, I
wouldn't bother with C until you've master this.


And well you shouldn't! How'd you pass maths in the first place?


I bet!

Cheers

Vladimir

Whip us up a simple example program where

3.14157 and 6.02*10^23

are the numbers.
 
F

Freshman_in_C_Programming

I tried it many times...but it seems not working.

My program goes like this:

_____________________________________________

#include<stdio.h>
main()
{
int x,y,product;
product=0;
clrscr();
printf("Enter first number: ");
scanf("%d",&a);
printf("Enter second number: ");
do{
product=a*b;
printf("The product of two numbers is %d", product);
} while(a==0&&b=0);
getch();
}

_____________________________________________

That was my original program and it ran correctly.
Then our teacher told us not to use multiplication operator ( * )....
and I thought for a moment..."God! can I do that?"
Does it sound easy...?
My problem is...how will I initialize the variable product.

P.S. My first initialization is = product=a*b;
 
S

santosh

scanf("%d %d", &a, &b) ;

do
{
if(b == 0 )
break ;
a = a + a ;
b-- ;
}while(1) ;


Enjoy...............
 
V

Vladimir S. Oka

Freshman_in_C_Programming said:
I tried it many times...but it seems not working.

My program goes like this:

No it does not!

Don't re-type your code here. Copy and paste _exact_ code you used.
This one won't compile and is broken in other ways too. See below.

BTW, since you re-typed it, you could have fixed that horrible
indentation.
#include<stdio.h>
main()

Avoid implicit `int` and do a proper

int main(void)

or

int main(int argc, char *argv[])
{
int x,y,product;

An `int` is not guaranteed to be able to hold the product of two
`int`s. You'd have been better off declaring `product` as `long`, but
that would not have been guaranteed to hold your product either (
sizeof(int) <= sizeof(long) ). You'd really want to check `x` and `y`
before you multiply them.

I prefer declarations on separate lines, at least the ones that are
logically separate:

int x, y;
long product;

Would have been much clearer.
product=0;

First initialisation of `product` (c.f. your own PS).
clrscr();

This is not a standard function. You also don't need it anyway.
printf("Enter first number: ");

You really want to terminate `printf`s with '\n', otherwise there's no
obligation on the part of the system to output anything.
scanf("%d",&a);

Don't use scanf(). Use fgets()/sscanf() combination instead. Search
this group for detailed discussion as to why (also why fgets() and
_not_ gets()).

Also, you never declared `a` (or `b` for that matter).
printf("Enter second number: ");

You don't seem to...

Here, I'd first check whether a*b can fit into product, and output a
diagnostic message if it won't.
product=a*b;
printf("The product of two numbers is %d", product);
} while(a==0&&b=0);

The comparison is broken. You probably meant `b==0`.

For that matter, relying on specific input parameter values as an exit
condition is generally not a good idea, and especially when what you do
with them is actually _defined_.
getch();
}

_____________________________________________

That was my original program and it ran correctly.

No it did not. What does it do if `a` and `b` are both == 0? Or was
hammering the fact that 0*0 = 0 into the poor user part of the
assignment? ;-)
Then our teacher told us not to use multiplication operator ( * )....
and I thought for a moment..."God! can I do that?"

Oh dear, here we go again... :'-(
Go back to where they taught you what multiplication is...
Does it sound easy...?

It actually does!
My problem is...how will I initialize the variable product.

No, that's /not/ what your problem is. The problems are listed above.
P.S. My first initialization is = product=a*b;

No it is not. See above.

Cheers

Vladimir
 
A

August Karlstrom

Vladimir said:
Freshman_in_C_Programming wrote:


You really want to terminate `printf`s with '\n', otherwise there's no
obligation on the part of the system to output anything.

Or use the (standard) fflush function.


August
 
K

kingshriek

Repeated addition is a poor way to represent multiplcation. A
combination of addition and bit shifting is far more efficient.

long mult(int a,int b) {
int c,p;
p = 0;
while(b > 0) {
c = b & 0x1;
if(c==1) { p = p + a; }
a <<= 1;
b >>= 1;
}
return p;
}

The above only works for nonnegative b, but is trivial to modify to
take this into account.
 
S

Stefan Krah

* santosh said:
scanf("%d %d", &a, &b) ;

do
{
if(b == 0 )
break ;
a = a + a ;
b-- ;
}while(1) ;

Enjoy...............

.... while a is repeatedly doubled. Did you actually test your code?


Stefan Krah
 
J

Jordan Abel

Whip us up a simple example program where

3.14157 and 6.02*10^23

Those are some funny-looking "integer numbers" you've got there - one is
only an integer for lack of precision, the other is certainly not one
at all.
 
V

Vladimir S. Oka

^^^^^^^^^^^^^^^^^^^
|
NB! ---------------+
Whip us up a simple example program where

For the first one I could, for the second one hardly...
3.14157 and 6.02*10^23
^^^^^^^^^^ invalid operands to binary ^, says gcc

....unless you really meant 6.02e23 (or 6.02 * (10 ^ 23))?
> are the numbers.

Please read the OP before trying to be funny.

Also, if you knew some /more/ maths you'd have known that this too can
be done without multiplication. Finding out how is left as an exercise
for the reader...

Cheers

Vladimir
 
D

Default User

Barry wrote:

Whip us up a simple example program where

3.14157 and 6.02*10^23

are the numbers.

I guess you missed the part where the OP said:


Last I checked, those numbers you reference aren't integers.




Brian
 
K

Keith Thompson

Freshman_in_C_Programming said:
Our teacher gave us a problem and it goes this way:

Create a Turbo C Program that will let a user enter two
integer numbers. The
program must display the product of two numbers without
using multiplication(*)
operator.

He also told us to use the "do-while loop."

I don't know how to solve the problem because i'm just a freshman in C
programming. I know there are lots of expert programmers in this group,
so please help me.


P.S. I can't proceed to sophomore years in programming if I can't solve
the problem...

Give us your instructor's e-mail address so we can submit the solution
directly. Give us your real name so we can tell him whose homework
we're doing.

Or, if you're having trouble, you might consider talking to your
instructor, or to a teaching assistant, or opening a book.
hu hu hu...

Indeed.
 
J

Julian V. Noble

Freshman_in_C_Programming said:
I tried it many times...but it seems not working.

My program goes like this:

Several people have told you to go back and review some maths, in
particular the definition of multiplication. Basically multiplication
is repeated addition. In fact, that's exactly how a digital computer
does it (except it does many operations in parallel to save time).

You might want to consult Knuth, "The Art of Computer Programming".
There is no law that you must confine yourself to yout course text.
(Of course, your course text may well have defined and explained
multiplication. Have you skipped that part?)

--
Julian V. Noble
Professor Emeritus of Physics

http://galileo.phys.virginia.edu/~jvn/

"For there was never yet philosopher that could endure the
toothache patiently."

-- Wm. Shakespeare, Much Ado about Nothing. Act v. Sc. 1.
 
R

Richard Heathfield

Julian V. Noble said:
Several people have told you to go back and review some maths, in
particular the definition of multiplication. Basically multiplication
is repeated addition. In fact, that's exactly how a digital computer
does it (except it does many operations in parallel to save time).

I'd be very disappointed in a digital computer that did multiplication by
repeated addition.

Consider multiplying 123 by 456. Repeated addition is going to require a
minimum of 123 operations (a mov, followed by 122 adds). But you can do it
this way instead:

101100100
x 1111011
---------
101100100
1011001000
00000000000
101100100000
1011001000000
10110010000000
101100100000000
---------------

That's a mov, a handful of tests of a bit against 0, and half a dozen
shifts, plus one add per set bit in one of the multiplicands.

No doubt a Real Programmer could bum a few more cycles out to make the whole
thing even more tense, but even without that, there's absolutely no need
for repeated addition.


(The posting of this article was somewhat delayed whilst I deleted over a
kilobyte of junk, typed by a cat!)
 
J

Julian V. Noble

Richard said:
Julian V. Noble said:


I'd be very disappointed in a digital computer that did multiplication by
repeated addition.

Consider multiplying 123 by 456. Repeated addition is going to require a
minimum of 123 operations (a mov, followed by 122 adds). But you can do it
this way instead:

101100100
x 1111011
---------
101100100
1011001000
00000000000
101100100000
1011001000000
10110010000000
101100100000000
---------------

That's a mov, a handful of tests of a bit against 0, and half a dozen
shifts, plus one add per set bit in one of the multiplicands.

No doubt a Real Programmer could bum a few more cycles out to make the whole
thing even more tense, but even without that, there's absolutely no need
for repeated addition.

(The posting of this article was somewhat delayed whilst I deleted over a
kilobyte of junk, typed by a cat!)

Of course. That, in fact, is how we learn to do it in grade school (except
not in binary, most places). If that is not repeated addition, however,
how would YOUR cat describe it?

(Said cat sounds intelligent--has it begun writing Shakespearean sonnets
yet?)


--
Julian V. Noble
Professor Emeritus of Physics

http://galileo.phys.virginia.edu/~jvn/

"For there was never yet philosopher that could endure the
toothache patiently."

-- Wm. Shakespeare, Much Ado about Nothing. Act v. Sc. 1.
 
E

Ed Prochak

Stefan said:
... while a is repeatedly doubled. Did you actually test your code?


Stefan Krah

SShhh...

<in a hushed voice...>
he is trying to hint at the correct answer with out giving it away.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top