C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Closed Thread
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 4th, 2013, 02:54 PM
tredway tredway is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 4 tredway User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 58 m 43 sec
Reputation 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

Reply With Quote
  #2  
Old February 4th, 2013, 03:37 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,134 dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 3 Days 20 h 49 m 9 sec
Reputation Power: 1974
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.

Reply With Quote
  #3  
Old February 4th, 2013, 07:11 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,134 dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 3 Days 20 h 49 m 9 sec
Reputation Power: 1974
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.

Reply With Quote
  #4  
Old February 5th, 2013, 10:10 AM
tredway tredway is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 4 tredway User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 58 m 43 sec
Reputation Power: 0
Quote:
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++;     
} 
}

Reply With Quote
  #5  
Old February 6th, 2013, 03:27 AM
admiraln admiraln is offline
Still Learning
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Location: Montreal, Canada
Posts: 55 admiraln User rank is Sergeant Major (2000 - 5000 Reputation Level)admiraln User rank is Sergeant Major (2000 - 5000 Reputation Level)admiraln User rank is Sergeant Major (2000 - 5000 Reputation Level)admiraln User rank is Sergeant Major (2000 - 5000 Reputation Level)admiraln User rank is Sergeant Major (2000 - 5000 Reputation Level)admiraln User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 18 h 57 m 51 sec
Reputation Power: 38
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.

Reply With Quote
  #6  
Old February 6th, 2013, 03:39 AM
admiraln admiraln is offline
Still Learning
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Location: Montreal, Canada
Posts: 55 admiraln User rank is Sergeant Major (2000 - 5000 Reputation Level)admiraln User rank is Sergeant Major (2000 - 5000 Reputation Level)admiraln User rank is Sergeant Major (2000 - 5000 Reputation Level)admiraln User rank is Sergeant Major (2000 - 5000 Reputation Level)admiraln User rank is Sergeant Major (2000 - 5000 Reputation Level)admiraln User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 18 h 57 m 51 sec
Reputation Power: 38
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");         
      
} 
}

Reply With Quote
  #7  
Old February 6th, 2013, 10:02 AM
tredway tredway is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 4 tredway User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 58 m 43 sec
Reputation Power: 0
Quote:
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

Reply With Quote
Closed Thread

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Urgent help needed for a Newbie C learner.

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap