Hi,
I got some data from this table. Can it be use using (for loop) ?

http://i.imgur.com/lq7WtWA.png

Recently, I used (if else) function, but it seem too longer.

I am new to C progrmming. Here the code (if else) funtion that I done:

                if (0 < result < 409  ) // 0 to 408
                {
                    volt = 0 ;
                }
                else if (409 < result < 818 ) //408 to 817
                {
                    volt = 1 ;
                }
                else if (818 < result < 1227 ) //818 to 1226
                {
                    volt = 2 ;
                }
                else if (1227 < result < 1636 ) //1227 to 1635
                {
                    volt = 3 ;
                }
                else if (1636 < result < 2045 ) // 1636 to 2044
                {
                    volt = 4 ;
                }
                else if (2045 < result < 2454 ) // 2045 to 2453
                {
                    volt = 5 ;
                }
                else if (2454 < result < 2863 ) //2454 to 2862
                {
                    volt = 6 ;
                }
                else if (3271 < result < 2863 ) //2863 to 3271
                {
                    volt = 7 ;
                }
                else if (3860 < result < 3272 ) //3272 to 3860
                {
                    volt = 8 ;
                }
                else if (4094 < result < 3861 ) //3861 to 4094
                {
                    volt = 9 ;
                }

                else if (result >= 4095 ) //4095++
                {
                    volt = 10 ;
                }

Thanks.
Regrads,
nazif

You could do it this way:

if ( result < 409 ) // 0 to 408
{
    volt = 0 ;
}
else if ( result < 818 ) //408 to 817
{
    volt = 1 ;
}
etc.....

You dont need last if else. instead just use else

...
...
else //4095++
{
    volt = 10 ;
}

You also need to define result variable as a unsigned.

first=0;
second=409;

if(3861 < result < 4094)
    volt=9;
elsif(result>=4095)
    volt=10;
else

    for(i=1;i<9;i++)
        if(first<result<second)
            volt=i--;
            break;
        else
            fisrt=second;
            second=first*i+1;

@Rakwl I dont think this code will work for him, you dont consider the other value btw 410 to 3860..... to me, i dont think this code is welcome
if the result is 2045... it is greater thank your first and second value, so the value of volt will be Wrong.

Edited 1 Year Ago by jnneson

This article has been dead for over six months. Start a new discussion instead.