473,396 Members | 1,834 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,396 software developers and data experts.

Help with exponents loop, not allowed to use pow() library

6
so i'm trying to set up this exponents loop, keep in mind this is my first year in computer science so my knowledge of script is somewhat minimal. basically this assignment (or at least part of it) tells us to have the user enter 2 numbers, (left and right) and to output the correct answer using a loop to calculate. my idea was to use to write a script so the number the user inputs for the right side will determine the number of times the loop runs. this is what i have so far but it's not working. can anyone help please?
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7.     int left, right;
  8.  
  9.     cout << "Enter left and right side: ";
  10.     cin >> left >> right;
  11.  
  12.  
  13.  
  14.     for (right;;)
  15.  
  16.     left *= right;
  17.  
  18.  
  19.  
  20.  
  21.  
  22.  
  23.     system("pause");   
  24.     return 0;
  25. }
Oct 21 '07 #1
12 12942
sicarie
4,677 Expert Mod 4TB
Well, your for loop is missing just about everything.

What is the logic you are trying to implement? How would you accomplish this task if you were given a piece of paper and the numbers? Each of those steps is your algorithm, which you need to figure out before you start coding. I would recommend reading in the number and the exponent, and then using the exponent as the test limit in your for loop.
Oct 21 '07 #2
zalery
6
i'm not sure if i understand you completely, as i've been only doing this for a few months, but i'll try express myself the best i can.

basically what i'm trying to do is have the user enter in two numbers, (left and right) and then calculating it like this:
left ^ right
but i can't use pow()
i'm not too sure what you meant by "reading in" though
i'm still at a loss for this assignment really, no idea what to do
do you think im on the right track?

#include <iostream>

using namespace std;

int main()
{
int left, right, count, answer;

cout << "Enter left and right side: ";
cin >> left >> right;



do
{
left *= right;
} while (count = right);







system("pause");
return 0;
}
Oct 21 '07 #3
sicarie
4,677 Expert Mod 4TB
Okay. Close your compiler/IDE. Leave it closed.

Take a pen (or pencil) and a sheet of paper. Now, without using code, write down the steps that anyone who did not know how to calculate an exponent would be able to pick up, read, and follow. For example, you would start with the basics:

get base
get exponent

from there, where would you go? What do you do to calculate a power? What steps do you take (when you're not allowed to use a calculator)? You need to figure this out, and look at how the real-world behaves before you can conceptualize it enough to abstract it to a computer.
Oct 21 '07 #4
oler1s
671 Expert 512MB
Have you thought about writing down on paper the logic? Look at what you are saying right now. Left is multiplied by right. Actually, you know what, learn to use reasonable mathematical terms. Call left "base" and "right" power instead.

Your loop involves multiplying base and power. Here's an example for you 3^2. What's that? 3 * 3. 2^4 = 2*2*2*2. You see the base and power directly multiplying. Try writing your logic down in plain english. Once you think you have it right, show your logic, in plain english to us.
Oct 21 '07 #5
zalery
6
so if this was done on a paper, i would input the numbers

x ^ y

let's say x=5, and y=6

so 5^6

5*5*5*5*5*5=15625

very basic, i do completely understand that concept (as though it may not seem that way)

so what i'm trying to do with my code, in plain english:

1. ask user to input a number for the base, and a number for the exponent
2. calculate that by using a loop (and with no use of pow() )
3. output the answer
Oct 21 '07 #6
sicarie
4,677 Expert Mod 4TB
so what i'm trying to do with my code, in plain english:
...
2. calculate that by using a loop with no use of pow()
....
Not so much in plain English because I don't (for this thread) know how to calculate the power. How do you do that? I'll give you a hint, you do the same thing a certain number of times. What do you do, and how many times do you do it?

FYI - You are still concentrating on the code instead of the issue - you're still talking about the pow() function. You're not supposed to use it, so forget about it. Concentrate on the numbers, and what they are doing. Look for patterns.
Oct 21 '07 #7
zalery
6
Not so much in plain English because I don't (for this thread) know how to calculate the power. How do you do that? I'll give you a hint, you do the same thing a certain number of times. What do you do, and how many times do you do it?
i suppose you would take the base value, multiply that by the base value, and then multiply the same number of times there is an exponent

so for 5^6

(5*5)

and multiply that by 5, 5 more times over (since the first time counts as once)
Oct 21 '07 #8
sicarie
4,677 Expert Mod 4TB
for 5^6

(5*5)

and multiply that by 5, 5 more times over
That looks very close to a control structure used in programming, doesn't it? ;) From there it's just the technical details on where the base and exponent go for the checks each time.
Oct 21 '07 #9
zalery
6
That looks very close to a control structure used in programming, doesn't it? ;) From there it's just the technical details on where the base and exponent go for the checks each time.
yes, it certainly does. the only problem is that i'm not too sure how to repeat a loop for a set number of times. i'll try figure it out by going through my textbook though.
Oct 21 '07 #10
zalery
6
so i got this code, and it works like a charm!

int result = 1;

for (; exp--; )
result *= base;
Oct 21 '07 #11
JosAH
11,448 Expert 8TB
so i got this code, and it works like a charm!

int result = 1;

for (; exp--; )
result *= base;
Well done; if you want funny loops like the one above you can also do:

Expand|Select|Wrap|Line Numbers
  1. int result= 1;
  2. for (;exp--; result*= base);
  3.  
or:

Expand|Select|Wrap|Line Numbers
  1. int result= 1;
  2. while(exp--)
  3.    result*= base;
  4.  
They all do the same thing.

kind regards,

Jos
Oct 21 '07 #12
sicarie
4,677 Expert Mod 4TB
so i got this code, and it works like a charm!

int result = 1;

for (; exp--; )
result *= base;
I was thinking more along the lines of:

Expand|Select|Wrap|Line Numbers
  1.  
  2. // this is pseudocode, BTW, this won't compile
  3. declare base, exponent, result
  4.  
  5. for i = 0; i < exponent; i++
  6.     result *= base
  7.  
Of course, that would involve figuring out how to initialize result, and how that effects the loop, but you get the idea. Or you can do like Jos said and use the more odd-looking loops...
Oct 22 '07 #13

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

Similar topics

0
by: PHP2 | last post by:
can someone help me instal CGI C++ library on Linux http://www.vbmcgi.org/
13
by: na1paj | last post by:
here's a simple linked list program. the DeleteNode function is producing an infinit loop i think, but i can't figure out where.. #include <stdio.h> typedef struct { char *str; //str is a...
6
by: Ed Jay | last post by:
<disclaimer>New to js.</disclaimer> I have several pages, each with menues comprising checkboxes or radio boxes within the same form. I presently 'brute force' clear the buttons with individual...
3
by: lawrencec | last post by:
Hi there, I'm trying to add the values of a number of form fields and to get a result at the end. It must loop and be able to dynamically update the result of calculation. I have attached the...
4
by: Scoop | last post by:
I am new to php and not much of a developer to begin with. I am trying to write what seems like it should be a simple piece of code but I can't get it to work. I'm trying to check if a file...
1
by: winston.heng | last post by:
Hi, Thanks for reading this posting. I have been cracking my head on solving the infinite loop when i call the following section code. Any help or advise is greatly appreciated =D Thanks in...
1
by: R69D | last post by:
Write and test the following method that implements the power function: static double pow(double x, int n) This method returns the value of x raised to the power n. For example pow(2.0, -3)...
1
by: erykewell | last post by:
Hi there people. I'm a newbie in using Visual Basic and need help with my college project I'm working on. I'm developing a program based on a MATLAB program. The situations are: 1. How do I...
2
Thekid
by: Thekid | last post by:
I had made a post about making a loop using letters instead of numbers and dshimer gave me this solution: for i in range(65,70): for j in range(65,70): for k in range(65,70): ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.