![]() |
|
|||||||
|
|
Thread Tools | Search this Thread | Rate Thread |
|
#1
|
|||
|
|||
Beginner program to put product (A times B) into variable C.I am just starting to learn how to program in C++, so this question should be an easy one for you experts out there haha. This practice question is bugging me because we haven't learned how to do much in class yet, but I want to have it for tomorrow's class because we get a bonus mark if it's done! I'll give you what I have so far, and the question.
Question (a) Using only the available operations (shown below), write the C++ code that takes two variables A and B, and puts their product (A times B) into variable C. Your solution must work for the cases... -where A is zero -where B is zero -where A and B are both positive C++ statements allowed... Declarations allowed: int X,Y,Z; //you may declare as many int variables as you need I/O Operations allowed: cin >> X; cout << Y << endl; Assignment Statements allowed: X = 0; // 0 is the only constant that may be assigned X = Y; // copy a variable Arithmetic Operations allowed: X++; X--; Conditional Statements allowed: If (<predicate>) { } while (<predicate>) { } Predicates allowed: X < 0 X > 0 X == 0 X !=0 // You are not allowed to use AND(&&), OR(||), or NOT(!) This is what I have so far. CPP / C++ / C Code:
I know that isn't much, but as I already said, I have no clue how to do this problem haha. I know that I need usingnamespace std; so that I can use cout and cin. The program that I've written multiplies everything fine, except that it uses a "multiplication operator", how do I do the same thing while sticking to the allowed operators? I know that the program should be more complicated than this. (b) Suppose you were also not allowed the copy statement X=Y. Could you still make a multiplication routine? Explain. A million thanks for all the help. Dan Last edited by LuciWiz : 08-Nov-2007 at 12:46.
Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
|
|
#2
|
|||
|
|||
Re: Beginner program to put product (A times B) into variable C.The trick is to not use the multiply operator. You will have to do it in a loop. It looks like you're allowed to use a while() loop so that should do the trick. Here's how I would do 3 times 5.
1) have an int variable, let's call it result, and initialize it to zero. 2) For 3 times, I would repeat step 3. 3) For 5 times, I would increment result. Now, result should have your product. I hope this helps. For a hint, use while() loops. |
|
#3
|
|||
|
|||
Re: Beginner program to put product (A times B) into variable C.I'm not too sure how to use a while loop though, it looks complicated in my textbook.
|
|
#4
|
|||
|
|||
Re: Beginner program to put product (A times B) into variable C.I just need to know what the predicate inside the while loop would be...
while (???) |
|
#5
|
||||
|
||||
Re: Beginner program to put product (A times B) into variable C.Well, what is it you are trying to do? What is the loop supposed to accomplish -- and how? Figure out how to describe that, but not in code. Write the code from that description. Post your description if you need help...
__________________
Age is unimportant -- except in cheese |
|
#6
|
|||
|
|||
Re: Beginner program to put product (A times B) into variable C.Re: Well, what is it you are trying to do? What is the loop supposed to accomplish -- and how? Figure out how to describe that, but not in code. Write the code from that description. Post your description if you need help...
I'm trying to multiply 'X' by 'Y', without using a multiplication sign. That means I have to add 'X' to itself 'Y' times. For example: to multiply 4*5, I have to add 5 groups of 4 together. Let X = 4, and Y = 5, and Z = answer. So X+X+X+X+X=Z. I'm not sure if this is the correct method though, because I still get caught on what to put in between the brackets in the while loop, and then how to write that equation within the while loop. |
|
#7
|
|||
|
|||
Re: Beginner program to put product (A times B) into variable C.This is the new code that I have so far...
CPP / C++ / C Code:
Last edited by LuciWiz : 08-Nov-2007 at 12:49.
Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
|
|
#8
|
|||
|
|||
Re: Beginner program to put product (A times B) into variable C.If you do that in your loop, it will never end because X never changes. Try doing it like I said before:
CPP / C++ / C Code:
|
Recent GIDBlog
Meeting the local Iraqis by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Two-Tier data dissemination code installation problem | nidhibansal1984 | Computer Software Forum - Linux | 6 | 16-Sep-2007 10:13 |
| Text-Based Roulette Game | mfm1983 | C++ Forum | 5 | 29-Nov-2006 12:20 |
| Pointer Usage in C++: Beginner to Advanced | varunhome | C++ Forum | 0 | 19-Aug-2005 09:25 |
| [Tutorial] Pointers in C (Part I) | Stack Overflow | C Programming Language | 1 | 08-Apr-2005 18:35 |
| [Review] SalesCart Pro | BobbyDouglas | Web Design Forum | 0 | 11-Mar-2004 12:09 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The