Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,278 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,238 people online right now. Registration is fast and FREE... Join Now!




loop using (while) i got a problem with the declaration

3 Pages V  1 2 3 >  
Reply to this topicStart new topic

loop using (while) i got a problem with the declaration, need help

dreamygirl
13 Oct, 2008 - 05:32 AM
Post #1

New D.I.C Head
*

Joined: 8 Oct, 2008
Posts: 20

Hey guys

i got a problem with writing this program

its supposed to ask the user to input 2 integers
process : the program should assign the second integer as a power for the first integer (as long as the base is >0 and the power is >=0).

this is the code i came up with

CODE
#include <iostream>
using namespace std;

int main () {

    int x;
    int y;
    int m;

    cout<<" Enter the first number Base"<<endl;
    cin>>x;
    cout<<" Enter the second number Power"<<endl;
    cin>>y;

    while ( y>=0 && x>0) {
        
    m=

         cout<<" The result is  "<<endl;
    cout<<m;

    }

        
         cout<<" Math error! "<<endl;


    return 0;


}



Math error should shows up when x=0 coz 0^0 is undefined

Anyway

I've tried to define m as the sum or the multiplicaton product of the two integers and it worked .

I just have a problem with the symbol of "Power"

i wanna type m= (x*x) y times

but i dunno what is the right way to do it

any idea ? wub.gif
User is offlineProfile CardPM
+Quote Post

gabehabe
RE: Loop Using (while) I Got A Problem With The Declaration
13 Oct, 2008 - 05:49 AM
Post #2

Donkey DIC
Group Icon

Joined: 6 Feb, 2008
Posts: 5,523



Thanked: 96 times
Dream Kudos: 2650
Expert In: ruling the world.

My Contributions
Looks to me like you're really overcomplicating this.

You can #include <cmath> and use pow(double,double) to get the power~

cpp
#include <iostream>
#include <cmath>
using namespace std;

int main () {
double x, y, m;

cout<<" Enter the first number Base"<<endl;
cin>>x;
cout<<" Enter the second number Power"<<endl;
cin>>y;

m = pow(x,y);
cout << "Result: " << m;

cin.get(); // pause
return EXIT_SUCCESS;
}

Hope this helps smile.gif
User is online!Profile CardPM
+Quote Post

dreamygirl
RE: Loop Using (while) I Got A Problem With The Declaration
13 Oct, 2008 - 05:55 AM
Post #3

New D.I.C Head
*

Joined: 8 Oct, 2008
Posts: 20


That's what i've suggested when the teacher asked us to do it,
math library really helps

but he doesnt want it that way :S
instead,he says it should be written using "while" !!

so ? what can i do ? =(
User is offlineProfile CardPM
+Quote Post

AmitTheInfinity
RE: Loop Using (while) I Got A Problem With The Declaration
13 Oct, 2008 - 06:01 AM
Post #4

C Surfing ∞
Group Icon

Joined: 25 Jan, 2007
Posts: 1,025



Thanked: 35 times
Dream Kudos: 125
My Contributions
Well the motive of this assignment is to teach you how to use loops and not how to use libraries, you will anyway use them once you are comfortable with basic programming skills.

Here is the algorithm for you.

accept base from user
accept power from user
check if power is invalid and exit if it's invalid.
answer = base
while power is greater than 1
-->answer = answer * base
-->reduce power by 1
end of while
print the answer

I think you can implement this algorithm in C now.

I hope this will help you. smile.gif
User is offlineProfile CardPM
+Quote Post

dreamygirl
RE: Loop Using (while) I Got A Problem With The Declaration
13 Oct, 2008 - 06:11 AM
Post #5

New D.I.C Head
*

Joined: 8 Oct, 2008
Posts: 20

QUOTE(AmitTheInfinity @ 13 Oct, 2008 - 07:01 AM) *

Well the motive of this assignment is to teach you how to use loops and not how to use libraries, you will anyway use them once you are comfortable with basic programming skills.

Here is the algorithm for you.

accept base from user
accept power from user
check if power is invalid and exit if it's invalid.
answer = base
while power is greater than 1
-->answer = answer * base
-->reduce power by 1
end of while
print the answer

I think you can implement this algorithm in C now.

I hope this will help you. smile.gif


hmm i didn't get it
why should we reduce power by 1 ??
and how is the answer=answer*base ??

i'm really confused

User is offlineProfile CardPM
+Quote Post

dreamygirl
RE: Loop Using (while) I Got A Problem With The Declaration
13 Oct, 2008 - 06:18 AM
Post #6

New D.I.C Head
*

Joined: 8 Oct, 2008
Posts: 20

I just wanna know if there exists a symbol or a way to make the power

This post has been edited by dreamygirl: 13 Oct, 2008 - 06:21 AM
User is offlineProfile CardPM
+Quote Post

gabehabe
RE: Loop Using (while) I Got A Problem With The Declaration
13 Oct, 2008 - 06:21 AM
Post #7

Donkey DIC
Group Icon

Joined: 6 Feb, 2008
Posts: 5,523



Thanked: 96 times
Dream Kudos: 2650
Expert In: ruling the world.

My Contributions
My exponent function might help. smile.gif
User is online!Profile CardPM
+Quote Post

AmitTheInfinity
RE: Loop Using (while) I Got A Problem With The Declaration
13 Oct, 2008 - 06:24 AM
Post #8

C Surfing ∞
Group Icon

Joined: 25 Jan, 2007
Posts: 1,025



Thanked: 35 times
Dream Kudos: 125
My Contributions
Seems that I need to explain the algorithm. smile.gif

well when you say cube of 4, we actually do 4*4*4 right?

so let's see the same example but in steps.
so sum = 4
then sum = sum * 4 which is actually 4 * 4 or 4 square. [as we stored 4 in sum]
then sum = sum * 4 which is actually 4 * 4 * 4 or 4 cube. [as we now stored 4 * 4 in sum.]

so you can see that we had 2 multiplications to get cube.
This means you need n-1 multiplications to get nth power.

that's why I have the while loop going from power to 2. so here power works as your counter going from the power's max value to 2, we need to decrement it for that. power does not have any other requirement in this program. And I have already explained why I multiply answer with x.

I hope this will help you. smile.gif
User is offlineProfile CardPM
+Quote Post

dreamygirl
RE: Loop Using (while) I Got A Problem With The Declaration
13 Oct, 2008 - 06:25 AM
Post #9

New D.I.C Head
*

Joined: 8 Oct, 2008
Posts: 20

QUOTE(gabehabe @ 13 Oct, 2008 - 07:21 AM) *


It seems that it will work smile.gif
thank you so much
User is offlineProfile CardPM
+Quote Post

dreamygirl
RE: Loop Using (while) I Got A Problem With The Declaration
13 Oct, 2008 - 06:41 AM
Post #10

New D.I.C Head
*

Joined: 8 Oct, 2008
Posts: 20

QUOTE(AmitTheInfinity @ 13 Oct, 2008 - 07:24 AM) *

Seems that I need to explain the algorithm. smile.gif

well when you say cube of 4, we actually do 4*4*4 right?

so let's see the same example but in steps.
so sum = 4
then sum = sum * 4 which is actually 4 * 4 or 4 square. [as we stored 4 in sum]
then sum = sum * 4 which is actually 4 * 4 * 4 or 4 cube. [as we now stored 4 * 4 in sum.]

so you can see that we had 2 multiplications to get cube.
This means you need n-1 multiplications to get nth power.

that's why I have the while loop going from power to 2. so here power works as your counter going from the power's max value to 2, we need to decrement it for that. power does not have any other requirement in this program. And I have already explained why I multiply answer with x.

I hope this will help you. smile.gif


thanx AmitTheInfinity

i guess i got it

hmmm can u please type how the process will be

starting from while

this is the part i got :

since in my program :
x:base .. y:power .. m: is the product of exponential
this part of the code will be like that

CODE
while (y>1) {
m=m*x;



is that right ?
User is offlineProfile CardPM
+Quote Post

AmitTheInfinity
RE: Loop Using (while) I Got A Problem With The Declaration
13 Oct, 2008 - 06:45 AM
Post #11

C Surfing ∞
Group Icon

Joined: 25 Jan, 2007
Posts: 1,025



Thanked: 35 times
Dream Kudos: 125
My Contributions
Yes, you are almost there. You just forgot to decrement your power.

CODE

while (y>1) {
m=m*x;
y--;
}


and don't forget to set your m to x before while loop.

All the best. smile.gif
User is offlineProfile CardPM
+Quote Post

dreamygirl
RE: Loop Using (while) I Got A Problem With The Declaration
13 Oct, 2008 - 06:57 AM
Post #12

New D.I.C Head
*

Joined: 8 Oct, 2008
Posts: 20

QUOTE(AmitTheInfinity @ 13 Oct, 2008 - 07:45 AM) *

Yes, you are almost there. You just forgot to decrement your power.

CODE

while (y>1) {
m=m*x;
y--;
}


and don't forget to set your m to x before while loop.

All the best. smile.gif



QUOTE(dreamygirl @ 13 Oct, 2008 - 07:52 AM) *

QUOTE(AmitTheInfinity @ 13 Oct, 2008 - 07:45 AM) *

Yes, you are almost there. You just forgot to decrement your power.

CODE

while (y>1) {
m=m*x;
y--;
}


and don't forget to set your m to x before while loop.

All the best. smile.gif



I'm so sorry i feel like i disturbed u alot

hmmm how can i set my m to x

u mean like that
m=x

??

User is offlineProfile CardPM
+Quote Post

3 Pages V  1 2 3 >
Reply to this topicStart new topic
Time is now: 12/2/08 05:18AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month