Hey guys little prob

M

MARQUITOS51

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;
}
 
D

David Resnick

MARQUITOS51 said:
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
 
M

mazsx

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
 
K

Keith Thompson

MARQUITOS51 said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top