Sunday, August 2, 2009

What is this Variable, in C programming?

I'm leaning how to calculate a triangular number and using the "for" statement to do so. Now heres the program:





/* program to generate a table of triangular numbers */





main ()


{


Int n, triangular_number;





Printf ("TABLE OF TRIANGULAR NUMBERS\n\n");


Printf (" n sum from 1 to n\n");


Printf ("--- ---------------\n");





triangular_number = 0;





for ( n = 1; n %26lt;= 10 ++n );


{


triangular_number = triangular_numbers _ n;


Printf (" %d %d\n, n, triangular_number);


}


}





My problem is that I don't exactly know what the variable "n" stands for, in the FOR statement. Can anyone just explain to me what it is, and does?? Thanks a lot.

What is this Variable, in C programming?
A for loop is C's implementation of a so-called "counting loop". The loop is controlled by three semi-colon (;) delimited sections, defined as followed:





-------------


for ( initial_counter ; continue_condition ; counter_increment )


{


loop_statements;


}


-------------





The initial_counter is an assignment statement that initializes the counter for the first iteration of the loop.





The continue_condition is a boolean (true/false) condition (ideally based on the counter) that determines if the loop will continue.





The counter_increment is an assignment or auto-increment expression that changes the value of the counter at the end of a loop iteration.





So the following for loop:


-------------


for ( n = 1 ; n %26lt;= 10 ; ++n )


{


triangular_number = triangular_numbers + n;


printf (" %d %d\n, n, triangular_number);


}


-------------


is a loop that has n assigned to 1 before the first iteration.





At the start of each iteration the loop checks to see if n is less than or equal to 10; if so then the loop continues iterating; if not it ends (control goes to the next statement after the loop). After a successful check, the loop runs the actual iteration, the two statements in the body of the loop. At the end of each iteration, the loop increments the value of n (++n) and goes back to the top of the loop (start of this paragraph).





You can think of this for loop as being equivalent to the following while loop:





-------------


n = 1;


while ( n %26lt;= 10 )


{


/* for loop iteration statements */


triangular_number = triangular_numbers + n;


printf (" %d %d\n, n, triangular_number);


/* end of for loop iteration statements */





++n;


}


-------------
Reply:n is just an incremetor which starts at 1 and will increment to 10. Your code is a bit off and should be the following





for ( n = 1; n %26lt;= 10; n++ )


{


triangular_number = triangular_number + n;


Printf (" %d %d\n, n, triangular_number);


}
Reply:n is nothing but just a default value 1 from where ur loop must begin...........
Reply:It's the counter for the loop. Every time the program goes through the loop n will increment by 1. The loop program will continue to loop until n%26lt;=10, the it will jump out of the loop.





Hope this helps.
Reply:in for loop - "n" is a variable, as the name indicates it "varies" throught the program- means the value it holds changes.





using can use anyalphabet in place of "n" and assign values to it.





like for(a=1; a%26lt;=10; a++)





a=1 is assignment


a%26lt;=10 comparison


a++ means a=a+1 incrementing by 1
Reply:n is the loop variable that makes the loop run 10 times.





It appears that you have the wrong character between the triangular_numbers variable and n inside the for loop, since the underscore character is not an operator.





Check the link below for the definition of the triangular number, and you should be able to figure out what character goes in its place (which is the real reason you asked this homework question, right?)


No comments:

Post a Comment