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

question on a simple for loop

I am tring to write a loop that will do exponents without using the cmath class.

Expand|Select|Wrap|Line Numbers
  1. // Output:  1 2 4 ... 2^(num - 1)
  2.     // First num powers of 2, ^ denotes exponentiation/power
  3.     // There is no operator to do exponentiation in C++.
  4.     cout << "\n 6. ";
  5.     int a = 1;
  6.     cout << a;
  7.     for (int i = 0; i <= num; i++)
  8.     {
  9.         int b = 2;
  10.         int c = 0;
  11.         int d = 1;
  12.         while (d < num)
  13.         {
  14.         c *=b;
  15.         }
  16.         cout << c;
  17.     }
this is what I have so far but im getting a infinite loop, i think im over thinking this can anyone help?
May 4 '07 #1
1 1190
Ganon11
3,652 Expert 2GB
I am tring to write a loop that will do exponents without using the cmath class.

Expand|Select|Wrap|Line Numbers
  1. // Output:  1 2 4 ... 2^(num - 1)
  2.     // First num powers of 2, ^ denotes exponentiation/power
  3.     // There is no operator to do exponentiation in C++.
  4.     cout << "\n 6. ";
  5.     int a = 1;
  6.     cout << a;
  7.     for (int i = 0; i <= num; i++)
  8.     {
  9.         int b = 2;
  10.         int c = 0;
  11.         int d = 1;
  12.         while (d < num)
  13.         {
  14.         c *=b;
  15.         }
  16.         cout << c;
  17.     }
this is what I have so far but im getting a infinite loop, i think im over thinking this can anyone help?
Two issues:

1) Your while loop is being controlled by d - but you aren't changing d's value inside the loop. This is where your infinite loop comes from.

2) Inside this while loop, you multiply c by b to obtain a new c value. But c's initial value is 0. 0 times anything is 0, so your c value will always be 0.

You can probably do this with 2 nested for...loops and not bother with this while loop...
May 4 '07 #2

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

Similar topics

3
by: StepH | last post by:
Hi, I'm building a little application, which the goal is to: 1./ Collect data via Serial line and or via a file (for playback). 2./ Display these data as graph, oscilloscope, ... How manage...
6
by: Arthur J. O'Dwyer | last post by:
I was paging through Coplien's book "Advanced C++ Programming Styles and Idioms" this afternoon and found some code that looked something like void sort(vector<foo> a) { int flip; do { for...
22
by: lokman | last post by:
Hi, In the following code, can someone tell me the difference between *p++ and p++ ? I can see both achieve the same result. Thanks a lot !
13
by: Elaine | last post by:
This has to do with self-joins and includes a running balance problem. Is it possible to do this using SQL alone? Amortization schedule -------------------------- Givens: beginning balance,...
1
by: Brian Henry | last post by:
Hello, I was tring to learn socket's (being i never used them before) and have a simple question. I want to create a listner that will get any data recieved and print it out. I've been able to...
9
by: ash | last post by:
i have got this question in old question papers and i know the answer but didn`t understand how it is possible. main( ) { int x=0; while(x<=50) for( ; ;) if( ++x % 50 ==0) break;
45
by: Robbie Hatley | last post by:
Hello, group. I've been doing too much C++ programming lately, and I'm starting to become rusty at some aspects of the C way of doing things, esp. efficient low-level data copies. ...
15
by: googlegroups | last post by:
I am learning vb.net and trying to create a simple program to watch port 25 and receive email. I do not need any advanced features. I just need to connect, watch and grab the emails as they come. ...
30
by: galiorenye | last post by:
Hi, Given this code: A** ppA = new A*; A *pA = NULL; for(int i = 0; i < 10; ++i) { pA = ppA; //do something with pA
8
by: =?ISO-8859-1?Q?Konrad_M=FChler?= | last post by:
Hi, I've a list of objects. I iterate the list and read the value of each object for many operation like: x = myList.value1 + myList.value2 etc. My question: Is it efficient to always use...
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...
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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?

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.