473,395 Members | 1,608 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,395 software developers and data experts.

Output wrong..

This is my code and i dont know why when i enter let's say x=3 and n=0 it gives 3.


Expand|Select|Wrap|Line Numbers
  1. #include <cstdlib>
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. int main()                                                      
  7.    {            
  8.          printf("==========©©Andrei78©©==========");
  9.          printf("=======©All Rights Reserved©====");
  10.          printf("===========©05-06-2012©=========");                                                                       
  11.        int count,x,u=1;                                           
  12.        float n,y;                                             
  13.  
  14.        printf("Enter the values of x and n : ");               
  15.        cin>>x>>n;               
  16.        y = 1;                                                
  17.        count = 1;        
  18. do
  19.  {                                                       
  20.             y = y*x;                                           
  21.             count++;           
  22.  
  23.        }        
  24. while ( count <= n);                                        
  25.  
  26.        cout<<y;
  27.        if(n=0){
  28.             cout<<"1";
  29.             }  
  30.  
  31.        system("PAUSE");
  32.     return EXIT_SUCCESS;
  33. }
  34.  
Jun 5 '12 #1

✓ answered by weaknessforcats

First, before you get to your loop check to see if n is 0. If it is, skip the loop and display a 1. Anything to the 0th power is 1.

Second, if n is positive set your loop control variable to n.

Third, if n is negative, set your loop control variable to
-1 *n. So -3 will end up as 3 in the loop control variable.

Fourth, save the number in x.

Fifth, inside the loop calculate y = y * x.

Sixth, after you exit the loop check if n -s negative. If it is, your answer is 1/y. Otherwise, your answer is y.

Seventh, as to spending a ton of time, we once were where you are now. My story is that a barber once asked me what I did for a living. I said I was a C++ programmer. He said that sounded hard. I asked how long it took to become a barber. He said about 300 hours of class time plus 1800 hours of on-the-job haircutting. I thought, hmm 2100 hours. Full tme for year. Now I tell my students C++ programming is as easy as cutting hair -- after you spend 2100 hours practicing it. Good luck.

10 2086
weaknessforcats
9,208 Expert Mod 8TB
This code:
Expand|Select|Wrap|Line Numbers
  1.            y = y*x;                                           
  2.               count++;           
  3.  
  4.          }        
  5.   while ( count <= n);   
  6.  
When n is 0 and count is 1 then count<=n is really 1<=0, which is false so you drop out of the loop.

This time x=3 and y=1 so y*n is 1*3 which is 3.

You display 3.

Beyond that, this code:
Expand|Select|Wrap|Line Numbers
  1. if(n=0){
  2.               cout<<"1";
  3.               } 
  4.  
assigns 0 to n, which will always be false. Probably you meant:

Expand|Select|Wrap|Line Numbers
  1. if(n==0){
  2.               cout<<"1";
  3.               } 
  4.  
Jun 5 '12 #2
Rabbit
12,516 Expert Mod 8TB
That's because a do while loop is guaranteed to run at least once. Use a while loop instead.
Jun 5 '12 #3
Got it.My bad i readed while and while but it was do-while and while sorry
Jun 5 '12 #4
Ok,i solved with positive and 0 powers.But now i am trying to do with negative power.I want it to show :
x=2
n=-1
y=0.5
i changed the code
Expand|Select|Wrap|Line Numbers
  1. while ( count <= n);
with
Expand|Select|Wrap|Line Numbers
  1. while ( count >= n);
but nothing happens.And when i am trying to put it with if(){} it gives the same number with the x(the base) with every number i enter
Jun 5 '12 #5
divideby0
131 128KB
You wouldn't need to change much; add a var to check whether or not the power is negative. If it is, multiply the power by -1 and use division instead of multiplication with the loop.

Always check against div by 0.
Jun 5 '12 #6
weaknessforcats
9,208 Expert Mod 8TB
Remember, a negative power is calculated differetly from a positive power. You will need different logic inside your loop.

Plus, the loop control for -1 power needs to be +1 and the loop control for +1 power needs to be +1. I suggest you have a separate variable for your loop control.
Jun 5 '12 #7
Ok so i put
Expand|Select|Wrap|Line Numbers
  1.  while(n<0){
and then i put
Expand|Select|Wrap|Line Numbers
  1.  y = y*x;
  2.       count--;
  3.       cout<<y;       
  4.  
but the output doesn't stop.I am working for like 6 hours at this project.Can u give me a hint?
Jun 5 '12 #8
weaknessforcats
9,208 Expert Mod 8TB
First, before you get to your loop check to see if n is 0. If it is, skip the loop and display a 1. Anything to the 0th power is 1.

Second, if n is positive set your loop control variable to n.

Third, if n is negative, set your loop control variable to
-1 *n. So -3 will end up as 3 in the loop control variable.

Fourth, save the number in x.

Fifth, inside the loop calculate y = y * x.

Sixth, after you exit the loop check if n -s negative. If it is, your answer is 1/y. Otherwise, your answer is y.

Seventh, as to spending a ton of time, we once were where you are now. My story is that a barber once asked me what I did for a living. I said I was a C++ programmer. He said that sounded hard. I asked how long it took to become a barber. He said about 300 hours of class time plus 1800 hours of on-the-job haircutting. I thought, hmm 2100 hours. Full tme for year. Now I tell my students C++ programming is as easy as cutting hair -- after you spend 2100 hours practicing it. Good luck.
Jun 5 '12 #9
Expand|Select|Wrap|Line Numbers
  1.    while(count<=-1*n){
or
Expand|Select|Wrap|Line Numbers
  1.    while(n<0){
and then what?
how do i save a number..
Jun 6 '12 #10
Mariostg
332 100+
@weaknessforcats
Nice Seventh. :)
Jun 7 '12 #11

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

Similar topics

5
by: Jepsensen | last post by:
Dear Everybody. I got a problem with my cpp code. I'm trying to calculate a very simple Discrete Cosine Transform (DCT), but my c++ code seams to calculate a wrong result. I have just started...
3
by: Zhixin Han | last post by:
Hi, I got a problem while using the ostream operator to print a trace message. Normally it is printing something like this: The code is: os << " ";
1
by: Qiangning Hong | last post by:
I decide to seperate my data collection routine from my data analysis and storage program to a seperate process, so I try to use the new subprocess model in Python 2.4. The main program spawns...
3
by: danmc91 | last post by:
Hi, I'm just getting going with xml and xslt. I'm trying to write what are essentially man pages and I need 3 output formats. 1) nroff -man format for real man pages 2) html for an online...
3
by: mosimu | last post by:
I am using Visual C++ .NET 2003. I have discovered the following problem and did not find any other mention of it on the forums. This code was being run in Debug mode, console application, with...
2
by: Soddy | last post by:
Hello! I'm playing with an Access 2003 (Converted Northwind & Split) DB. I 'copy & paste' the split Access DB into the 'Solution' of C#.NET and make the 'connection'. I then 'copy & Paste' the...
0
by: arlie_maija | last post by:
Hey - I'm writing a control that contains a DataGrid, and I'm unable to get the update event to fire. When I click the update link, the edit event fires. heres the details... my control...
9
by: bobo | last post by:
Hello, How can I output the file with arguments... i.e I have a file called first.php and i have a file called second.php what I want to do is output the first.php, but with arguments......
87
by: pereges | last post by:
I have a C program which I created on Windows machine. I have compiled and executed the program on windows machine and it gives me the consistent output every time i run it. for eg. input a = 2,...
109
by: bonneylake | last post by:
Hey Everyone, Well i am having a problem outputting for my report and hoping someone can explain what i am doing wrong. What the report is about is comparing my company's part price's to d and...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
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,...

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.