Velocity Reviews > Hey guys little prob

Hey guys little prob

MARQUITOS51
Guest
Posts: n/a

 11-14-2005
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[i];

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

}

return 0;
}

David Resnick
Guest
Posts: n/a

 11-14-2005
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[i];
>
> if (sum<=100.0)
> {printf("%.1f \n", n[i]);}
> else
> n[i]=-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[i] = -1;
printf("%.1f \n", n[i]);
}

> {printf("%.1f \n", n[i]);}
>
> }
>
> 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

mazsx
Guest
Posts: n/a

 11-14-2005
> 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[i];
>
> if (sum<=100.0)
> {printf("%.1f \n", n[i]);}
> else
> n[i]=-1;
> {printf("%.1f \n", n[i]);}

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

mazsx

Keith Thompson
Guest
Posts: n/a

 11-14-2005
"MARQUITOS51" <(E-Mail Removed)> 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[i];
>
> if (sum<=100.0)
> {printf("%.1f \n", n[i]);}
> else
> n[i]=-1;
> {printf("%.1f \n", n[i]);}
>
> }
>
> 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

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[i];
if (sum<=100.0) {
printf("%.1f \n", n[i]);
}
else
n[i]=-1;
{
printf("%.1f \n", n[i]);
}
}
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.velocityreviews.com/forums/(E-Mail Removed) <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.