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? -
#include <iostream>
-
-
using namespace std;
-
-
int main()
-
{
-
int left, right;
-
-
cout << "Enter left and right side: ";
-
cin >> left >> right;
-
-
-
-
for (right;;)
-
-
left *= right;
-
-
-
-
-
-
-
system("pause");
-
return 0;
-
}
12 12942
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.
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;
}
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.
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.
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
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.
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)
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.
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.
so i got this code, and it works like a charm!
int result = 1;
for (; exp--; )
result *= base;
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: -
int result= 1;
-
for (;exp--; result*= base);
-
or: -
int result= 1;
-
while(exp--)
-
result*= base;
-
They all do the same thing.
kind regards,
Jos
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: -
-
// this is pseudocode, BTW, this won't compile
-
declare base, exponent, result
-
-
for i = 0; i < exponent; i++
-
result *= base
-
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...
Sign in to post your reply or Sign up for a free account.
Similar topics
by: PHP2 |
last post by:
can someone help me instal CGI C++ library on Linux
http://www.vbmcgi.org/
|
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...
|
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...
|
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...
|
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...
|
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...
|
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)...
|
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...
|
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):
...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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,...
|
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: 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...
|
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...
| | |