#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    5
    Rep Power
    0

    Unhappy Urgent help needed for a Newbie C learner.


    My teacher asked me to write a simple C program using "while" loop which shows output like given below when executed:
    1
    2 3
    4 5 6
    7 8 9 10
    ... ... ... ... n

    user can input a number "n"

    so I tried it myself and wrote a code like this:

    Code:
    #include<stdio.h> 
    int main() 
    {     
    int i,a,c,n;
    printf("enter an integer\n");
    scanf("%d", &n);     
    i = 1;     
    while (i <= n)    
    {         
    c=1;         
    while ( c <= i)         
    {         
    printf("%d ",c);         
    c++;         
    }      
    i++;     
    printf("\n");     
    }     
    return 0; 
    }
    this code shows output like this:
    1
    1 2
    1 2 3
    1 2 3 4
    ... ... ... ... n

    Can anyone please tell me what I missed here? I just want to show the output exact same way my teacher asked me to.
    thanks in advance :o
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,091
    Rep Power
    2222
    With your general lack of naming conventions, it's difficult to keep track of what variable does what, so you might be confusing yourself.

    There's an old tradition from FORTRAN about naming loop-control variables which probably dated from 1966, a decade after the creation of FORTRAN. FORTRAN allowed you to have either integer (AKA "INTEGER") or floating-point (AKA "REAL") numeric variables, plus it would create the variable automatically as soon as you used it (which led to many stupendous bugs due to misspelling an existing variable). In order to determine whether to make this new variable INTEGER or REAL, FORTRAN compilers would use the first letter of the variable's name: if the name started with I through N then it would be an INTEGER (think of the first two letters of "INTEGER"), otherwise it would be a REAL. From that rule, the custom developed to name loop variables I, J, K, L, M, N, with the outer-most loop being I, the next one in J, the third one in K, etc.

    This practice has spread to just about every programming language I've encountered, even C, so when any programmer encounters variables i, j, k, etc, he immediately recognizes them as very likely being loop variables. Similarly, every programmer will immediately declare his loop variables by those names without giving it a second thought.

    You have two loops, so why not name those two loop variables i and j? You need to control the number of columns, so why not use c for that, incrementing c with each line to increase the number of columns by one each time? What you want to print out increases by one each time it's printed out regardless of which line or column it's in, so why not initialize a variable to 1 and just increment it each time? n is already taken, as are j and c, but i doesn't really serve any purpose. Or you could use a different variable name, use i to control the looping through the columns, and eliminate j.

    In case you haven't figured out yet why you're getting that wrong output, you're printing out c, but you reinitialize c to 1 for each line, which you need to do if you're using it to control the number of columns printed (which you are doing), but not if that's the value to be printed.

    So think about what each variable is supposed to do and what meaningful names you can give them.

    Comments on this post

    • tredway agrees
    Last edited by dwise1_aol; February 4th, 2013 at 03:40 PM.
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,091
    Rep Power
    2222
    I was wrong. You are using i as the row number to see how many columns to display per line.

    I notice that you declared a variable, a, and never used it. Maybe a should be the output value that you increment each time you display. Also, you would compare a to n.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    5
    Rep Power
    0
    Originally Posted by dwise1_aol
    With your general lack of naming conventions, it's difficult to keep track of what variable does what, so you might be confusing yourself.

    There's an old tradition from FORTRAN about naming loop-control variables which probably dated from 1966, a decade after the creation of FORTRAN. FORTRAN allowed you to have either integer (AKA "INTEGER") or floating-point (AKA "REAL") numeric variables, plus it would create the variable automatically as soon as you used it (which led to many stupendous bugs due to misspelling an existing variable). In order to determine whether to make this new variable INTEGER or REAL, FORTRAN compilers would use the first letter of the variable's name: if the name started with I through N then it would be an INTEGER (think of the first two letters of "INTEGER"), otherwise it would be a REAL. From that rule, the custom developed to name loop variables I, J, K, L, M, N, with the outer-most loop being I, the next one in J, the third one in K, etc.

    This practice has spread to just about every programming language I've encountered, even C, so when any programmer encounters variables i, j, k, etc, he immediately recognizes them as very likely being loop variables. Similarly, every programmer will immediately declare his loop variables by those names without giving it a second thought.

    You have two loops, so why not name those two loop variables i and j? You need to control the number of columns, so why not use c for that, incrementing c with each line to increase the number of columns by one each time? What you want to print out increases by one each time it's printed out regardless of which line or column it's in, so why not initialize a variable to 1 and just increment it each time? n is already taken, as are j and c, but i doesn't really serve any purpose. Or you could use a different variable name, use i to control the looping through the columns, and eliminate j.

    In case you haven't figured out yet why you're getting that wrong output, you're printing out c, but you reinitialize c to 1 for each line, which you need to do if you're using it to control the number of columns printed (which you are doing), but not if that's the value to be printed.

    So think about what each variable is supposed to do and what meaningful names you can give them.

    I wrote a new code according to the suggestions u made. Can you check it once more? If you find more problems please point them out for me. thank you for your help :)
    Code:
    #include<stdio.h> 
    int main() 
    {     
    int i,j,c,n,a;     
    printf("enter a number\n");     
    scanf("%d", &n);     
    i=1;     
    a=1;     
    while(i<=n)     
    {         
    j=1;         
    while(j<=i)         
    {             
    printf("%d ", a);             
    a++;             
    j++;         
    }         
    printf("\n");         
    i++;     
    } 
    }
  8. #5
  9. No Profile Picture
    Still Learning
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Location
    Montreal, Canada
    Posts
    55
    Rep Power
    39

    Loopy code


    You are missing the point of what you are trying to do.

    Your variable i should increment every iteration of you inner most loop.
    It is the count you are displaying. The loop on i should limit the total output.

    The inner loop on j should only print out the number of items matching to the line count. You do not have the line count info directly because you are not counting which line you are writing.


    Let's say we use c to hold the line count. That way the inner loop just has to run from 1 to c. Each time it prints the value of i and increments it. This is the key, i is incremented in the inner loop whose only purpose is to limit the number of item output to the line count.
  10. #6
  11. No Profile Picture
    Still Learning
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Location
    Montreal, Canada
    Posts
    55
    Rep Power
    39
    Here is a simpiler version that does not stop at n but stops on the line that contains n.

    Code:
    #include<stdio.h> 
    int main() {     
    int i,j,c,n,a;        
    n=100;     
    i=1;     
    
    c=0;  
    while(i<=n) { 
      printf(" Line %d : ", ++c);
              
      j=1; 
            
      while(j<=c){             
        printf("%d ", i);             
        i++;             
        j++;         
      }         
      printf("\n");         
          
    } 
    }
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    5
    Rep Power
    0
    Originally Posted by admiraln
    You are missing the point of what you are trying to do.

    Your variable i should increment every iteration of you inner most loop.
    It is the count you are displaying. The loop on i should limit the total output.

    The inner loop on j should only print out the number of items matching to the line count. You do not have the line count info directly because you are not counting which line you are writing.


    Let's say we use c to hold the line count. That way the inner loop just has to run from 1 to c. Each time it prints the value of i and increments it. This is the key, i is incremented in the inner loop whose only purpose is to limit the number of item output to the line count.
    thank you. I got it now :D

IMN logo majestic logo threadwatch logo seochat tools logo