954,496 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Regarding previous programming q 2

Hi,
Anyone here can advise me correction of my c programming?
It doesn't seem to be working.
#include
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?

shermaine
Light Poster
33 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0
 

attaced is the question

Attachments q1.JPG 122.65KB
shermaine
Light Poster
33 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0
 
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:

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 on asking the user the choice until he enters a correct number.

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

Try 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 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.

vegaseat
DaniWeb's Hypocrite
Moderator
5,987 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 

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.

int main (void)
{
    int counter = 1 ;
    float  my_weight = 0;
    char buffer [BUFSIZ] = {'\0'} ;

    while (counter <= 4 )
    {
       printf ("Enter the weight %d: ", counter) ;
       fgets (buffer, BUFSIZ, stdin) ;

        if ( ! isdigit (buffer [0] ) )
        {
            printf ("Illegal input") ;
            exit (1) ;
        }

       if ( buffer [strlen (buffer) - 1] == '\n' )
         buffer [strlen (buffer) - 1] = '\0' ;

         my_weight = atof (buffer) ;

       if ( my_weight > 0 && my_weight < 10)
       {
          counter ++ ;
        }
       else
       {
          printf ("Incorrect choice!!!\n") ;
        }
    }
    return 0 ;
}
~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

Now that is sweet, except I wouldn't use exit (1), but would rather stay in the input loop.

vegaseat
DaniWeb's Hypocrite
Moderator
5,987 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 
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.

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

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.

shermaine
Light Poster
33 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0
 

Hi,

Can this be done?
#include
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 #include0002 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 `=' [IMG]http://zanovar.csci.unt.edu/~ues/cppErrors/errorInfo.gif[/IMG] 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
[IMG]http://zanovar.csci.unt.edu/~ues/UES4/images/redLLC.gif[/IMG][IMG]http://zanovar.csci.unt.edu/~ues/UES4/images/redLRC.gif[/IMG]
[IMG]http://zanovar.csci.unt.edu/~ues/UES4/images/redULC.gif[/IMG] Syntax Errors found in /usr/include/stdlib.h:[IMG]http://zanovar.csci.unt.edu/~ues/UES4/images/redURC.gif[/IMG]

shermaine
Light Poster
33 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0
 

0001 #include0002 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 `=' [IMG]http://zanovar.csci.unt.edu/~ues/cppErrors/errorInfo.gif[/IMG] 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
[IMG]http://zanovar.csci.unt.edu/~ues/UES4/images/redLLC.gif[/IMG][IMG]http://zanovar.csci.unt.edu/~ues/UES4/images/redLRC.gif[/IMG]
[IMG]http://zanovar.csci.unt.edu/~ues/UES4/images/redULC.gif[/IMG] Syntax Errors found in /usr/include/stdlib.h:[IMG]http://zanovar.csci.unt.edu/~ues/UES4/images/redURC.gif[/IMG]

shermaine
Light Poster
33 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0
 

Dude just post your error messages. I can't follow U

andor
Posting Whiz in Training
276 posts since Jun 2005
Reputation Points: 251
Solved Threads: 29
 

Looks like you have somewhat incomplete code:

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

int 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);
  }
  
  //???????????? missing something here!!!
  {
    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);



  // blahblah...
  return 0;
}


There are quite a number of obvious errors too!

bumsfeld
Nearly a Posting Virtuoso
1,445 posts since Jul 2005
Reputation Points: 404
Solved Threads: 184
 

Hi,

Can kindly advise what's wrong and what should i input into the program to make it complete? Please advise.

shermaine
Light Poster
33 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0
 

Have done some amending here:

#include#includefloat 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;int main(void){ 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[i]*x[i]; printf("\nThe weights are %f",f); printf("\nThe average is %f\n",xave); }

shermaine
Light Poster
33 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0
 

Hi,

Have done some amending here:

#include
#include
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;
int main(void)
{
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[i]*x[i];
printf("\nThe weights are %f",f);
printf("\nThe average is %f\n",xave);
}
}

shermaine
Light Poster
33 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0
 

error:

0001 #include0002 #include0003 float x[4] = {1.2,2.4,3.6,4.8};0004 float f[4] = {0.1,0.2,0.3,0.4};0005 float xave = 0.0;0006 float ftotal = 0.0;0007 int main(void)0008 {0009 int i;0010 float weights[4] = {0};0011 char buf[80];0012 for(i = 0; i < 4; ++i)0013 {0014 printf("Enter weight #%d", i+1);0015 fgets(buf,sizeof(buf),stdin);0016 weights[i] = atof(buf);0017 }0018 {0019 int i;0020 for (i=0; 1<4; i++) ftotal += f[i];0021 if(ftotal!=1.0)0022 {0023 printf("error\n");0024 exit(); at this point in file0025 }0026 for(i=0;1<4;i++) xave+=f[i]*x[i];0027 printf("\nThe weights are %f",f);0028 printf("\nThe average is %f\n",xave);0029 }0030 parse error at end of input confused by earlier errors, bailing out

shermaine
Light Poster
33 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0
 

Please advise what should i change or input inorder for the program to work

shermaine
Light Poster
33 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0
 

I didnt change the logic of the program but just made the syntax errors which bugged you to go away. I would very much recommend you to format your code properly otherwise i wont be much of a help in the near future coz its really difficult to make out the problem with the code formatted in a bad way.

Also in your code in place of "i" variable you have written the number "1" which caused your program to crash. If you are coding in Notepad i would very much recommend you to switch to a syntax highlighter and indenter IDE which would make your as well as our task easy.

int main(void)
{
    double x[4] = {1.2,2.4,3.6,4.8};
    double f[4] = {0.1,0.2,0.3,0.4};
    double xave = 0.0;
    double ftotal = 0.0;
    int i;
    double weights[4] = {0};
    char buf[BUFSIZ];
    for(i = 0; i < 4; ++i)
    {
        printf ("Enter weight #%d ", i+1);
        fgets (buf, BUFSIZ, stdin);
        if ( buf [strlen(buf) - 1] == '\n' )
            buf [strlen(buf) - 1] = '\0' ;
        weights[i] = atof(buf);
    }

    for (i=0; i <4; i++)
        ftotal += f[i];


    if  ( (int) ftotal != 1 )
    {
        printf("error\n");
        exit (1);
    }

    for(i=0; i<4;i++)
    {
        xave += f[i]*x[i] ;
        printf("\nThe weights are %f",f [i]);
    }
    printf("\nThe average is %f\n",xave);

}


Hope it helped, bye.

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

Hi,

Thanks a lot. One last question. It seem like my program doesn't work. Could you kindly advise what part should i amend or input for this program to work based on your professional skill in c programming. Please kindly advise.

shermaine
Light Poster
33 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0
 

hmmm. This is getting out of hand. Is it just me or are more and more people expecting us to do their work?
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?But is there amuch easily way of writing that part as it's seem pretty tough for me to understand.Please advise. Thanks. But seem to hv compilng errors...can anyone please help me out where should i amend correctly yo make it works? Can kindly advise what's wrong and what should i input into the program to make it complete? Please advise. Please advise what should i change or input inorder for the program to work It seem like my program doesn't work. Could you kindly advise what part should i amend or input for this program to work based on your professional skill in c programming. Please kindly advise.

All these are just asking someone else to do your work for you. All the code and error messages are next to crap. Do your own work, and ask proper questions with proper error messages next time. Use code tags also. Post crap and I delete.

WolfPack
Postaholic
Moderator
2,051 posts since Jun 2005
Reputation Points: 572
Solved Threads: 115
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You