473,398 Members | 2,380 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Hey guys little prob

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

Nov 15 '05 #1
3 1018
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

Nov 15 '05 #2
> 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

Nov 15 '05 #3
"MARQUITOS51" <cr********@gmail.com> 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
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[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 <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.
Nov 15 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: josephrthomas | last post by:
hi..i am trying to make a login page and i am using access table.. when the user enters his userid and password i want to check the password from the table.. if any user with the userID that is...
29
by: Thomas | last post by:
Hi I have an XSL stylesheet: <xsl:for-each select="TRACKS/TRACK"> <tr class="TDL"> <td width="90%"><xsl:number value="position()" format="1" /> - <xsl:value-of select="TRACKTITLE"/></td>...
5
by: David Sobey | last post by:
Hi Sorry bout this basic prob. Got a file called file.obj. tryna read the first line from it as a string and print it to the screen. getting errors: #include "stdafx.h" #include <stdio.h>...
34
by: Pmb | last post by:
I've been working on creating a Complex class for my own learning purpose (learn through doing etc.). I'm once again puzzled about something. I can't figure out how to overload the assignment...
2
by: Dishan Fernando | last post by:
Hi my prob is like this.. ----------------------------- create table ax( i int , j int ) create table ay( i int ,
15
by: Raj | last post by:
Hello all: We have a table with about 2400 cells. Our requirement is to highlight the cells in the table whose data has changed, every 5 seconds. Our script behaves relatively ok in Firefox, but...
6
by: skubik | last post by:
Hi everyone. I'm attempting to write a Javascript that will create a form within a brand-new document in a specific frame of a frameset. The problem is that I can create the form and input...
2
by: Wilhelm Kutting | last post by:
hi, i like to solve a code-prob: tidy is bitching about <...> proprietary attribute "type" <...> lacks "action" attribute how can i make this nice? this is my code
28
by: Vishal Naidu | last post by:
i m new to the C world... i ve been told by my instructors not to use goto stmt.. but no one could give me a satisfactory answer as to why it is so.. plz help me out of this dilemma, coz i use...
2
by: mnacw | last post by:
Can anybody help me to resolve this prob. i have installed Visual Studio 2005 Professional edition. I am working in VB.Net. When I tried to connect to database it is connected but when i make some...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.