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

Making an exponent project in C++

im stuck on what to put in my for loop please help me ive tried everything it feels like


//Computer Science
//Period 7
//Bonus

#include <iostream.h>
#include <stdlib.h>

int main()
{

float x;
float y;
int e;

cout << "What is the base?" << "\n";
cin >> x;
cout << "What is the exponent?" << "\n";
cin >> e;

if (e==o) {cout << "Answer is: 1" << "\n";}
else
{
for (int i=1; i<=e; i++)

{


cout << "Answer is:" << y << "\n";
}
}









system("PAUSE");
return 0;
}
Nov 9 '06 #1
58 6113
sicarie
4,677 Expert Mod 4TB
im stuck on what to put in my for loop please help me ive tried everything it feels like
Well, can you tell me in a sentence or two, what a power is, mathematically?
Nov 9 '06 #2
yes...2^3 would be 2*2*2 is that what youre asking?
Nov 9 '06 #3
sicarie
4,677 Expert Mod 4TB
yes...2^3 would be 2*2*2 is that what youre asking?
And you just answered your own post. Good job (I knew you could do it)!
Nov 9 '06 #4
i know what it means im just not sure how to code that. i cant make the loop multiply the base by itself
Nov 9 '06 #5
sicarie
4,677 Expert Mod 4TB
i know what it means im just not sure how to code that. i cant make the loop multiply the base by itself
Read over your initialization in the 'for' loop, and think about that.
Nov 9 '06 #6
i just learned how to use for loops like 2 weeks ago and havent done a program like this before so im not understanding what your telling me
Nov 9 '06 #7
sicarie
4,677 Expert Mod 4TB
How many times do you want to multiply the base by itself (in either terms, your program or the structure of an exponent)?
Nov 9 '06 #8
whatever e is
Nov 9 '06 #9
sicarie
4,677 Expert Mod 4TB
Bingo! You are seriously right there!

Ok, final click - what are you going to save the answer in?
Nov 9 '06 #10
y.....so i would put y=x*e??
Nov 9 '06 #11
sicarie
4,677 Expert Mod 4TB
Ok, you're right when you say 'y', but think of this in a for loop.

How many times does your 'for' loop execute?
Nov 9 '06 #12
i honestly have no idea
Nov 9 '06 #13
sicarie
4,677 Expert Mod 4TB
Ok, that's ok.

You said you jsut learned for loops, right? What is the structure of a for loop?

for (a ; b; c)
{
d
}

What goes in each part (a, b, c and d)?
Nov 9 '06 #14
for (int i=1; i<=e; i++)

{
y
}
Nov 9 '06 #15
sicarie
4,677 Expert Mod 4TB
Sorry, my fault.

What goes in a for loop logically? I'll start you out;

A) the initialization of the temporary counter variable that will be manipulated each time the loop is executed.
Nov 9 '06 #16
so am i putting an equation in my for loop? or doea the equation go outside the loop?
Nov 9 '06 #17
sicarie
4,677 Expert Mod 4TB
so am i putting an equation in my for loop? or doea the equation go outside the loop?
You are going to put an equation inside your for loop.

Do you know what functions the parts b, c, and d perform?
Nov 9 '06 #18
c increments it and d is what is actually going to be reapeted but im not sure what b does
Nov 9 '06 #19
sicarie
4,677 Expert Mod 4TB
c increments it and d is what is actually going to be reapeted but im not sure what b does
Good. One last question before we hit what b does.

How many times will your loop execute?
Nov 9 '06 #20
whatever e is
Nov 9 '06 #21
sicarie
4,677 Expert Mod 4TB
whatever e is
Right.

And you just answered what b is.

So your loop will start out at i, increment by 1 each time, however many times e is (because of the conditional <= which means 'less than or equal to').

Now, can you tell me one more time how many times you multiply a base by itself?
Nov 9 '06 #22
whatever the exponent is is how many times you multiply the base
Nov 9 '06 #23
sicarie
4,677 Expert Mod 4TB
whatever the exponent is is how many times you multiply the base
And what is e?
Nov 9 '06 #24
umm i?????
Nov 9 '06 #25
sicarie
4,677 Expert Mod 4TB
No, what does e stand for in your program?
Nov 9 '06 #26
exponent entered by user
Nov 9 '06 #27
sicarie
4,677 Expert Mod 4TB
So you have a loop

for (i = 1; i <= e; i++)

that is going to count up to however many times until it is equal to the exponent.

And you have your base, which is what you want to multiply by.

Are you missing anything else?
Nov 9 '06 #28
yes, telling the computer to multiply the bases until its equal to the exponent
Nov 9 '06 #29
sicarie
4,677 Expert Mod 4TB
yes, telling the computer to multiply the bases until its equal to the exponent
Ha, sorry - my fault again - bad question. For the general problem of a base and exponent, what is still missing?
Nov 9 '06 #30
the answer?
Nov 9 '06 #31
sicarie
4,677 Expert Mod 4TB
the answer?
Exactly!

Now, what is something in your program that we haven't used?
Nov 9 '06 #32
the base, x
Nov 9 '06 #33
sicarie
4,677 Expert Mod 4TB
the base, x
But we did - we are going to use the base, as many times as the exponent for the answer.

There's something else in your code that we haven't talked about, but you're REALLY close with 'x'.
Nov 9 '06 #34
the answer, y
Nov 9 '06 #35
sicarie
4,677 Expert Mod 4TB
the answer, y
Right.

So we are going to take the base, as many times as we have an exponent, multiply it by the answer.

Basic math, right?
Nov 9 '06 #36
sicarie
4,677 Expert Mod 4TB
Sorry, hand slipped, posted before I wanted to.

With computer algorithms, the syntax is just a little different

for as many times as the exponent, we are going to multiply the answer times the base

Now can you see the for loop, and what is supposed to be inside it?
Nov 9 '06 #37
right

x=e

y=y*x?
Nov 9 '06 #38
no, im sorry but im having the hardest time applying this
Nov 9 '06 #39
sicarie
4,677 Expert Mod 4TB
right

x=e

y=y*x?
You got it.
Nov 9 '06 #40
sicarie
4,677 Expert Mod 4TB
But you just answered it. And came up with it all yourself.

Is this better?

for as many times as the exponent,
multiply the answer times the base
Nov 9 '06 #41
really?? i ran it and it gave a crazy number and e-41
Nov 9 '06 #42
sicarie
4,677 Expert Mod 4TB
really?? i ran it and it gave a crazy number and e-41
Yeah, i tried to run it just now and my compiler is yelling about using c++ and .h in the includes. Hang on a sec
Nov 9 '06 #43
sicarie
4,677 Expert Mod 4TB
Yeah, i tried to run it just now and my compiler is yelling about using c++ and .h in the includes. Hang on a sec
It's the initializations.

In math, anything multiplied by anything is 0, so you just have to assign x and y the values of 1.0, and e 1, or else none of the math will work.
Nov 9 '06 #44
i did 3^2 and it gave the the answer 4
Nov 9 '06 #45
sicarie
4,677 Expert Mod 4TB
Can you re-post the code? I did have to make a few modifications to get yours running with my compiler...
Nov 9 '06 #46
int main()
{

float x=1.0;
float y=1.0;
int e=1;

cout << "What is the base?" << "\n";
cin >> x;
cout << "What is the exponent?" << "\n";
cin >> e;

for (int i=1; i<=e; i++)

{
x=e;
y=y*x;
}

cout << "Answer is:" << y << "\n";
Nov 9 '06 #47
sicarie
4,677 Expert Mod 4TB
Once again, my bad. But an integral part of programming (debugging!).

Can you tell me what is happening inside your for loop? Not the "this will increment..." but just give a short explanation of both lines:

this line modifies....
Nov 9 '06 #48
first part says the part to keep looping, and the second part tells it how to loop it
Nov 9 '06 #49
makes x and e equal,
multiplies it all
Nov 9 '06 #50

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

Similar topics

1
by: Bryan | last post by:
is there a way to make the Decimal class not print the exponent version of the decimal? >>> str(Decimal('1010')) '1010' >>> str(Decimal((0, (1, 0, 1), 1))) '1.01E+3' >>>
1
by: adahili | last post by:
Hi, I use cout to print numbers in scientific notation. I noticed that a number like 12.34 is sometimes (compiler-dependent) printed as 1.234e+1 or 1.234e+01 or 1.234e+001. Since I know the...
9
by: Joseph Aldred | last post by:
I am new to C and I am writing a program for a class I am taking. I need to use a exponent but I can not figure how to do it. I figure I need to use exp() or pow() but I can not figure out how to...
5
by: sankar | last post by:
Hi, I am using a Q14.18 value. There are tables used in my program which are being indexed by the exponent and mantissa parts of the corresponding floating point value. So how can I get the...
2
by: Jens Bloch Helmers | last post by:
How can I control the number of digits in the exponent when writing floats to a file? It seems that Python2.4.2(winXP) prints three digits anyway. >>> print 1.0e50 1e+050 >>> print '%e' %...
11
by: Protoman | last post by:
I'm getting a compilation error with this program, something about the instation of invArg; here's the code: #include <iostream> #include <cstdlib> using namespace std; template<class T>...
6
by: hrbigelow | last post by:
hi, i was wondering if anyone has ever seen a floating point library that uses at least 20 bits to represent the exponent, rather than the 11 bits for a double-precision standard IEEE-754 float. ...
1
by: Wayne Shu | last post by:
Hei everyone: Just see the output of the following program #include <iostream> #include <cstdlib> #include <limits> int main() { std::cout << "minimum exponent of double: " <<
3
by: =?Utf-8?B?UmF5IE1pdGNoZWxs?= | last post by:
Hello, By default, the "g" format specifier seems to use 2 digits in the exponent if it decides to use the scientific format. I.e., Double.ToString("g"). How do I control the number of...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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...

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.