473,399 Members | 4,192 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,399 software developers and data experts.

C++ Program to Calculate X^Y

8
Question:
Write a Program using Functions that calculates X^Y where both X and Y are doubles and X is positive. (Hints: Call the function pow() in <math.h>)
Sep 25 '07 #1
18 12545
sicarie
4,677 Expert Mod 4TB
So what have you tried? Where are you stuck?
Sep 25 '07 #2
JosAH
11,448 Expert 8TB
Question:
Write a Program using Functions that calculates X^Y where both X and Y are doubles and X is positive. (Hints: Call the function pow() in <math.h>)
I don't consider that a hint; I consider that a complete spoiler. If you don't recognize
a hint like that you wouldn't recognize a hint even if it were dancing on a piano
wearing a purple tutu singing "I am a hint; I am a hint".

kind regards,

Jos
Sep 25 '07 #3
way87
8
i jus started to do programming since last month and i still a newbie =(
i cant solve the question ,it's hard for me =(
Sep 25 '07 #4
sicarie
4,677 Expert Mod 4TB
The experts on this site are more than happy to help you with your problems but they cannot do your assignment/program for you. Attempt the assignment/program yourself first and post questions regarding any difficulties you have or about a particular function of the code that you don't know how to achieve.

Please read the Posting Guidelines and particularly the Coursework Posting Guidelines.

Then when you are ready post a new question in this thread.
Sep 25 '07 #5
way87
8
is it ok?
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. int square(int y);
  3.  
  4. int main (void)
  5. {
  6.  int x;
  7.  for (x = 0;x>=0;x++) {
  8.  printf ("%d",square (x) );
  9.  }
  10.  
  11.  printf("\n");
  12.  
  13.  return 0;
  14. }
  15.  
  16. int square (int y)
  17. {
  18.  return y*y;
  19. }
Sep 25 '07 #6
kreagan
153 100+
Question:
Write a Program using Functions that calculates X^Y where both X and Y are doubles and X is positive. (Hints: Call the function pow() in <math.h>)
(define x x)
(define y y)
(pow x y)

hehehehe.
Sep 25 '07 #7
sicarie
4,677 Expert Mod 4TB
tis is wat i hav done ,but it seems not correct =(
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. int square(int y);
  3.  
  4. int main (void)
  5. {
  6.  int x;
  7.  for (x = 1;x>=0;x++) {
  8.  printf ("%d",square (x) );
  9.  }
  10.  
  11.  printf("\n");
  12.  
  13.  return 0;
  14. }
  15.  
  16. int square (int y)
  17. {
  18.  return y*y;
  19. }
Why aren't you using the easy method your teacher suggested? Why are you reinventing something that has already been created?

Also, why use a for loop? Was that a requirement as well? FYI - you declare them as ints, not doubles.
Sep 25 '07 #8
kreagan
153 100+
tis is wat i hav done ,but it seems not correct =(
Expand|Select|Wrap|Line Numbers
  1.  for (x = 1;x>=0;x++) {
  2.  printf ("%d",square (x) );
  3.  }
  4. }
X is equal than 1, continue while X is greater than 0, after each iteration, increase X by 1.

I hope you see atleast 1 problem with this for loop.
Sep 25 '07 #9
way87
8
is it ok?
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. int square(int y);
  3.  
  4. int main (void)
  5. {
  6.  int x;
  7.  for (x = 0;x>=0;x++) {
  8.  printf ("%d",square (x) );
  9.  }
  10.  
  11.  printf("\n");
  12.  
  13.  return 0;
  14. }
  15.  
  16. int square (int y)
  17. {
  18.  return y*y;
  19. }
i still have no idea about doubles =(
Sep 25 '07 #10
sicarie
4,677 Expert Mod 4TB
i still have no idea about doubles =(
http://www.cplusplus.com/doc/tutorial/variables.html

If you are having trouble with something that basic, we are going to have a difficult time helping you with your programs.

Ask to sit down and spend an hour with your teacher or TA going over variables, program structure, and control structures (loops), or you are going to have a LOT of trouble very quickly.
Sep 25 '07 #11
way87
8
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. int main ()
  5. {
  6.     double x = 1;
  7.     double y = 1;
  8.     double result;
  9.  
  10.     for ( x = 1; x <= 10; x++ ){
  11.         printf("%f\n",result = pow(x,y) );
  12.         }
  13.  
  14.  
  15.     return 0;
  16. }
  17.  
i did it but the output is seems not correct:
1
2
3
4
5
6
7
8
9
10
Sep 25 '07 #12
JosAH
11,448 Expert 8TB
#include <stdio.h>
#include <math.h>

int main ()
{
double x = 1;
double y = 1;
double result;

for ( x = 1; x <= 10; x++ ){
printf("%f\n",result = pow(x,y) );
}


return 0;
}

i did it but the output is seems not correct:
1
2
3
4
5
6
7
8
9
10
That output is perfectly correct because for every x != 0, x^1 == x.

kind regards,

Jos
Sep 25 '07 #13
sicarie
4,677 Expert Mod 4TB
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. int main ()
  5. {
  6.     double x = 1;
  7.     double y = 1;
  8.     double result;
  9.  
  10.     for ( x = 1; x <= 10; x++ ){
  11.         printf("%f\n",result = pow(x,y) );
  12.         }
  13.  
  14.  
  15.     return 0;
  16. }
  17.  
i did it but the output is seems not correct:
1
2
3
4
5
6
7
8
9
10
Okay, let's trace through one of these

x=1
y=1

for x =1
while x<10 // while 1< 10
print pow(x,y) // print x^y -> print 1^1(1)
x++ // x=2
while x< 10 // while 2 < 10
print pow (x,y) // print x^y -> print 2^1 (2)
etc...

What you are printing out is correct math. Set y to something else and you will see it.
Sep 25 '07 #14
way87
8
it should not be correct..
becoz the output should be:
1
4
9
16
25
36
49
64
81
100

?
Sep 25 '07 #15
sicarie
4,677 Expert Mod 4TB
it should not be correct..
becoz the output should be:
1
4
9
16
25
36
49
64
81
100

?
No, not the way you have it written. You do nothing to 'y' through the whole thing. So it is always 1. You need to increment y.
Sep 25 '07 #16
way87
8
im really stuck,i dunno how to fix it
Sep 25 '07 #17
way87
8
#include <stdio.h>
#include <math.h>

int main (void)
{
double x;
double y;
double result;

printf("Enter an integer for x\n");
scanf("%d",&x);

printf("Enter an integer for y\n");
scanf("%d",&y);

result = pow(x,y);

printf("x raised to power y is %f\n",result);

return 0;

}

it not works,i input 8 as x and 2 as y,the output is 1...
Sep 25 '07 #18
oler1s
671 Expert 512MB
First, please use CODE tags unless your code is something like one function call or some obviously readable block. Be on the safe side as a beginner and always use them.

Next, why declare x and y as doubles if you treat them as integers in your program? Shouldn't you rather treat them as doubles? For example, your scanf format specifier would be %f, not %d, right? (You should be verifying this with a C reference that you can easily google. Or type in "man scanf" in Google).
Sep 26 '07 #19

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

Similar topics

1
by: GM | last post by:
Hello. I am starting my 7th week of an intro to programming class and have a problem I have been working on. My instructor said we could look for source code on the internet and use it as long as...
5
by: Ron Adam | last post by:
Hi, I'm having fun learning Python and want to say thanks to everyone here for a great programming language. Below is my first Python program (not my first program) and I'd apreciate any...
5
by: cj | last post by:
Thanks to everyone that has helped me. Now I'm trying to write my first program. I have an example of one that I need to write about. Any help getting me started is appreciated. I'm having trouble...
109
by: zaidalin79 | last post by:
I have a java class that goes for another week or so, and I am going to fail if I can't figure out this simple program. I can't get anything to compile to at least get a few points... Here are the...
1
by: coolindienc | last post by:
I converted this program from using global variables to local variables. When I did that my while loop stopped working in my main function(module). Anyone, any idea why? I underlined the area where...
5
by: jbailey006 | last post by:
I am trying to add the following information to a payroll program I am working on for assignment. Modify the Payroll Program so that it uses a class to store and retrieve the employee's name, the...
4
nexcompac
by: nexcompac | last post by:
Ok, I posted a similar post but now need to jump back into it. Here is what I have been able to clean up. I am using textpad and jbuilder. Still getting used to the whole java world and I am...
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...
3
by: Starbuck01 | last post by:
I have to write a program for my AP Computer Science Class. Here is the instructions. The Police Department is asking for help in catching those who speed. You will write a batch style program...
3
by: strangetorpedo | last post by:
The problem lies under where if (choice == '2') is...I need the program to keep prompting for num and den values as long as the user enters values less than or equal to 0. Once they are values...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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.