Thread: Problem with position of variable declaration

  1. #1
    Registered User
    Join Date
    Mar 2014
    Posts
    15

    Problem with position of variable declaration

    Hey guys, I was working on a program to display an output
    Code:
    1
    2    4
    3    9    27
    4    16    64   256
    I got my code working but I noticed something funny...
    Here's what the code with var b defined in the inner 'for' loop
    Code:
    #include <stdio.h>
    #include<math.h>
    
    void main(void) {
    
    	double a=1,b;
    
    	for(;a<5;a++)
        {
            for(b=1;b<=a;b++)
            {
    
            //printf("b is %d\tand a is %d\n",b,a);
                printf("%.0lf\t",pow(a,b));
            }
            printf("\n");
    
        }
    }
    The output is as desired in this case...

    But when I assign the value of b to the declaration statement like this...
    Code:
    #include <stdio.h>
    #include<math.h>
    
    void main(void) {
    
    	double a=1,b=1;
    
    	for(;a<5;a++)
        {
            for(;b<=a;b++)
            {
    
            //printf("b is %d\tand a is %d\n",b,a);
                printf("%.0lf\t",pow(a,b));
            }
            printf("\n");
    
        }
    }
    I get a wrong output like this (?!)
    Code:
    1
    4
    27
    256
    Oh, and the commented line shows that var b is always zero EVEN though it is incremented in the loop...
    Here's the output with that line uncommented and the subsequent line commented...
    Code:
    b is 0  and a is 1072693248
    
    b is 0  and a is 1073741824
    
    b is 0  and a is 1074266112
    
    b is 0  and a is 1074790400

    Thanks in advance for all the help, guys!
    Have a great day!

  2. #2
    Registered User naaissus's Avatar
    Join Date
    Jan 2016
    Location
    Balkan
    Posts
    23
    Code:
    printf("b is %d\tand a is %d\n",b,a);
    Well, to print out double you need to use %lf format specifier.

    You need to reinitialize b to 1 for each pass of 1st (a) loop.
    I. e.

    Code:
    double a=1,b=1;
    for(;a<5;a++)
    {
        for(;b<=a;b++)
        {
            printf("%.0lf\t",pow(a,b));
        }
        printf("\n");
        b = 1;
    }

  3. #3
    Registered User
    Join Date
    Mar 2014
    Posts
    15
    Quote Originally Posted by naaissus View Post
    Code:
    printf("b is %d\tand a is %d\n",b,a);
    Well, to print out double you need to use %lf format specifier.

    You need to reinitialize b to 1 for each pass of 1st (a) loop.
    I. e.

    Code:
    double a=1,b=1;
    for(;a<5;a++)
    {
        for(;b<=a;b++)
        {
            printf("%.0lf\t",pow(a,b));
        }
        printf("\n");
        b = 1;
    }
    Thanks for the insight provided. I appreciate it greatly.
    Quite some embarrassing errors :P
    Thanks again!

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by [David
    ]I was working on a program to display an output
    Code:
    1
    2    4
    3    9    27
    4    16    64   256
    The output looks like an arrangement of integers. You should not be introducing double variables unnecessarily.

    From what I see, you decided to use double because you used pow, but frankly you should not use pow here. Observe:
    16 = 4 * 4
    64 = 4 * 16
    256 = 4 * 64

    So, if you keep track of the previous term for a == 4, you can compute b = 4 * previous_term.

    Quote Originally Posted by naaissus
    Well, to print out double you need to use %lf format specifier.
    %f will do, though %lf is permitted and does make it more consistent with reading.

    By the way, void main should be int main.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Variable Declaration
    By Ducky in forum C++ Programming
    Replies: 5
    Last Post: 05-03-2010, 10:40 AM
  2. Concurrent variable declaration
    By @nthony in forum C Programming
    Replies: 7
    Last Post: 07-19-2006, 11:13 AM
  3. Help with variable declaration please!
    By CodeNinja in forum C++ Programming
    Replies: 2
    Last Post: 07-15-2004, 08:38 AM
  4. Replies: 15
    Last Post: 10-14-2003, 03:08 PM
  5. Variable declaration...
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 06-11-2002, 11:54 AM

Tags for this Thread