473,386 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

multiplication coding fault

This code is meant to keep prompting for user input (integers), and when 0 is pressed it is meant to view the product of all the entered numbers apart from 0.
It works fine, but when I hit 0 to view the product, it shows 0.
Here's the code:



Expand|Select|Wrap|Line Numbers
  1. if (a == 10)
  2. {
  3. int l;
  4. int m;
  5. cout<< "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx \n \n";
  6. cout << "Activated multiplying function... \n \n";
  7. while (1){
  8. cout << "Enter one number to multiply ( 0 to find total ): \n";
  9. cin >> l;
  10. m = m*l;
  11. if (l == 0)
  12. {
  13. cout << "PRODUCT:" << endl;
  14. cout << m << endl;
  15. cout << endl;
  16. cout<< "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx \n \n";
  17. break;
  18. }
  19. }
  20. }
Can anyone see the problem here, and how I can fix it?

Thanks

:P
Aug 16 '07 #1
14 1529
This code is meant to keep prompting for user input (integers), and when 0 is pressed it is meant to view the product of all the entered numbers apart from 0.
It works fine, but when I hit 0 to view the product, it shows 0.
Here's the code:



Expand|Select|Wrap|Line Numbers
  1. if (a == 10)
  2. {
  3. int l;
  4. int m;
  5. cout<< "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx \n \n";
  6. cout << "Activated multiplying function... \n \n";
  7. while (1){
  8. cout << "Enter one number to multiply ( 0 to find total ): \n";
  9. cin >> l;
  10. m = m*l;
  11. if (l == 0)
  12. {
  13. cout << "PRODUCT:" << endl;
  14. cout << m << endl;
  15. cout << endl;
  16. cout<< "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx \n \n";
  17. break;
  18. }
  19. }
  20. }
Can anyone see the problem here, and how I can fix it?

Thanks

:P

I see your problem here. You are multiplying the breaking 0 with the total. Anything multiplied by 0 is 0. But anything multiplied by 1 , is the same thing. so try doing a bit of manipulation to the code : )
Aug 16 '07 #2
JosAH
11,448 Expert 8TB
Expand|Select|Wrap|Line Numbers
  1. cin >> l;
  2. m = m*l;
  3. if (l == 0)
  4. {
  5. // display m ...
  6. }
Can anyone see the problem here, and how I can fix it?
Can't you see the problem? Suppose I enter 0, so zero is assigned to l ('ell')
and m is multiplied by ell (i.e. zero (0)) ... and then ell is tested for zero.

Somone already warned you for this logic mistake w.r.t. sentinel values but I
bet you didn't even read it.

kind regards,

Jos
Aug 16 '07 #3
so what i need to do is make it ignore the value 0 in the multiplication formula.
How could I do that?
Aug 16 '07 #4
JosAH
11,448 Expert 8TB
so what i need to do is make it ignore the value 0 in the multiplication formula.
How could I do that?
Don't multiply by ell when ell equals zero.

kind regards,

Jos
Aug 16 '07 #5
and it is because I am using a previous code from a program that does the same thing but with addition.
Now ive realized in addition, adding value 0 would give the same output
anyway back to how to make it ignore value 0, see post above
Aug 16 '07 #6
Yeah I was just about to point out

Killer42's Avatar
Killer42
Moderator
5,108 Posts

Yesterday
10:09 PM
#41

Re: The addition problem
Quote:
Originally Posted by Hunderpanzer
...
Code: ( cpp )

1.
while (1){
2.
cout << "Enter numbers to add ( 0 to total )";
3.
cin >> n;
4.
sum = sum + n;
5.
if (n == 0){
6.
cout << sum ;
7.
break;
8.
}

Just as a general point, CyBerFirEZ. In this sort of situation it will usually make more sense to do your "if blah then break" processing before you add to your total. I'm aware that it doesn't make any practical difference in this case, since you're adding zero. It's just something I would recommend keep in mind for future loops. It's generally preferable to avoid any unnecessary processing, for reasons of efficiency among others.
Aug 16 '07 #7
so then how do i make the forumla ignore the value 0?????
Aug 16 '07 #8
JosAH
11,448 Expert 8TB
so then how do i make the forumla ignore the value 0?????
C and C++ do have an if statement and an optional else clause you know ...

kind regards,

Jos
Aug 16 '07 #9
weaknessforcats
9,208 Expert Mod 8TB
Expanbd your if statement.


If ell is 0, display the total else do the calculation.

BTW, you realize, I hope that in the Courier font, the number one and the letter ell are the same symbol making your code imposssible to read.
Aug 16 '07 #10
JosAH
11,448 Expert 8TB
Yeah I was just about to point out
Yep that was the tip; credits to Killer42 for the useful tip and you, Hunderpanzer
for retrieving it. thanks.

kind regards,

Jos

edit: after re-reading that tip text I'm not sure anymore *who* actually supplied
the tip ;-)
Aug 16 '07 #11
Jos

edit: after re-reading that tip text I'm not sure anymore *who* actually supplied
the tip ;-)

Haha?




Crazyfire the answer you need is already been said here. Just read slower, and think harder : )
Aug 16 '07 #12
JosAH
11,448 Expert 8TB
Haha?
No, no 'haha'; your post #7 doesn't clearly show who actually supplied the tip
and that's why I added my 'edit' paragraph, that's all.

kind regards,

Jos
Aug 16 '07 #13
No, no 'haha'; your post #7 doesn't clearly show who actually supplied the tip
and that's why I added my 'edit' paragraph, that's all.

kind regards,

Jos


I may be a bit confused. It said killer42 at the top ?

I'll just let it go :P
Aug 16 '07 #14
JosAH
11,448 Expert 8TB
I may be a bit confused. It said killer42 at the top ?

I'll just let it go :P
Yep, it said Killer42 at the top but it doesn't clearly tell where the (nested)
quote ends; and yep, I'll let go of it as well; thanks both of you anyway also
on behalf of the OP (I guess?)

kind regards,

Jos

ps. man this is a chaotic thread ...
Aug 16 '07 #15

Sign in to post your reply or Sign up for a free account.

Similar topics

54
by: Andy | last post by:
Hi, I don't know if this is the correct group to post this, but when I multiply a huge floating point value by a really small (non-zero) floating point value, I get 0 (zero) for the result. This...
9
by: Ralf Hildebrandt | last post by:
Hi all! First of all: I am a C-newbie. I have noticed a "strange" behavior with the standart integer multiplication. The code is: void main(void)
87
by: Vijay Kumar R Zanvar | last post by:
Hi, Why multiplication of pointers is not allowed? Till now I only know this, but not the reason why! PS: As a rule, I searched the FAQ, but could not find an answer. -- Vijay Kumar R...
17
by: Christopher Dyken | last post by:
Hi group, I'm trying to implement two routines to handle 32x32-bits and 64x64-bits signed integer multiplication on a 32 bits machine in C. It easy to find descriptions of non-signed...
20
by: TJ Doherty | last post by:
Need help understanding the following please: When I am creating a project and code my connection using Dim connectString As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data...
14
by: amitnanda | last post by:
Hi Guys, I have a matrix multiplication program in C that multiplies two matrices. When their size is 3*3 or 800*800, the program runs fine. But above that size, I get a "segmentation fault"....
7
by: VijaKhara | last post by:
Hi all, Is there any method which can implememt the matrix multiplication faster than using the formula as we often do by hand? I am writing the following code and my matrice: one is 3x40000 and...
9
by: helPlease | last post by:
hello !! Can anybody provide me an algorithm or C code for long integer multiplication.It's urgent.
7
by: vbwire | last post by:
hi.. im new to programming world.im a first year student in computer graphics. i just curious how to multiply 3 matrices in a coding. i just can do the 2 matrices multiplication.but it is not a good...
1
by: Sozos | last post by:
Hi guys. I have a problem with writing the base case for the following matrix multiplication function I have implemented. Please help. #define index(i,j,power) (((i)<<(power))+(j)) void...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.