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;
}
58 6113
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?
yes...2^3 would be 2*2*2 is that what youre asking?
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)!
i know what it means im just not sure how to code that. i cant make the loop multiply the base by itself
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.
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
How many times do you want to multiply the base by itself (in either terms, your program or the structure of an exponent)?
Bingo! You are seriously right there!
Ok, final click - what are you going to save the answer in?
y.....so i would put y=x*e??
Ok, you're right when you say 'y', but think of this in a for loop.
How many times does your 'for' loop execute?
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)?
for (int i=1; i<=e; i++)
{
y
}
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.
so am i putting an equation in my for loop? or doea the equation go outside the loop?
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?
c increments it and d is what is actually going to be reapeted but im not sure what b does
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?
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?
whatever the exponent is is how many times you multiply the base
whatever the exponent is is how many times you multiply the base
And what is e?
No, what does e stand for in your program?
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?
yes, telling the computer to multiply the bases until its equal to the exponent
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?
the answer?
Exactly!
Now, what is something in your program that we haven't used?
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'.
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?
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?
no, im sorry but im having the hardest time applying this
right
x=e
y=y*x?
You got it.
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
really?? i ran it and it gave a crazy number and e-41
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
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.
i did 3^2 and it gave the the answer 4
Can you re-post the code? I did have to make a few modifications to get yours running with my compiler...
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";
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....
first part says the part to keep looping, and the second part tells it how to loop it
makes x and e equal,
multiplies it all
Sign in to post your reply or Sign up for a free account.
Similar topics
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'
>>>
|
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...
|
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...
|
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...
|
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' %...
|
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>...
|
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. ...
|
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: " <<
|
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...
|
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,...
|
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$) {
}
...
|
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...
|
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
|
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: 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...
| |