446,171 Members | 1,004 Online
Need help? Post your question and get tips & solutions from a community of 446,171 IT Pros & Developers. It's quick & easy.

# need help with my code.

 P: 14 ok. here are a list of things i cant fix. ive been trying for way to long now. 1. if more then 1000 values are told, will still continue with program. 2. if -max, min, average etc run, and 2 values are given they will work, but will cause a random segmentation fault. (BUT only with giving two values.) 3. -average doesnt give average. 4. -gta is ment to give all the values greater then the average. but doesnt do anything 5. -gt is ment to give all the values greater then valueX. which doesnt do anything either. Expand|Select|Wrap|Line Numbers #include  #include  #include    int  main(int argc, char ** argv) { // Mainline Variable Declarations FILE * output = stdout; FILE * input = stdin;   int count; int maxsize;   float valueX; float total; float average; float max; float min; float maxX; float maxA;    fprintf(output,"please enter the number of values that will be given: "); fflush(output);  fscanf(input,"%d",&maxsize);    float values[maxsize];      if (maxsize<=1000 || maxsize>0)  {   for (count=0;countmax)    {     max=values[count];    }   }   fprintf(output,"the max value is: %f\n",max);  }    if (strcmp(argv[count],"-min")==0)  {   min=values[0];   for (count=0;countmaxA)    {     maxA=values[count];    }     fprintf(output,"values greater then the average are: %f\n", maxA);   }  }    if (strcmp(argv[count],"-gt")==0)  {   if (argc<4)   {   fprintf(output,"please enter a  value\n");   }   else   {    valueX = atof(argv[count+1]);//takes value off the command line and assigns to valuex      maxX=valueX;    for (count=valueX; countmaxX)     {      maxX=values[count];      fprintf(output,"values which are greater: %f\n",maxX);     }    }  } } } }   Feb 19 '07 #1
8 Replies

 P: 4 ok. here are a list of things i cant fix. ive been trying for way to long now. 1. if more then 1000 values are told, will still continue with program. 2. if -max, min, average etc run, and 2 values are given they will work, but will cause a random segmentation fault. (BUT only with giving two values.) 3. -average doesnt give average. 4. -gta is ment to give all the values greater then the average. but doesnt do anything 5. -gt is ment to give all the values greater then valueX. which doesnt do anything either. Expand|Select|Wrap|Line Numbers #include  #include  #include    int  main(int argc, char ** argv) { // Mainline Variable Declarations FILE * output = stdout; FILE * input = stdin;   int count; int maxsize;   float valueX; float total; float average; float max; float min; float maxX; float maxA;    fprintf(output,"please enter the number of values that will be given: "); fflush(output);  fscanf(input,"%d",&maxsize);    float values[maxsize];      if (maxsize<=1000 || maxsize>0)  {   for (count=0;countmax)    {     max=values[count];    }   }   fprintf(output,"the max value is: %f\n",max);  }    if (strcmp(argv[count],"-min")==0)  {   min=values[0];   for (count=0;countmaxA)    {     maxA=values[count];    }     fprintf(output,"values greater then the average are: %f\n", maxA);   }  }    if (strcmp(argv[count],"-gt")==0)  {   if (argc<4)   {   fprintf(output,"please enter a  value\n");   }   else   {    valueX = atof(argv[count+1]);//takes value off the command line and assigns to valuex      maxX=valueX;    for (count=valueX; countmaxX)     {      maxX=values[count];      fprintf(output,"values which are greater: %f\n",maxX);     }    }  } } } }   hi, i guess ur requirement will work with this code... Expand|Select|Wrap|Line Numbers #include  #include  #include    int  main(int argc, char ** argv) { // Mainline Variable Declarations FILE * output = stdout; FILE * input = stdin;   int count; int maxsize;   float valueX; float total; float average; float max; float min; float maxX; float maxA;    fprintf(output,"please enter the number of values that will be given: "); fflush(output);  fscanf(input,"%d",&maxsize);    float values[maxsize];      if (maxsize>0 && maxsize<=1000)  {   for (count=0;countmax)    {     max=values[count];    }   }   fprintf(output,"the max value is: %f\n",max);  }    if (strcmp(argv[count],"-min")==0)  {   min=values[0];   for (count=0;countmaxA)    {         fprintf(output,"values greater then the average are: %f\n", values[count]);      }   }  }    if (strcmp(argv[count],"-gt")==0)  {   if (argc<4)   {   fprintf(output,"please enter a  value\n");   }   else   {    valueX = atof(argv[count+1]);//takes value off the command line and assigns to valuex      maxX=valueX;    for (count=0; countmaxX)     {         fprintf(output,"values which are greater: %f\n",values[count]);     }    }  } } } } As i dont know ur requirements properly.. i cud solve some of your problems.. let me know if u get some other problems... Feb 19 '07 #2

 P: 14 it fixed a bit. lets see.. average seems to be printing out random numbers. not the average. -gta. this command is ment to find the average. then print out all the values greater then the average. -gt. this command is ment to get the user to input a value. then print all values greater then that. everything else works fine. Feb 19 '07 #3

 P: 4 If that is the case... above code should work.. Feb 19 '07 #4

 P: 14 -gta prints out the 5 values given, not the values greater then the average.. i will try.. does the code to find the average have to be typed again under -gta Feb 19 '07 #5

 P: 14 well i got it. just testing -gt now. Feb 19 '07 #6

 P: 14 that wasnt expected. with -gt. the else statement didnt work. i had to change it to an else if statement. so i put else if (argc=3). dont know if its the best way to put it. but it works. :D Feb 19 '07 #7

 P: 14 oh theres 1 thing i want to edit. is it possible. if -help is specified. i want it to give a list of help commands then stop. because at the moment it still asks for the array values. Feb 19 '07 #8

 Expert 2.5K+ P: 3,652 At this point, your program is asking for the values before it checks for the "-help" flag. If you want the program to stop before entering values if -help is typed in the command line, you will have to check for "-help" before your fscanf loop. Feb 19 '07 #9