facebook like button

26 March, 2011

program 12: addition of n numbers

The need:
     You have already seen our program 3, addition of 2 numbers. What if the count of numbers to be added is 100 or so? Do I need to write 'scan()' statement 100 times in that case? what if the count of the numbers to be added is not fixed for a program? Can it be dependent on user's wish? If it can then how? You have already started guessing that there should be a statement which would cause the single 'scan()' statement to repeat 100 times or say n times. The program below answers all these questions.

The code: 
-------------------------------------------- 
/*Addition program using goto*/
#include<stdio.h>
main()
{
    int m=0,n;
    float i,sum=0;
    printf("how many numbers do you want to add?\n");
    scanf("%d",&n);
    printf("Now keep on giving numbers ...\nPress ENTER after each number...\n");
    again:
    scanf("%f",&i);
    sum=sum+i;
    m=m+1;
    if(m<n)
    goto again;
    printf("__________________\n=%f\n\n\n",sum);
}
--------------------------------------------
   Up till the previous post you have come to know that any C program executes in sequential order. I mean the statement written first gets executed first. Now come to this program. Here in C we have a 'goto' statement which causes the execution of program to shift to some other position in the program. That position is specified by a label (in our case name of label is 'again'). Name of the label can be anything lets say my name 'ranu'. The label is put some where in program code with a colon(:) after it. See the syntax of 'goto' statement. It is usually put after if statement because we want execution of program to jump to a particular location within a program only based on a condition. The reason for that is in following sentence. Assume a situation without a condition in our case each time program encounters 'goto' its execution  goes to label 'again' and in sequential flow of program again 'goto' is encountered because there is no condition so each time program jumps to label 'again' unconditionally.
So the execution of the program is trapped in goto loop and the program will never come to an end.
    Now for our program if you start thinking of a condition(because you need one) you can easily think that the number of scan() statement should be 'n' because we have to take 'n' inputs from the user. So you can think of a counter variable that will count the number of scans. Here 'm' is such a counter variable. Observe initially when not even a single scan is executed, value of 'm' is 0(we say 'm' is initialized to 0). As any scan takes place in the next line 1 is added to 'm'. If m is less than n(count of number to be added given by user) or we can also say that is number of scans is less than n(count of number to be added given by user) then jump to again' else don't. Recall the use of 'if' statement.
 
Remarks:

   a). Now a days use of 'goto' statement is not advisable. You should use avoid using 'goto' statement. If you have question, "How shall we doing iterations?". Answer is: We have alternate loop statements(or loops). There are 3 types of loops in C.
1. 'for' loop.
2. 'while' loop
3. 'do....while' loop
These loops will be demonstrated in upcoming posts.

   b). Concept of 'counter' is very common in loops. These counters help to limit the number of iterations of a loop. There is also a concept of 'sentinel' which also helps in limiting the number of iterations of a loop. which will be explained in upcoming posts.

No comments:

Post a Comment

feel free to ask your doubts... if any
you can post your doubts on
www.facebook.com/programsimply