nested while-loop + undetermined error

This is a discussion on nested while-loop + undetermined error within the C++ Programming forums, part of the General Programming Boards category; I was working on a problem and able to detect the source. So, I keep isolating until I got the ...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    43

    Question nested while-loop + undetermined error

    I was working on a problem and able to detect the source. So, I keep isolating until I got the short code that caused me to buzz my head.

    Here is the code:
    Code:
    #include <iostream>
    #include <cmath>
    using namespace std;
    int main()
    {
    	
    	int power=1;
    	int i=1;
    	int counter=1;
    	while (i<=10)
    	{
    		cout << "i = " << i << endl;
    		while (counter<=(10-i))
    		{
    			power *= 2;
    			counter++;
    		}
    		cout << power << endl;
    		i++;
    	}
    	return 0;
    }
    If I replaced counter <= i, the program will output correctly
    Code:
    2
    4
    8
    ....
    1024
    In order to replicate my program, I tweaked it to be counter <= (10-i), then it started to screw up.
    Code:
    512
    512
    512
    ......
    I thought this is weird, since I analyzed it this way:
    At i=1, j=1, then power = 2
    At i=1, j=2, then power = 4
    ......
    At i=1, j = 9, then power = 2^9 = 512
    ------------------------------------------------
    At i=2, similarly, at the end of the inner-loop, j=8, the power should be 256?

    Perhaps, I misconcept something??
    Any hints will be so useful!
    THanks!

  2. #2
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,145
    if I understand your question correctly...
    Code:
    counter <= i
    should be what you want (multiply "power" by 2 i number of times)
    it is more idiomatic to use a "for" loop in this case (for both of the loops)

    also, don't forget to set counter = 1 and power = 1 before the inner loop. Actually, it would be a good idea to declare them inside the outer loop.

    Code:
    #include <iostream>
    #include <cmath>
    using namespace std;
    int main()
    {
    	
    	for (int i = 1; i<=10; ++i)
    	{
    		cout << "i = " << i << endl;
    		int power = 1;
    		for (int counter = 1; counter <= i; ++counter) {
    			power *= 2;
    		}
    		cout << power << endl;
    	}
    	return 0;
    }
    or the "while" variant (they are logically equivalent)
    Code:
    #include <iostream>
    #include <cmath>
    using namespace std;
    int main()
    {
    	
    	int i = 1;
    	while (i<=10)
    	{
    		cout << "i = " << i << endl;
    		int power = 1;
    		int counter = 1;
    		while (counter <= i) {
    			power *= 2;
    			++counter;
    		}
    		cout << power << endl;
    		++i;
    	}
    	return 0;
    }
    Last edited by cyberfish; 05-03-2008 at 04:49 PM.

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    43
    thanks!!
    When using counter <= i the output will be
    Code:
    2
    4
    8
    16
    ...
    1024
    I'd like to achieve the reverse.
    But, thanks, initializing "power" and "counter" inside the outer loop solve the problem.
    however, as I understand, variable should be replaced by new values whenever they're re-called, right?
    Shouldn't it matter whether you place the initialization out of the outer loop?

    Thanks!

  4. #4
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,145
    however, as I understand, variable should be replaced by new values whenever they're re-called, right?
    not true. Variables only change value when you explicitly set it to equal a new value.

    you could leave the initialization (and declaration) outside the loop, just that you have to initialize (set) them in each iteration.

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    No, power just keeps accumulating =*2. If you want it to start from 1 in the outer loop, you have to make it equal to 1 yourself. (It doesn't matter where they are declared, variables are just not magically reset.)
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  6. #6
    Registered User
    Join Date
    May 2008
    Posts
    43

    Unhappy

    hey thanks!
    The actual goal for this code is to calculate the sum of all digits of an integer.
    I'm sure there's smarter way doing this. Here is what I did (using loops and division/modulus)

    1. Input number (of course!)
    2. Count how many digits it has (digitCount)
    3. The first digit is easy, firstDigit=number/10^(digitCount-1)
    4. For these middle digits, I used nested while loops, 2 of them. The last digit is excluded from these loops, however.
    Code:
    int counter=1;
    int part_number;	
    while(counter < (digitCount - 1))
    	{
    		//Need to calcuate power of base 10
    		int power=1;
    		int counter_power=1;	
    		while (counter_power<= (digitCount-counter))
    		{
    			power = power*10;
    			counter_power++;
    		}
    		//Ending power calculation
    		part_number = number2 % power;
    		digit =  part_number / (power/10);
    		digitSum = digitSum + digit;
    		counter++;
    	}
    For example, 12345 = number, then part_number = 2345, then digit digit = 2345/1000 = 2 .
    So on so forth until the loop reach digit=4.
    When the loop is terminated, part_number = 45. Therefore, lastDigit=part_number/10 = 4. Then, it's trivial to calculate the sum of all digits.
    At the end, there's an "if" to check whether the Sum is dividable by 9.

    However, as I executed the .exe file. I kept receiving an pop-up that said : "Debug Error: Run-time check failure #3: variable part_number is being used without being initialized" .

    As you saw, part_number has been definitely declared
    I've been baffled by this without clues. Perhaps, I used many variables? 11 in this exercise.

    Thanks so much for any hints!

  7. #7
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Keep in mind that x &#37; 10 returns the last digit and x / 10 removes the last digit. It would be rather simple to sum the digits using these operations, except keep in mind to work with a copy of the input to keep the original value (won't be a problem if you use a function).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  8. #8
    Registered User
    Join Date
    May 2008
    Posts
    43
    my bad..it was a typo..I actually used &#37; 10 for the last digit in my source code.

    I'm certain that I've been working with copy of input.
    I'm not sure what caused the runtime-error though

  9. #9
    Registered User
    Join Date
    May 2008
    Posts
    43
    to make my point clearer
    I pasted my source code here

    I atually got run-time error when inputing 2-digit number.
    I suspected that the first while-loop is imperfect.

    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
    	int number1,number2, firstDigit;
    	int part_number, digit, lastDigit, digitTotalSum;
    	int counter=1;
    	int digitSum = 0;
    	int digit_count=0;
    	
    	//Ask for number;
    	cout << "Please enter a number: ";
    	cin >> number1;
    	//number2 will be used for later steps
    	number2=number1;
    	cout << endl;
    
    	//First, we need to know how many digit it has
    	//The loop keeps checking until number1 get to ZERO (false)
    	//This loop will also calculate the power of base 10
    	//in order to extract the 1st digit.
    	int firstPower=1;
    	while (number1)
    	{
    		number1 /= 10;
    		firstPower *= 10;
    		digit_count++;
    	}
    	//------------------------------------------------------
    
    	
    	//The first digit
    	firstDigit=number2/(firstPower/10); 
    	
    	//This loop will take care of the middle digits
    	while(counter < (digit_count - 1))
    	{
    		//Need to calcuate power of base 10
    		int power=1;
    		int counter_power=1;	
    		while (counter_power<= (digit_count-counter))
    		{
    			power = power*10;
    			counter_power++;
    		}
    		//Ending power calculation
    		part_number = number2 &#37; power;
    		digit =  part_number / (power/10);
    		digitSum = digitSum + digit;
    		counter++;
    	}
    	//------------------------------------------------------
    	//This will calculate the last digit and the sum
    	lastDigit=part_number % 10;
    	digitTotalSum=digitSum + lastDigit+firstDigit;
    	cout << "The sum is: " << digitTotalSum << endl;

  10. #10
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I don't see where part_number is used without being initialized if this is exactly the same code that you are using. You mat try initializing part_number to something, like 0.

    However, what I really meant in my last post was that you can sum digits using only &#37;10 and /10 to manipulate the value - without all the powers (you probably don't need to recalculate that each time).

    Edit:
    The comment applied to an earlier snippet.
    It is clear that if there are 2 digits the while loop that assigns a value to part_number is not entered at all, therefore you are indeed using it uninitialized after the loop.
    If you look at your code that counts the digits and think about my hints, wouldn't you be able to sum the digits right there - instead of counting the digits.
    Last edited by anon; 05-05-2008 at 12:34 PM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  11. #11
    Registered User
    Join Date
    May 2008
    Posts
    43
    uhmm i got your point!
    how come I didn't think of that . I certainly will try it to shorten the code which I'm not really happy with.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  3. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 05:45 AM
  4. Stupid compiler errors
    By ChrisEacrett in forum C++ Programming
    Replies: 9
    Last Post: 11-30-2003, 04:44 PM
  5. UNICODE and GET_STATE
    By Registered in forum C++ Programming
    Replies: 1
    Last Post: 07-15-2002, 03:23 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21