473,327 Members | 2,025 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,327 software developers and data experts.

Explanation

Can someone explain me what the
Expand|Select|Wrap|Line Numbers
  1. y=-y;
stands for in
Expand|Select|Wrap|Line Numbers
  1. #include<iostream.h>
  2. using namespace std;
  3. int main(){
  4. int x, y;
  5. float product=1 ;
  6. cout << "Enter a base number: ";
  7. cin >> x;
  8. cout << "Enter an integer exponent: ";
  9. cin >> y;
  10. int i = 1;
  11. if (y<0)
  12. { y=-y;//HERE I AM
  13. while (y>=i)
  14. {
  15. product *= x;
  16. i++;
  17. }
  18. cout<<x<<"to the power of "<<y<<"equals="<<(1/product);
Jun 7 '12 #1
9 1782
Mariostg
332 100+
What would be your guess?
Do you think y=-y is like y=(-1)*y ?
See if you can cout<<y after the assignment. What do you conclude?
Jun 7 '12 #2
When i compiled it it gives the same result,and i think it's the same.
Jun 7 '12 #3
Mariostg
332 100+
So you answered your own question. Now does your program do what you want it to do? Maybe not.
Jun 7 '12 #4
uhm...why not? if that line is there the program it will not work for negative power.
Jun 7 '12 #5
Mariostg
332 100+
Well I am not sure what you are trying to achieve.
But consider this:
Expand|Select|Wrap|Line Numbers
  1. if (y<0) 
  2. { y=-y;//HERE I AM 
  3.  
Don't you find this odd? Your code is saying that if y<0 then make y=-y.
Jun 7 '12 #6
@Mariostg
Yes but how can i do it without that?
Jun 7 '12 #7
Mariostg
332 100+
Well, you need to explain what your problem is. It is the first time you mention an issue with "negative power". This has nothing to do with your first question. What do you want to do with the variable "y"?. You want to make it positive if smaller than 0?
Jun 7 '12 #8
well,i have to do a program that will do A to the power B.
B can be 1,0,positive or negative.I have done this program.And that line
Expand|Select|Wrap|Line Numbers
  1. y=-y
i have writed it because i had no ideas how i can do it to work with negative powers.And i i erease that line the program it will not work for negative power and if i keep it it works.I want to know what that line means and why i can't do it without it.
Jun 7 '12 #9
divideby0
131 128KB
say y is -2 it can be rewritten as

y = -y
y = - (-2)
y = +2

math property: two like signs become positive.

you only need one loop; it should work whether or not the power is pos, 0, or neg. so long as you initialize result to 1.

Expand|Select|Wrap|Line Numbers
  1. while(i < (power > 0 ? power : -power)) {
  2.    result *= base;
  3.    ++i;
  4. }
the ? operator is handy because it behaves like an if/else. if power is greater than 0 use it as it is, and if it's less than 0, "make" it positive. when you ouput the result, you can use the same test.

Expand|Select|Wrap|Line Numbers
  1. std::cout << base << "^" << power << " = "
  2.           << (power > 0 ? result : 1/result);
  3.  
or use an if/else when outputting the result depending on power's sign.
Jun 7 '12 #10

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

Similar topics

2
by: Swartz | last post by:
Hi all. I'm building a development webserver (redhat-based). I'm trying to compile PHP (v4.3.4 if anyone cares) with all the features I might require in the near future. I've ran into a problem...
3
by: David MacQuigg | last post by:
I am writing a chapter for teaching OOP in Python. This chapter is intended as a brief introduction to replace the more complete discussion in Learning Python, 2nd ed, pp. 295-390. I need to...
2
by: MatthewRoberts | last post by:
Howdy All, I have a Windows Service that often stops in its tracks with no exception and no explanation on our QA system. During testing on the development machine, it can handle any workload,...
1
by: jimfortune | last post by:
From: http://groups-beta.google.com/group/comp.databases.ms-access/msg/769e67e3d0f97a90?hl=en& Errata: 19 solar years = 2939.6018 days should be 19 solar years = 6939.6018 days Easter...
2
by: Dave Taylor | last post by:
Is there a decent explanation of how menu merging with MDI forms work in VB.NET? I've read through the online help and it still seems that whenever I change menus around or whatever, it breaks...
12
by: jacob navia | last post by:
Hi I am writing this tutorial stuff again in the holidays and I came across this problem: The "width" field in printf is a minimum width. Printf will not truncate a field. for instance:...
4
by: dismantle | last post by:
Hi all, this is my 3rd week in studying VB codes and i came across with this codes from a online tutorial about classes. Public Function MiddleInitial() As String MiddleInitial =...
6
by: WolfgangS | last post by:
Ok first off, i am a total beginner at this groups stuff and i have no clue how this works. This is probabaly the wrong group for my problem but i will post it anyways. Learning by doing right? ...
16
by: DamienS | last post by:
In the interests of me saving hair, can someone please explain to me what's going on below? Why doesn't == work in comparing two int's when cast as objects? They're the same type. Note that it...
15
by: MNNovice | last post by:
Recently I posted a problem to generate a report which will pull titles of DVDs where two casts are common. My table name was ItemsCast which linked two tables (Items and Cast) and the field is...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.