Hey guys little prob

Discussion in 'C Programming' started by MARQUITOS51, Nov 14, 2005.

  1. MARQUITOS51

    MARQUITOS51 Guest

    This program is intended to read values of an array until the sum of
    those values reaches 100. Then at the element where the sum becomes
    greater thatn 100 store a -1.0 to denote the end of the array. I want
    to know why the program is printing the numbers twice on the screen.


    #include<stdio.h>

    int main()

    {
    float n[100]={10,20,30,40,50,60,70,80,90,100}, sum=0.0;
    int i;


    for(i=0; sum<=100.0; i++)
    {
    sum=sum+n;

    if (sum<=100.0)
    {printf("%.1f \n", n);}
    else
    n=-1;
    {printf("%.1f \n", n);}

    }

    return 0;
    }
     
    MARQUITOS51, Nov 14, 2005
    #1
    1. Advertising

  2. MARQUITOS51 wrote:
    > This program is intended to read values of an array until the sum of
    > those values reaches 100. Then at the element where the sum becomes
    > greater thatn 100 store a -1.0 to denote the end of the array. I want
    > to know why the program is printing the numbers twice on the screen.
    >
    >
    > #include<stdio.h>
    >
    > int main()
    >
    > {
    > float n[100]={10,20,30,40,50,60,70,80,90,100}, sum=0.0;
    > int i;
    >
    >
    > for(i=0; sum<=100.0; i++)
    > {
    > sum=sum+n;
    >
    > if (sum<=100.0)
    > {printf("%.1f \n", n);}
    > else
    > n=-1;

    The else causes only the assignment above to be executed.
    If you want multiple statements controlled by the else,
    use a block, as in
    else {
    n = -1;
    printf("%.1f \n", n);
    }

    > {printf("%.1f \n", n);}
    >
    > }
    >
    > return 0;
    > }


    A second comment: you are at risk of going off the end of the end of
    the array, perhaps you want your guard on the for loop to be:

    sum <= 100.0 && i < 100
    or
    sum <= 100.0 && i < (sizeof n/sizeof n[0])

    Just in case in the future the sum doesn't make it to 100.0...

    -David
     
    David Resnick, Nov 14, 2005
    #2
    1. Advertising

  3. MARQUITOS51

    mazsx Guest

    > I want to know why the program is printing the numbers twice on the screen.

    Strange question...

    > #include<stdio.h>
    > int main()
    > {
    > float n[100]={10,20,30,40,50,60,70,80,90,100}, sum=0.0;
    > int i;
    >
    >
    > for(i=0; sum<=100.0; i++)
    > {
    > sum=sum+n;
    >
    > if (sum<=100.0)
    > {printf("%.1f \n", n);}
    > else
    > n=-1;
    > {printf("%.1f \n", n);}

    ^^^^^^^^^^^^^^^^^^^
    Have you thought of this line?

    mazsx
     
    mazsx, Nov 14, 2005
    #3
  4. "MARQUITOS51" <> writes:
    > This program is intended to read values of an array until the sum of
    > those values reaches 100. Then at the element where the sum becomes
    > greater thatn 100 store a -1.0 to denote the end of the array. I want
    > to know why the program is printing the numbers twice on the screen.
    >
    >
    > #include<stdio.h>
    >
    > int main()
    >
    > {
    > float n[100]={10,20,30,40,50,60,70,80,90,100}, sum=0.0;
    > int i;
    >
    >
    > for(i=0; sum<=100.0; i++)
    > {
    > sum=sum+n;
    >
    > if (sum<=100.0)
    > {printf("%.1f \n", n);}
    > else
    > n=-1;
    > {printf("%.1f \n", n);}
    >
    > }
    >
    > return 0;
    > }


    This demonstrates how important it is to use consistent indentation.
    The compiler doesn't care how your code is indented; the layout is for
    the benefit of the reader.

    Here's your program again, with the layout corrected and nothing else
    changed.

    #include<stdio.h>
    int main()
    {
    float n[100]={10,20,30,40,50,60,70,80,90,100}, sum=0.0;
    int i;

    for(i=0; sum<=100.0; i++) {
    sum=sum+n;
    if (sum<=100.0) {
    printf("%.1f \n", n);
    }
    else
    n=-1;
    {
    printf("%.1f \n", n);
    }
    }
    return 0;
    }

    As you can see, the second printf is not part of the else clause; it's
    executed unconditionally after the if/else statement is done.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Nov 14, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. walala
    Replies:
    3
    Views:
    4,236
    Renaud Pacalet
    Oct 3, 2003
  2. Mike Dee
    Replies:
    0
    Views:
    373
    Mike Dee
    Nov 30, 2005
  3. ThaDoctor
    Replies:
    3
    Views:
    385
    Alan Woodland
    Sep 28, 2007
  4. Junkone

    little prob with Date.strptime

    Junkone, Feb 27, 2009, in forum: Ruby
    Replies:
    1
    Views:
    104
  5. Daniel
    Replies:
    1
    Views:
    214
    Bart van Ingen Schenau
    Jul 9, 2013
Loading...

Share This Page