Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   array (http://www.go4expert.com/forums/array-t11996/)

answerme 10Jul2008 19:20

array
 
Kindly tell me the logic behind 2 statement which are in bold
Code:

main()
{
        int c,i, nwh,noth;
        int ndig[3];
        nwh=noth=0;
        for(i=0;i<3;i++)
                ndig[i]=0;
        while((c=getchar())!=EOF)
                if(c>='0' && c<='9')
                        ++ndig[c-'0'];
                else
                        if(c==' ' |c =='\n'|c=='\t')
                                ++nwh;
                        else
                                ++noth;
        for(i=0;i<3;i++)
        printf("digit =%d",ndig[i]);
        printf(" white space= %d others= %d",nwh,noth);
}


NewUser 11Jul2008 09:18

Re: array
 
Hi,

Actually i am not able to got u.
If you need to know the meaning of these lines then find below:
if(c>='0' && c<='9') Here, we are checking, whether enetred input is between 0 and 9;
Here input is index of array (ndig);
if entered input is between 0 and 9 then in statement ++ndig[c-'0'], we are going to the particular
index of array using this [c-'0'] (index of array is input entered by user) and incresing the value of array of particular index by one.Suppose user is entered 1, this value is greater than o and less than 9.So conpiler will execute statement ++ndig[c-'0'], in this statement compiler will find the index of array through statement c-'0', in this statement c is enetered digit, eneterd digit is 1 in chat this will be 49, '0' means 48, if we minus the 48 from 49 then result will be one. In this way compiler will go the first index of array (ndig) and increment the value of that index by one.


All times are GMT +5.5. The time now is 11:57.