944,052 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 4202
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Oct 2nd, 2006
0

Regarding previous programming q 2

Expand Post »
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[i];
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?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
shermaine is offline Offline
33 posts
since Oct 2006
Oct 2nd, 2006
0

Re: Regarding previous programming q 2

attaced is the question
Attached Thumbnails
Click image for larger version

Name:	q1.JPG
Views:	11
Size:	122.7 KB
ID:	2387  
Reputation Points: 10
Solved Threads: 0
Light Poster
shermaine is offline Offline
33 posts
since Oct 2006
Oct 2nd, 2006
0

Re: Regarding previous programming q 2

Click to Expand / Collapse  Quote originally posted by shermaine ...
Hi,
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:

  1. int counter = 1 ;
  2. while (counter <= 4 )
  3. {
  4. printf ("Enter the weight %d: ", counter) ;
  5. scanf ("%d", &my_weight [counter - 1] ) ;
  6.  
  7. if ( some_condition_of_validity)
  8. {
  9. counter ++ ;
  10. }
  11. else
  12. {
  13. printf ("Incorrect choice!!!") ;
  14. }
  15. }

This should keep on asking the user the choice until he enters a correct number.
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006
Oct 2nd, 2006
1

Re: Regarding previous programming q 2

Click to Expand / Collapse  Quote originally posted by ~s.o.s~ ...
Try something like:

  1. int counter = 1 ;
  2. while (counter <= 4 )
  3. {
  4. printf ("Enter the weight %d: ", counter) ;
  5. scanf ("%d", &my_weight [counter - 1] ) ;
  6.  
  7. if ( some_condition_of_validity)
  8. {
  9. counter ++ ;
  10. }
  11. else
  12. {
  13. printf ("Incorrect choice!!!") ;
  14. }
  15. }

This should keep on asking the user the choice until he enters a correct number.
One mild problem from past experience, if the users enters a floating point number, this loop spins out of control. Give it a try, it's good exercise.
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Oct 2nd, 2006
0

Re: Regarding previous programming q 2

OH 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 for your program which accpets only valid floats.

  1.  
  2. int main (void)
  3. {
  4. int counter = 1 ;
  5. float my_weight = 0;
  6. char buffer [BUFSIZ] = {'\0'} ;
  7.  
  8. while (counter <= 4 )
  9. {
  10. printf ("Enter the weight %d: ", counter) ;
  11. fgets (buffer, BUFSIZ, stdin) ;
  12.  
  13. if ( ! isdigit (buffer [0] ) )
  14. {
  15. printf ("Illegal input") ;
  16. exit (1) ;
  17. }
  18.  
  19. if ( buffer [strlen (buffer) - 1] == '\n' )
  20. buffer [strlen (buffer) - 1] = '\0' ;
  21.  
  22. my_weight = atof (buffer) ;
  23.  
  24. if ( my_weight > 0 && my_weight < 10)
  25. {
  26. counter ++ ;
  27. }
  28. else
  29. {
  30. printf ("Incorrect choice!!!\n") ;
  31. }
  32. }
  33. return 0 ;
  34. }
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006
Oct 2nd, 2006
0

Re: Regarding previous programming q 2

Now that is sweet, except I wouldn't use exit (1), but would rather stay in the input loop.
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Oct 2nd, 2006
0

Re: Regarding previous programming q 2

Click to Expand / Collapse  Quote originally posted by vegaseat ...
Now 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 he wants.
Last edited by ~s.o.s~; Oct 2nd, 2006 at 2:54 pm.
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006
Oct 2nd, 2006
0

Re: Regarding previous programming q 2

Hi Guys,

Thanks so much. But is there amuch easily way of writing that part as it's seem pretty tough for me to understand.Please advise. Thanks.
Reputation Points: 10
Solved Threads: 0
Light Poster
shermaine is offline Offline
33 posts
since Oct 2006
Oct 2nd, 2006
0

Re: Regarding previous programming q 2

Hi,

Can this be done?
#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;
float weights[4] = {0};
char buf[80];
for(i = 0; i < 4; ++i)
{
printf("Enter weight #%d", i+1);
fgets(buf,sizeof(buf),stdin);
weights[i] = atof(buf);
}
{
int i;
for (i=0; 1<4;i++) ftotal + = f[i];
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);
}

But seem to hv compilng errors...can anyone please help me out where should i amend correctly yo make it works?

0001 #include<stdio.h>0002 float x[4] = {1.2,2.4,3.6,4.8};0003 float f[4] = {0.1,0.2,0.3,0.4};0004 float xave = 0.0;0005 float ftotal = 0.0;0006 main()0007 {0008 int i;0009 float weights[4] = {0};0010 char buf[80];0011 for(i = 0; i < 4; ++i)0012 {0013 printf("Enter weight #%d", i+1);0014 fgets(buf,sizeof(buf),stdin);0015 weights[i] = atof(buf);0016 }0017 {0018 int i;0019 for (i=0; 1<4;i++) ftotal + = f[i]; parse error before `=' 0020 if(ftotal!=1.0)0021 {0022 printf("error\n");0023 exit(); at this point in file0024 }0025 for(i=0;1<4;i++) xave+=f *x; invalid operands `float[4]' and `float[4]' to binary `operator *'0026 printf("\nThe weights are %f"&f); invalid operands `const char[20]' and `float[4]' to binary `operator &'0027 printf("\nThe average is %f\n",xave);0028 }0029 0030 0031 parse error at end of input confused by earlier errors, bailing out

Syntax Errors found in /usr/include/stdlib.h:
Reputation Points: 10
Solved Threads: 0
Light Poster
shermaine is offline Offline
33 posts
since Oct 2006
Oct 2nd, 2006
0

Re: Regarding previous programming q 2

0001 #include<stdio.h>0002 float x[4] = {1.2,2.4,3.6,4.8};0003 float f[4] = {0.1,0.2,0.3,0.4};0004 float xave = 0.0;0005 float ftotal = 0.0;0006 main()0007 {0008 int i;0009 float weights[4] = {0};0010 char buf[80];0011 for(i = 0; i < 4; ++i)0012 {0013 printf("Enter weight #%d", i+1);0014 fgets(buf,sizeof(buf),stdin);0015 weights[i] = atof(buf);0016 }0017 {0018 int i;0019 for (i=0; 1<4;i++) ftotal + = f[i]; parse error before `=' 0020 if(ftotal!=1.0)0021 {0022 printf("error\n");0023 exit(); at this point in file0024 }0025 for(i=0;1<4;i++) xave+=f *x; invalid operands `float[4]' and `float[4]' to binary `operator *'0026 printf("\nThe weights are %f"&f); invalid operands `const char[20]' and `float[4]' to binary `operator &'0027 printf("\nThe average is %f\n",xave);0028 }0029 0030 0031 parse error at end of input confused by earlier errors, bailing out

Syntax Errors found in /usr/include/stdlib.h:
Reputation Points: 10
Solved Threads: 0
Light Poster
shermaine is offline Offline
33 posts
since Oct 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: pthreads scheduling problem?
Next Thread in C Forum Timeline: a simple question





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC