473,472 Members | 2,163 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

'For' loop problem involving i <= e Vs. i < e.

36 New Member
Hello again!

Ok, I wrote the following program ( just messing around ), to find the exponent of a user's number.

Here's the problem:

If I place the less-than symbol ( < ) in the 'for' loop, it works fine.
However, the second I place the less-than or equal to operator ( <= ) in it's place, the code returns some crazy numbers.

I'm using: Win XP; & Dev-C++ 4.9.9.2;

Here's the code WITH the LToET operator:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using std::cout;
  3. using std::endl;
  4. using std::cin;
  5.  
  6.  
  7. int main()
  8. {
  9.   int x = 0;
  10.   int y = 1;
  11.   int exp = 0;
  12.   int choice = 0;
  13.  
  14.   cout << "Enter the base number: ";
  15.   cin >> x;
  16.   cout << endl;
  17.  
  18.   cout << "Now enter the exponent you want for " << x << ": ";
  19.   cin >> choice; 
  20.   cout << endl;              //    |   Here it is...
  21.                                    //    V
  22.   for (int index = 0; index <= choice; index++)
  23.   {
  24.     y *= x;
  25.   }
  26.  
  27.   cout << x << "^" << choice << " = " << y << endl;
  28.  
  29.   return 0;
  30. }
  31.  
I must be missing something, because I didn't think there was much difference
between the two operators ( besides the obvious one of course 8^) ).

I appreciate any help anyone could give me. It's not a huge problem, but it is
a bit frustrating.

Thanks,

-Soneji
Apr 28 '07 #1
3 1522
JosAH
11,448 Recognized Expert MVP
If you want to raise x to the power of n you have to multply n x's so you have
to have a loop that executes its body n times; there are two choices:
Expand|Select|Wrap|Line Numbers
  1. for (int i= 0; i < n; i++) ...
or this one:
Expand|Select|Wrap|Line Numbers
  1. for (int i= 1; i <= n; i++) ... 
If you do this instead (what you did in your code):
Expand|Select|Wrap|Line Numbers
  1. for (int i= 0; i <= n; i++) ...
you effectively raise x to the power n+1.Note that overflow can cause funny
numbers too.

kind regards,

Jos
Apr 28 '07 #2
cbbibleboy
29 New Member
Also, if you just include math.h, there's an pow() function...
Apr 28 '07 #3
Soneji
36 New Member
Thanks for the reply!

I should have caught that. I haven't dealt with the <= operator much, but I think I've got it now.

Simply put: I was multiplying y times x as long as 'index' was "less than or equal to" 'choice'.

That "equal to" was giving me an extra iteration that I didn't need. Correct?

That's twice you've helped me JosAH, Thanks!

cbbibleboy, I haven't used 'math.h' much yet. But I'll look into it. Thanks!

Thanks again for your time,

Lates,

-Soneji
Apr 29 '07 #4

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

Similar topics

2
by: M P | last post by:
Can anyone help me find a code for Database Paging? I have seen some of it but its very hard for me to read, its too advance. Need to have a code with beginner to intermediate level. thanks, ...
4
by: Tjerk Wolterink | last post by:
Hello all, i have a xsl:for-each loop like this: <xsl:for-each select="xc:agendapunt"> <xsl:sort select="xc:datum/xc:year" data-type="number"/> <xsl:sort select="xc:datum/xc:month"...
4
by: LT | last post by:
Good day! I'm trying to read in values from input file (in_file) and add them to another file (out_file) Repeat the same process but opening a different file... I'm trying to use a combination of...
5
by: Henry Jordon | last post by:
hello I was wondering if someone could help me get a main going on this project I've completed the header file that the professor started us on but not really sure how to get the main going. If...
2
by: Krisitne | last post by:
I need to use a for statement to write some pseudocode. I am not sure how to actually write it - using a for loop. Could someone please give me an example of what a for statment would look like...
52
by: Rick | last post by:
Hi, For portability, can an issue arise if we use while(1) for an infinite loop in C? If so, should we then use for(;;)? Thanks, Rick
2
by: hexusnexus | last post by:
I wrote a simple algorithm and it keeps getting stuck in a loop. I guess I'm just to tired to figure it out: compcount= suitrank= trump=2 l,lt=0,0 while l<4: while lt<4:
2
ADezii
by: ADezii | last post by:
If you are executing a code segment for a fixed number of iterations, always use a For...Next Loop instead of a Do...Loop, since it is significantly faster. Each pass through a Do...Loop that...
16
by: Kapteyn's Star | last post by:
Hello all, I want a function to make the program pause for a few seconds but K&R have not listed anything for this i even looked in index. can anyone help? Thank you very much. --...
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...
1
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,...
1
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...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...

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.