473,586 Members | 2,718 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Need help with a program I am writing

11 New Member
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 3909
jessy
106 New Member
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<iostre am.h>

int integerPower(in t ,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<<"equa ls="<<integerPo wer(base,exp);
getch(); //waits for a character to return to main program
}

int integerPower(in t 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
punkybrewster
11 New Member
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<iostre am.h>

int integerPower(in t ,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<<"equa ls="<<integerPo wer(base,exp);
getch(); //waits for a character to return to main program
}

int integerPower(in t 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 New Member
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 New Member
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
punkybrewster
11 New Member
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 New Member
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
punkybrewster
11 New Member
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 New Member
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 New Member
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<iostre am.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

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

Similar topics

13
2127
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 DrPython for the Editor.... I still need to decide on a database........ I've really only used Access, and my SQL skills aren't that great. It...
13
3395
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 took me a long time to figure out what I should do. For instance, I was writing a program which tells you all the prime numbers that are less than...
19
4083
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 the code that implements managing unbound controls on forms given the superior performance of unbound controls in a client/server environment. I can...
27
2081
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 experienced in programming. I am trying to use the concept of arrays to calculate the hours of my backoffice staff, however I am getting a...
13
2453
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 month. We are assuming that the year will be valid 4 digit integer. So you don't have to think much about that(in terms of validation) except for...
12
2800
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 we enter 4, the size of four triangles are 4 rows. In addition, I am also writing a program which i started and failed in giving the right size...
1
1608
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 program, it can be a mouse and move around double click = normal one click click twice = normal double click When I run the writing pad program Any...
12
2993
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 the instructions and couldnt get the correct results. heres the code stuff... temperature=input("what is the temperature of the spam?") if...
7
2996
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 i will pay another $10 upon completion> CISY 103 - Project 01 - Fall 2007 - 02x Daily Sales report for Kahuna Burgers Overview The manager of...
5
1789
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. Assume that the bar code is to access a file that store the product descriptions, unit price and quantity of each product sold in the shop. Assume that...
0
7911
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7839
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8200
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8338
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8215
tracyyun
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6610
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5710
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3864
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2345
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.