Regarding previous programming q 2

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2006
Posts: 33
Reputation: shermaine is an unknown quantity at this point 
Solved Threads: 0
shermaine shermaine is offline Offline
Light Poster

Regarding previous programming q 2

 
0
  #1
Oct 2nd, 2006
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?
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 33
Reputation: shermaine is an unknown quantity at this point 
Solved Threads: 0
shermaine shermaine is offline Offline
Light Poster

Re: Regarding previous programming q 2

 
0
  #2
Oct 2nd, 2006
attaced is the question
Attached Thumbnails
q1.JPG  
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,649
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 474
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Regarding previous programming q 2

 
0
  #3
Oct 2nd, 2006
Originally Posted by shermaine View Post
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.
I don't accept change; I don't deserve to live.

Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,125
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 945
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Regarding previous programming q 2

 
1
  #4
Oct 2nd, 2006
Originally Posted by ~s.o.s~ View Post
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.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,649
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 474
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Regarding previous programming q 2

 
0
  #5
Oct 2nd, 2006
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. }
I don't accept change; I don't deserve to live.

Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,125
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 945
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Regarding previous programming q 2

 
0
  #6
Oct 2nd, 2006
Now that is sweet, except I wouldn't use exit (1), but would rather stay in the input loop.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,649
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 474
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Regarding previous programming q 2

 
0
  #7
Oct 2nd, 2006
Originally Posted by vegaseat View Post
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.
I don't accept change; I don't deserve to live.

Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 33
Reputation: shermaine is an unknown quantity at this point 
Solved Threads: 0
shermaine shermaine is offline Offline
Light Poster

Re: Regarding previous programming q 2

 
0
  #8
Oct 2nd, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 33
Reputation: shermaine is an unknown quantity at this point 
Solved Threads: 0
shermaine shermaine is offline Offline
Light Poster

Re: Regarding previous programming q 2

 
0
  #9
Oct 2nd, 2006
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:
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 33
Reputation: shermaine is an unknown quantity at this point 
Solved Threads: 0
shermaine shermaine is offline Offline
Light Poster

Re: Regarding previous programming q 2

 
0
  #10
Oct 2nd, 2006
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:
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC