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?

Recommended Answers

All 22 Replies

attaced is the question

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.

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.

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

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

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.

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.

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 = atof(buf);
}
{
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);
}

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 = atof(buf);0016 }0017 {0018 int i;0019 for (i=0; 1<4;i++) ftotal + = f; 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]

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 = atof(buf);0016 }0017 {0018 int i;0019 for (i=0; 1<4;i++) ftotal + = f; 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]

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

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!

Hi,

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

Have done some amending here:

#include<stdio.h>#include<stdlib.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(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 = atof(buf); } { 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); }

Hi,

Have done some amending here:

#include<stdio.h>
#include<stdlib.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(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 = atof(buf);
}
{
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);
}
}

error:

0001 #include<stdio.h>0002 #include<stdlib.h>0003 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 = atof(buf);0017 }0018 {0019 int i;0020 for (i=0; 1<4; i++) ftotal += f;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*x;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

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

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.

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.

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.

Hi SOS,

Thanks alot for your help.
But can i have one last question?
What does this actually means and doing?
Please advise. Thanks.

printf ("Enter weight #%d ", i+1);
fgets (buf, BUFSIZ, stdin);
if ( buf [strlen(buf) - 1] == '\n' )
buf [strlen(buf) - 1] = '\0' ;
weights = atof(buf);
}

Before answering your question i for the last time request you to post the code in the way which i have posted, neat , clean, indented and make it stand out using code tags. Last and final warning otherwise you would lose the friend who is trying to help you out.

printf ("Enter weight #%d ", i+1);
// ask the user to input in the weight

fgets (buf, BUFSIZ, stdin);

// this is a function which accepts the input given by the user and puts
// it in a character array. The variable "buffer" is declared and defined
// an array of BUFSIZ or "512" characters (here BUFSIZ is a constant
// built inside the compiler )

// stdin : since i am accepting the input from standard input stream
// or device which is the keyboard in this case. You can check the
// prototype of this function at www.cppreference.com

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

// here in these two stmts i remove the stray newline character which
// also entered the char arrray along with the normal input since you
// pressed the RETURN or the ENTER key. so if the last element of the
// char array is the newline character, i replace it with a null terminator
// (c style strings are character arrays with the '\0' at the end.
// This is done so that there are no problems when i use the buffer with
// funcitons which accept C style strings as input.

weights = atof(buf);

// "atof" is the function which accpets the C style string ( '\0' at the
// end of char array ) and returns the float present in the string if any
// if the first char of the array is a non digit (anything other than a
// digit , it returns 0.
// For eg. if char array has "123.123" ( here it is a string not value)
// it will put in the floating point array 123.123 (here it is float value)

Hope it helped, and for function references do visit www.cppreference.com which has a list of all the standard functions which will guarantee portability to the code which you write using them.

Bye and best of luck.

I assume your assignment is to enter 4 weights and calculate the total weight and the average weight. For some odd reason you have found code to calculate weighted averages and threw that in with some of your own work. Sit down and write some pseudocode and you will find that your assignment is rather simple.

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.