455,433 Members | 1,844 Online Need help? Post your question and get tips & solutions from a community of 455,433 IT Pros & Developers. It's quick & easy.

# Hey guys little prob

 P: n/a 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 int main() { float n={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; } Nov 15 '05 #1
3 Replies

 P: n/a 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 int main() { float n={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) Just in case in the future the sum doesn't make it to 100.0... -David Nov 15 '05 #2

 P: n/a > I want to know why the program is printing the numbers twice on the screen. Strange question... #include int main() { float n={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 Nov 15 '05 #3

 P: n/a "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 int main() { float n={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 the benefit of the reader. Here's your program again, with the layout corrected and nothing else changed. #include int main() { float n={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) ks***@mib.org San Diego Supercomputer Center <*> We must do something. This is something. Therefore, we must do this. Nov 15 '05 #4

### This discussion thread is closed

Replies have been disabled for this discussion. 