Hi,
Anyone here can advise me correction of my c programming?
It doesn't seem to be working.
#include<stdio.h>
float x[4] = {1.2,2.4,3.6,4.8};
float f[4] = {0.1,0.2,0.3,0.4};
float xave = 0.0;
float ftotal = 0.0;
main()
{
int i;
for (i=0; 1<4;i++) ftotal + = f;
if(ftotal!=1.0)
{
printf("error\n");
exit();
}
for(i=0;1<4;i++) xave+=f *x;
printf("\nThe weights are %f"&f);
printf("\nThe average is %f\n",xave);
}
What should i add on so that user can enter the four weights from the keyboard. The program should print an error message is the weights are out of range?
shermaine
0
Light Poster
Recommended Answers
Jump to PostHi,
What should i add on so that user can enter the four weights from the keyboard. The program should print an error message is the weights are out of range?Try something like:
int counter = 1 ; while (counter <= 4 ) { printf ("Enter …
Jump to PostTry something like:
int counter = 1 ; while (counter <= 4 ) { printf ("Enter the weight %d: ", counter) ; scanf ("%d", &my_weight [counter - 1] ) ; if ( some_condition_of_validity) { counter ++ ; } else { printf ("Incorrect choice!!!") ; } }
This should keep …
Jump to PostOH sorry Mr. Vegaseat my bad, thanks for pointing out the flaw to me. The OP required the user to input floating point values so i actually shouldnt have taken a decimal input in the first place.
Well here is a near foolproof Implementation of the input accepting phase …
Jump to PostNow that is sweet, except I wouldn't use exit (1), but would rather stay in the input loop.
Jump to PostNow that is sweet, except I wouldn't use exit (1), but would rather stay in the input loop.
Thanks for the appreciation.
Hmm i guess you are right, no use being too harsh :)
But that just involves adjusting the "if" stmt a bit and voila one would get what …
All 22 Replies
shermaine
0
Light Poster
~s.o.s~
2,560
Failure as a human
Team Colleague
Featured Poster
vegaseat
1,735
DaniWeb's Hypocrite
Team Colleague
~s.o.s~
2,560
Failure as a human
Team Colleague
Featured Poster
vegaseat
1,735
DaniWeb's Hypocrite
Team Colleague
~s.o.s~
2,560
Failure as a human
Team Colleague
Featured Poster
shermaine
0
Light Poster
shermaine
0
Light Poster
shermaine
0
Light Poster
andor
25
Posting Whiz in Training
bumsfeld
413
Nearly a Posting Virtuoso
shermaine
0
Light Poster
shermaine
0
Light Poster
shermaine
0
Light Poster
shermaine
0
Light Poster
shermaine
0
Light Poster
~s.o.s~
2,560
Failure as a human
Team Colleague
Featured Poster
shermaine
0
Light Poster
WolfPack
491
Posting Virtuoso
Team Colleague
shermaine
0
Light Poster
~s.o.s~
2,560
Failure as a human
Team Colleague
Featured Poster
vegaseat
1,735
DaniWeb's Hypocrite
Team Colleague
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.