473,396 Members | 1,975 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.

Need help with a program I am writing

I need to write a program that will compute a number (base) raised to a power which can be an integer, negative or zero with out including the math library. This is what I wrote thus far. Please help me.

#include <iostream>
using namespace std;

int main()
{
double x, y;

cout << "Enter a base number: ";
cin >> x;
cout << "Enter a integer exponent: "
cin >> y;

double i = 1;
double product = x;

while ( y > i)
{
product *= x;
i++;
}

return 0;
}

My problem is how do I set up the while statement to compute lets say 4 raised to the -2? Do I include an if statement in the body? Your help is greatly appreciated.
Oct 28 '06 #1
11 3882
jessy
106 100+
look hunny this program is so easy if you used functions to do the exponential part and then call that function from the main

here's the code i'v posted it today 4 someone asking your same question :

#include <stdio.h>
#include<conio.h>
#include<iostream.h>

int integerPower(int ,int ); //this is a prototype
void main() //void main or int main won't matter but i prefer void main
{ clrscr(); //just to clear the screen 4 you each time you run your program
int exp;
int base;

printf("Enter integer base and exponent:");
cin>>base>>exp;
cout<<base<<"to the power of"<<exp<<"equals="<<integerPower(base,exp);
getch(); //waits for a character to return to main program
}

int integerPower(int b,int e)
{
int product=1;

for(int i=1; i<=e; i++)
product*=b;
return product;
}
if you need any help with this code just post me ......
Oct 28 '06 #2
This is my first lesson in c++ and I don't have a professor to teach me throughly so I am basically winging it. I must write the program only with the header file <iostream> nothing else. Also I should only use the while loop and take into consideration positive, negative values for the exponent.


I must say thanking you for responding and for your help. Do you have any other suggestion as to how to do this exercise with only the required header file?







look hunny this program is so easy if you used functions to do the exponential part and then call that function from the main

here's the code i'v posted it today 4 someone asking your same question :

#include <stdio.h>
#include<conio.h>
#include<iostream.h>

int integerPower(int ,int ); //this is a prototype
void main() //void main or int main won't matter but i prefer void main
{ clrscr(); //just to clear the screen 4 you each time you run your program
int exp;
int base;

printf("Enter integer base and exponent:");
cin>>base>>exp;
cout<<base<<"to the power of"<<exp<<"equals="<<integerPower(base,exp);
getch(); //waits for a character to return to main program
}

int integerPower(int b,int e)
{
int product=1;

for(int i=1; i<=e; i++)
product*=b;
return product;
}
if you need any help with this code just post me ......
Oct 29 '06 #3
jessy
106 100+
hunny you don't need the other header files ...

#include<conio.h> i used it in order to write clrscr(); which as i told you clears the screen for you each time you see the output but you may not use it at all but each time you run your program you will see the same sentences that were written before so if you run this program many times you will have a very DIRTY screen ( the black screen for output)....

also #include<stdio.h> is not necessary i just used it coz i thought i may use printf and scanf but don't worry you can remove it at alllll

and about the for loop you can use any other loop just like the while but right now i can't provide you with the while loop coz i have to sleep now ...but promise to post you tommorow

by the way where r u from ??
Oct 29 '06 #4
jessy
106 100+
Good MOrning !!

that's the while loop you want

while(y>=i)

{
product*=x;
i++ ;

}
your code was right except that part in while( y>i ) it should be (y>=i)

and also you shouldn't say double product=x; so plz remove that part
Oct 29 '06 #5
Thank you so much for your help and time. I got the program to run but when I enter a negative number for the exponent it didn't give me the correct value. My task is to ensure that whatever value is entered for the exponent can be zero, positive or negative.




Good MOrning !!

that's the while loop you want

while(y>=i)

{
product*=x;
i++ ;

}
your code was right except that part in while( y>i ) it should be (y>=i)

and also you shouldn't say double product=x; so plz remove that part
Oct 29 '06 #6
jessy
106 100+
Do you know why it gives you uncorrect answer when you enter a negative number ?????

i just want to make sure you understand what you r doing ...
Oct 29 '06 #7
To be quite honest with you I can't see what I am doing wrong. Based on my understanding of the program if I enter base as 1.75 and the exponent as -2 the program should be able to compute the result by putting the answer as 1/base. I thought in the body of the while loop I would have to use an if statement but that didn't work either. Currently I don't have a professor who devotes time to teach the course work since our class is only for 50 minutes and most of the assignments are given for us to learn little details on our own.



Do you know why it gives you uncorrect answer when you enter a negative number ?????

i just want to make sure you understand what you r doing ...
Oct 29 '06 #8
jessy
106 100+
that's pretty good .... im telling you that you r doing just fine ....
your program is so complicated now since you r not using functions but later on when you learn functions and how to call them from the main i want you to do the same program and see how hard it was before

anyway , your program is based on positive numbers only since your while loop only checks positive numbers
so when you enter a negative exponent this while loop will not be executed

while(y>=i ) if y= -3 for example this means that y< i so the condition of the while loop will not hold ......
its good of you to think of putting a IF statment inside the loop body but this IF won't be executed also coz he won't enter the loop .......

i hope you got that part ??!

so what if you said after : cout <<"enter an exponent"; cin>>y;
if (y<0)
{ y= -y ; }
now when he enters -3 or -4 the IF statement will deal with it as if it was 4 0r 3
that's what y= -y ; do .... i hope you got that ??
after that you can do your loop as before since now any negative exponent will be like positive one but you will face another small problem ?
can you tell what's it ??
the problem is now if the user entered 2 to the power of -3 the answer will be 8 the same as if he said 2 to the power of 3 ...but we want the result in case of -3 to be (1/8) not 8 ...so just say product=(1/product) ...
Oct 29 '06 #9
jessy
106 100+
i can send you the code after these changes but it is a bit longer that before since now you have two while loops

you first check if (y<0) then do the first while and make product=(1/product)

ELSE
the second while which is he one you done before !!

anyway here's the code and if you didn't got it just mark on what you don't understand and post me

#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{ clrscr();
double x, y;
double product=1 ;

cout << "Enter a base number: ";
cin >> x;
cout << "Enter an integer exponent: ";
cin >> y;

int i = 1;

if (y<0)
{ y=-y;

while (y>=i)
{
product *= x;
i++;
}// end while

cout<<x<<"to the power of "<<y<<"equals="<<(1/product);
}//end if
else {

while ( y >= i)
{
product *= x;
i++;
}
cout<<x<<"to the power of "<<y<<"equals="<<product;
}//end else
getch();

}
and again don't care about CONIO.H " lol "
Oct 29 '06 #10
LOL.

Well I tried running your program but it is not taking the value of the exponent to be negative since the if statement you wrote said:

if (y<0)
y = -y;

It is turning the exponent into a positive integer. I don't know if you are testing it first before you send me your explanation but I must say it is bring me closer to my result.

Whatever number is inputed as base raised to a negative number should give the right result. I am also testing the input on a calculator so we have some garbage value somewhere. Thanks again.










i can send you the code after these changes but it is a bit longer that before since now you have two while loops

you first check if (y<0) then do the first while and make product=(1/product)

ELSE
the second while which is he one you done before !!

anyway here's the code and if you didn't got it just mark on what you don't understand and post me

#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{ clrscr();
double x, y;
double product=1 ;

cout << "Enter a base number: ";
cin >> x;
cout << "Enter an integer exponent: ";
cin >> y;

int i = 1;

if (y<0)
{ y=-y;

while (y>=i)
{
product *= x;
i++;
}// end while

cout<<x<<"to the power of "<<y<<"equals="<<(1/product);
}//end if
else {

while ( y >= i)
{
product *= x;
i++;
}
cout<<x<<"to the power of "<<y<<"equals="<<product;
}//end else
getch();

}
and again don't care about CONIO.H " lol "
Oct 29 '06 #11
jessy
106 100+
YEs i tested it well and it's very very right and working ...plz try running it again

i know it's not taking the exponent to be negative but that doesn't matter as long as you display ( 1/product) and not the product itself


but in the second while you just say cout<<product ....

i hope u got me !!! try it and post again
Oct 29 '06 #12

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

Similar topics

13
by: Heather Stovold | last post by:
Wow - deciding to program in python sure requires a lot of decisions! So far: I've decided on python for the programming language. I've decided on wxpython for the GUI.... I've decided on...
13
by: takashi | last post by:
Hi, I have a question. I am learning about how to use c++ language. I have attempted to make my own programs, using the knowledge that I have, but sometimes when I get stuck on writing a code, it...
19
by: James Fortune | last post by:
I have a lot of respect for David Fenton and Allen Browne, but I don't understand why people who know how to write code to completely replace a front end do not write something that will automate...
27
by: SK | last post by:
Hi I am trying to teach myself how to program in C. I am a physician hoping to be able to help restructure my office. Anyhow, I amhoping that the porblem I am having is simple to those much more...
13
by: vgame64 | last post by:
Hi, I have been struggling with writing a program for a few hours. The requirements are that: """You will be writing a program which will determine whether a date is valid in terms of days in that...
12
by: asif929 | last post by:
I am trying to write a program which creates four triangles. The program begins with prompting a user " Enter the size of triangles", number from 1 to N is the size of four triangles For Example if...
1
by: erickwan88 | last post by:
I want to capture the input from a writing pad and apply in vb2005 but I don't know any programming codes about it. The writing pad is connected by a USB port. Before I run the writing pad...
12
by: adamurbas | last post by:
ya so im pretty much a newb to this whole python thing... its pretty cool but i just started today and im already having trouble. i started to use a tutorial that i found somewhere and i followed...
7
by: rohitsripathi | last post by:
i need help writing this program... i am in college and i do not have time do this right now as i need to study for another test....but this is due tonight...i will pay $5 if you accept to do it and...
5
by: alck1234 | last post by:
Hi, I need help on my mini project on object orientated programming. The question goes like this: A mini-mart has just installed a bar code reader to improve efficiency at their checkouts....
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.