Need help

Discussion in 'Programmer's Corner' started by system0, Mar 21, 2009.

  1. system0

    Thread Starter New Member

    Mar 21, 2009
    3
    0
    i have this project in c programming language

    1.I have to input a float number which after calculating returns me a 32-bit
    note stored in a 32 bit registry .

    it has been put to my attention that the --for (int i=0;i>=1;i--) -- part of the code is not being caried out , so can anyone help me fix this problem
    Code:

    #include<stdio.h>

    int main(){
    char binbroj[32];
    int predznak = 1;
    int pot=0;
    float realniBroj,frac;
    int E;

    printf("Unesi realni broj");
    scanf("%f",&realniBroj);
    if (realniBroj < 0)
    {
    predznak = -1;
    frac = realniBroj * predznak;
    binbroj[0] ='1';
    }
    if (frac < 1 )
    {
    while (frac < 1)
    {
    frac *= 2.0;
    pot--;
    }
    }
    else {
    while (frac > 2)
    {
    frac /= 2.0;
    pot++;
    }
    }
    E = pot * 127 ;
    for (int i=0;i>=1;i--)
    {

    if (E % 2 == 0)
    binbroj = '0';
    else
    binbroj ='1';
    E /= 2;
    }
    frac -= 1;
    for (i=9;i<=31;i++){
    frac *= 2.0;
    if (frac >=1) {
    binbroj = '1';
    frac -= 1.0;

    } else binbroj ='0';
    }


    printf("float number %f in registry has a binary note \n",realniBroj);

    for(i=0;i<32;i++)
    {
    if(i==1) printf (" ");
    if(i==9) printf (" ");
    printf("%c",binbroj);
    }
    }
     
  2. SgtWookie

    Expert

    Jul 17, 2007
    22,182
    1,728
    You need to declare what kind of variable "i" is. Also, you did not initialize your float variables. While most compilers will zero the contents for you, it is a good practice to initialize them yourself.

    I put a couple of notes in your code, along with indenting it.
    Code ( (Unknown Language)):
    1.  
    2. #include<stdio.h>
    3.  
    4. int main(){
    5.     char binbroj[32];
    6.     int predznak = 1;
    7.     int pot=0;
    8.     float realniBroj=0.0,frac=0.0;
    9.     int E;
    10.  
    11.     printf("Unesi realni broj");
    12.     scanf("%f",&realniBroj);
    13.     if (realniBroj < 0)
    14.     {
    15.         predznak = -1;
    16.         frac = realniBroj * predznak;
    17.         binbroj[0] ='1';
    18.     }
    19.     if (frac < 1 )
    20.     {
    21.         while (frac < 1)    // If frac starts off being zero or negative, this will loop infinitely.
    22.         {
    23.             frac *= 2.0;
    24.             pot--;
    25.         }
    26.     }
    27.     else {
    28.         while (frac > 2)
    29.         {
    30.             frac /= 2.0;
    31.             pot++;
    32.         }
    33.     }
    34.     E = pot * 127 ;
    35.     for (int i=0;i>=1;i--)  // "i" has not been declared.  Since i is initialized to 0, i>=1 will never be satisfied.
    36.     {
    37.  
    38.         if (E % 2 == 0)
    39.         binbroj[i] = '0';
    40.         else
    41.         binbroj[i] ='1';
    42.         E /= 2;
    43.     }
    44.     frac -= 1;
    45.     for (i=9;i<=31;i++){
    46.         frac *= 2.0;
    47.         if (frac >=1) {
    48.             binbroj[i] = '1';
    49.             frac -= 1.0;
    50.  
    51.         } else binbroj[i] ='0';
    52.     }
    53.  
    54.  
    55.     printf("float number %f in registry has a binary note \n",realniBroj);
    56.  
    57.     for(i=0;i<32;i++)
    58.     {
    59.         if(i==1) printf (" ");
    60.         if(i==9) printf (" ");
    61.         printf("%c",binbroj[i]);
    62.     }
    63. }
    64. [/i][/i][/i][/i][/i]
     
  3. system0

    Thread Starter New Member

    Mar 21, 2009
    3
    0
    Can someone help me make the code work.
    Im a begginner ...
     
  4. DumboFixer

    Active Member

    Feb 10, 2009
    219
    34
    As SgtWookie pointed out in his comments, there is a problem with the line

    "for (int i=0;i>=1;i--)".

    I has not been declared.

    With i initialised to 0, then condition i >= 0 will not be satisfied. also did you mean i-- ?
     
  5. SgtWookie

    Expert

    Jul 17, 2007
    22,182
    1,728
    I'm not sure what you're attempting to accomplish with this program.

    The prompts and variable names are not in English; therefore do not make much sense to me.

    This: for (int i=0;i>=1;i--)
    Why didn't you declare "int i;" at the start of the program?
    It should look more like:
    for (i=0;i<=1;i++)

    Near the end of the listing, you have this: printf (" ");
    That will output a space, keeping the cursor on the same line.
    If you meant to advance to a new line, do this instead: printf ("\n");
     
    Last edited: Mar 23, 2009
  6. Mark44

    Well-Known Member

    Nov 26, 2007
    626
    1
    Elaborating on what Wookie and others have said...

    In your first post, you said
    The body of this for loop will never execute. The three expressions inside the parentheses and separated by semicolons are (1) the initialization expresssion, (2) the test expression, and (3) loop counter increment or decrement. Things work this way. The initialization expression is evaluated once, before anything else happens. The test expression in the middle is evaluated each time before the body of the loop is executed. If the test expression is true, the loop body executes. If the test expression is false, the loop body does not execute. After the loop body executes or doesn't execute, the increment/decrement part is evaluated.

    In your loop, a local variable i is created on the stack, and is initialized to 0. The test expression evaluates i >= 1, and finds that it is false, so the body of the loop is not executed.

    In the for loop you asked about ( for (int i = 0; i >= 1; i--) ), the loop control variable's scope is this loop. The other for loops you have use undeclared variables. Although they also use the name i, there is no connection between them and the loop control variable in this loop. You should either declare them in each loop (that is, for (i = 0; ...)) or declare i once at the beginning of your code, near the top of the main function.

    Hope that helps.
     
Loading...