GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 07-Nov-2007, 15:36
ddaigle ddaigle is offline
New Member
 
Join Date: Nov 2007
Posts: 11
ddaigle is on a distinguished road

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:
#include <iostream.h>
#include <stdlib.h>
using namespace std;

int main()
{
    int X,Y,Z;
    cout << "Enter a number" <<endl;
    cin >> X;
    cout << "Enter another number" <<endl;
    cin >> Y;
    Z = X*Y;
    cout <<  X << " times " <<  Y << " equals " <<  Z <<endl;
    system("PAUSE")
    return 0;
}

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  
Old 07-Nov-2007, 15:51
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 482
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

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  
Old 07-Nov-2007, 16:18
ddaigle ddaigle is offline
New Member
 
Join Date: Nov 2007
Posts: 11
ddaigle is on a distinguished road

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  
Old 07-Nov-2007, 18:34
ddaigle ddaigle is offline
New Member
 
Join Date: Nov 2007
Posts: 11
ddaigle is on a distinguished road

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  
Old 07-Nov-2007, 19:29
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,243
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

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  
Old 07-Nov-2007, 23:10
ddaigle ddaigle is offline
New Member
 
Join Date: Nov 2007
Posts: 11
ddaigle is on a distinguished road

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  
Old 07-Nov-2007, 23:16
ddaigle ddaigle is offline
New Member
 
Join Date: Nov 2007
Posts: 11
ddaigle is on a distinguished road

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:
#include <iostream.h>
#include <stdlib.h>

int main ()
{
    int X,Y,Z,i;
    i = 0;
    cout << "Enter first number" << endl;
    cin >> X;
    cout << "Enter second number" <<endl;
    cin >> Y;
    while (X>0) 
    {
        Z= i+X;
        i=Z;
    }
    cout <<"All Finished!" << endl;
    system("PAUSE");
    return 0;
}
Last edited by LuciWiz : 08-Nov-2007 at 12:49. Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
  #8  
Old 08-Nov-2007, 07:35
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 482
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

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:
#include <iostream.h>
#include <stdlib.h>

int main ()
{ int X,Y,i,j,result;
  
  // Get user input
  cout << "Enter first number" << endl;
  cin >> X;
  cout << "Enter second number" <<endl;
  cin >> Y;

  // Initialize variables for the outer loop
  result = 0;
  i = 0; // we will loop with i for our outer loop
  while (i < X) 
  { // TODO: Initialize variables for the inner loop
    // TODO: Create the inner loop similar to the outer loop
    // HINT: in the inner loop, you'll want to increment result
   
    i++;
  }
  cout <<"The product is " << result << endl;
  system("PAUSE");
  return 0;
}
See if you can finish the code. Good Luck.
 
 

Recent GIDBlogMeeting the local Iraqis by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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

All times are GMT -6. The time now is 16:55.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.