good day to you....
I've been searching this code which computes the product of two numbers using addition...this problem is so confusing...what code did you used in this program?
I hope it will reply very soon...thanks
Printable View
good day to you....
I've been searching this code which computes the product of two numbers using addition...this problem is so confusing...what code did you used in this program?
I hope it will reply very soon...thanks
Think about it for a minute.. How do you multiply numbers together on paper?
What's 4 multiplied by 5...?
well, its 4 + 4 + 4 + 4 + 4
.. the number 4 added together 5 times
or, 5 + 5 + 5 + 5
the number 5 added together 4 times
If you want to perform an operation a certain number of times, then use a loop... Read here for more information
http://www.cprogramming.com/tutorial/lesson3.html
what type of loop do you used in this program?
for
while
do
Think about it for 5 minutes - OK ?
in this case, what code (in a for loop pattern) satisfies the problem?
What are your ideas on that question?
it's about the program which input two numbers and get the product of it using adition
I mean, what are your ideas on what code (in a for loop pattern) satisfies the problem?
ya...
for example:
Think about it for a minute.. How do you multiply numbers together on paper?
What's 4 multiplied by 5...?
well, its 4 + 4 + 4 + 4 + 4
.. the number 4 added together 5 times
or, 5 + 5 + 5 + 5
the number 5 added together 4 times
Is that your answer to my question? I want to know what you think, not a copy and paste of what somebody else said.
The point is that you make some effort to solve the problem, and we help you figure out where your thinking is wrong or can be improved. If you have a question for us, then I'm asking you to give your thoughts on that question first, so they can be responded to.
what code satifisfies this problem?
What Salem and Daved are getting at is that no one here is going to spoon feed you this code. Please try to work this out on your own, then post the code and what problems you are having. If you do not understand the for loop, look at the tutorial again, read it carefully, and post what questions you have concerning the for loop structure. Once you understand the for loop structure, try to apply it to what Bench82 said. Post your efforts to the board and we'll help you solve specific problems with what you're doing.
[edit] beaten, took too long making it polite...[/edit]
my code is found in my flash disk... do you want to show it?
Yes, we'd like to see what you have accomplished so far.
insertis there somethinG wrong in my code? IF YES, what part of code/program that does not satisfies this problem?Code://program that input two numbers and multiply it using addition
#include <iostream.h>
#include <conio.h>
void main();
{
clrscr();
int, x,y,prod;
cout<<"Input first number:";
cin>>x;
cout<<"Input second number:";
cin>>y:
for(x=0; x>=y; x+x)
cout<<"The product is"<<prod;
getch();
}
Ok, that's a good start. Especially the part where you read in the first two numbers.
Explain, in english how you want your for loop to work. A for loop has a start value, end value, and increment, and then it has a block of code that executes each time until the end value is reached. Explain what variable you want to use to loop through, the start value, the end value, and how it much it will increment each time. Then explain what will be done inside the block of code. Finally explain how that will solve the problem.
Your current for loop does not solve the problem, but if you explain in English what you are trying to do, we can find out if you are just having problems coding the solution, or if you are still trying to come up with the solution in the first place.
> is there somethinG wrong in my code? IF YES, what part of code/program that does not satisfies this problem?
Well it doesn't compile - did you try to compile it?
> void main();
a) main returns an int, see the FAQ
b) the ; at the end is really bad news for the rest of the code
When it compiles, run it and see what answers you get.
for(x=0; x>=y; x+x)
you ignore value entered by user replacing it by 0;
x+x does not modify the x value, so your loop or run never or forever based on the condition 0>=y
cout<<"The product is"<<prod;
prod is not initialized, you're going to print some garbage value
Declare and initialise all your variables first ... 0 is always a good number.Code:for ( int i=0; i<5; i++ )
{
std::cout<< i << '\n';
}
Read in the two numbers.
Change 5 in the above loop (used as an example) to one of the numbers the user inputs.
Inside the {}'s += the other inputted value to the product.
Don't need to cout<< inside the loop.
After it's finished print the value.
it would not run properly!!!! the compilation result is an error!!!!!!!
is there something wrong with this code?Code:#include <iostream.h>
#include <conio.h>
void main()
{
clrscr();
int y,x,prod;
cout<<" INPUT 1ST NUMBER:";
cin>>x;
cout<<"INPUT SECOND NUMBER:";
cin>>y;
for( x=0; x<5; x++)
{
cout<<x<<'\n';
}
cout<<"THE PRODUCT IS "<<prod;
getch();
}
yes it is. read the thread from the beginning
depends on the compiler you use.
my compiler says
KurtCode:error: `main' must return `int'
Blacksnake, did you read the pointers I left in my last thread? Why does the for loop loop 5 times? Why do you cout<< instead of adding inside the loop's scope? Where is the product calculated?
Psudo code:
Change that to real C++ and it should work.Code:var Num1 = 0
var Num2 = 0
var Product = 0
get: Num1
get: Num2
loop ( 0:Num1)
{
Product += Num2;
}
print: "Product is: " Product
EDIT:
Also, "#include <iostream.h>" ... use: #include <iostream> instead. It won't fix the problems in your code. It's just iostream.h isn't really used much. You may have to put a 'using namespace std;' before main (or you could put std:: on front of cout<<and cin>>):
orCode:#include <iostream>
using namespace std;
int main( void )
{
cout<< "Hello World!";
return 0;
}
Code:#include <iostream>
int main( void )
{
std::cout<< "Hello World!";
return 0;
}
i'm using borlanD c++ compiler...
Are you compiling code?
Are you getting errors?
Are you trying to fix them?
Are you testing and trying things?
Have you tried to convert the pseudo code above to real C++ code?
We are not here to give you code or do your homework for you! We're here to help you along. I see very little evidence of your work here!
Are you compiling code? yes
Are you getting errors? yes...many times
Are you trying to fix them? yup...
Are you testing and trying things? yup...
Have you tried to convert the pseudo code above to real C++ code? no... i'm confused about that
Pseudo code is code which is readable. You read how it should work.
so, for example, my loop(0:Num1) could be the same as for ( int i=0; i<Num1; i++ ). Print: "whatever", would be like cout<< "whatever", get: Num1, cin>> Num1; and in this case var is int
Post your efforts and I'll be more than happy to help. use code tags though.
the output should be:Code:
// This program based on the old version of BORLAND C++
#include <iostream.h>
#include <conio.h>
void main()
{
int x;
int y;
int product;
cout<<"input first number:";
cin>>x;
cout<<"input second number:";
cin>>y;
for(int i=0; i<x; i++)
{
product +=y;
}
cout<<"Product is:"<<product;
getch();
}
input first number: 5
input second number: 3
Product is: 1549 (product should be 15)
this program show at the last thread of the second page may encountered runtime error...as you input two numbers, the answer is not correct...is there something wrong with the program or the code?
example:
num1=3
num2=5
product=1549
it should be:
num1=3
num2=5
product=15
Changes in red
Code:
// This program based on the old version of BORLAND C++
#include <iostream.h>
#include <conio.h>
int main()
{
int x;
int y;
int product = 0;
cout<<"input first number:";
cin>>x;
cout<<"input second number:";
cin>>y;
for(int i=0; i<x; i++)
{
product +=y;
}
cout<<"Product is:"<<product;
getch();
return 0;
}
therefore, this program satisfies the problem...to all users behind it who help to solve...thank you....