By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,171 Members | 1,004 Online
Bytes IT Community
+ Ask a Question
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
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. int  main(int argc, char ** argv)
  6. {
  7. // Mainline Variable Declarations
  8. FILE * output = stdout;
  9. FILE * input = stdin;
  10.  
  11. int count;
  12. int maxsize;
  13.  
  14. float valueX;
  15. float total;
  16. float average;
  17. float max;
  18. float min;
  19. float maxX;
  20. float maxA;
  21.  
  22.  fprintf(output,"please enter the number of values that will be given: "); fflush(output);
  23.  fscanf(input,"%d",&maxsize);
  24.  
  25.  float values[maxsize];
  26.  
  27.  
  28.  if (maxsize<=1000 || maxsize>0)
  29.  {
  30.   for (count=0;count<maxsize;count++)
  31.   {
  32.    fprintf(output,"please enter a value: "); fflush(output);
  33.    fscanf(input,"%f",&values[count]);
  34.   }
  35.  }
  36.  else
  37.  {
  38.   fprintf(output,"please enter a value greater then 0 or 1000 or less\n");
  39.  }
  40.  
  41. for (count=0; count<=argc-1;count++)
  42. {
  43.  if (strcmp(argv[count],"-help")==0)
  44.  {
  45.   fprintf(output,"help\n");
  46.   return 0;
  47.  }
  48.  
  49.  if (strcmp(argv[count],"-max")==0)
  50.  {
  51.   max=values[0];
  52.   for (count=0;count<maxsize;count++)
  53.   {
  54.    if ( values[count]>max)
  55.    {
  56.     max=values[count];
  57.    }
  58.   }
  59.   fprintf(output,"the max value is: %f\n",max);
  60.  }
  61.  
  62.  if (strcmp(argv[count],"-min")==0)
  63.  {
  64.   min=values[0];
  65.   for (count=0;count<maxsize;count++)
  66.   {
  67.    if (values[count]<min)
  68.    {
  69.     min=values[count];
  70.    }
  71.   }
  72.   fprintf(output,"the min value is: %f\n",min);
  73.  }
  74.  if (strcmp(argv[count],"-average")==0)
  75.  {
  76.   total=values[0];
  77.   for (count=0;count<maxsize;count++)
  78.   {
  79.    total+=values[count];
  80.   }
  81.   average=total/2;
  82.  
  83.   fprintf(output,"the average for these values is: %f\n",average);
  84.  }
  85.  if (strcmp(argv[count],"-gta")==0)
  86.  {
  87.   for (count=0;count<maxsize;count++)
  88.   {
  89.     total=total+values[count];
  90.   }
  91.   average=total/2;
  92.  
  93.   maxA=average
  94.   for (count=average; count<maxsize;count++)
  95.   {
  96.    if (values[count]>maxA)
  97.    {
  98.     maxA=values[count];
  99.    }
  100.     fprintf(output,"values greater then the average are: %f\n", maxA);
  101.   }
  102.  }
  103.  
  104.  if (strcmp(argv[count],"-gt")==0)
  105.  {
  106.   if (argc<4)
  107.   {
  108.   fprintf(output,"please enter a  value\n");
  109.   }
  110.   else
  111.   {
  112.    valueX = atof(argv[count+1]);//takes value off the command line and assigns to valuex
  113.  
  114.    maxX=valueX;
  115.    for (count=valueX; count<maxsize;count++)
  116.    {
  117.     if (values[count]>maxX)
  118.     {
  119.      maxX=values[count];
  120.      fprintf(output,"values which are greater: %f\n",maxX);
  121.     }
  122.    }
  123.  }
  124. }
  125. }
  126. }
  127.  
Feb 19 '07 #1
Share this Question
Share on Google+
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
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. int  main(int argc, char ** argv)
  6. {
  7. // Mainline Variable Declarations
  8. FILE * output = stdout;
  9. FILE * input = stdin;
  10.  
  11. int count;
  12. int maxsize;
  13.  
  14. float valueX;
  15. float total;
  16. float average;
  17. float max;
  18. float min;
  19. float maxX;
  20. float maxA;
  21.  
  22.  fprintf(output,"please enter the number of values that will be given: "); fflush(output);
  23.  fscanf(input,"%d",&maxsize);
  24.  
  25.  float values[maxsize];
  26.  
  27.  
  28.  if (maxsize<=1000 || maxsize>0)
  29.  {
  30.   for (count=0;count<maxsize;count++)
  31.   {
  32.    fprintf(output,"please enter a value: "); fflush(output);
  33.    fscanf(input,"%f",&values[count]);
  34.   }
  35.  }
  36.  else
  37.  {
  38.   fprintf(output,"please enter a value greater then 0 or 1000 or less\n");
  39.  }
  40.  
  41. for (count=0; count<=argc-1;count++)
  42. {
  43.  if (strcmp(argv[count],"-help")==0)
  44.  {
  45.   fprintf(output,"help\n");
  46.   return 0;
  47.  }
  48.  
  49.  if (strcmp(argv[count],"-max")==0)
  50.  {
  51.   max=values[0];
  52.   for (count=0;count<maxsize;count++)
  53.   {
  54.    if ( values[count]>max)
  55.    {
  56.     max=values[count];
  57.    }
  58.   }
  59.   fprintf(output,"the max value is: %f\n",max);
  60.  }
  61.  
  62.  if (strcmp(argv[count],"-min")==0)
  63.  {
  64.   min=values[0];
  65.   for (count=0;count<maxsize;count++)
  66.   {
  67.    if (values[count]<min)
  68.    {
  69.     min=values[count];
  70.    }
  71.   }
  72.   fprintf(output,"the min value is: %f\n",min);
  73.  }
  74.  if (strcmp(argv[count],"-average")==0)
  75.  {
  76.   total=values[0];
  77.   for (count=0;count<maxsize;count++)
  78.   {
  79.    total+=values[count];
  80.   }
  81.   average=total/2;
  82.  
  83.   fprintf(output,"the average for these values is: %f\n",average);
  84.  }
  85.  if (strcmp(argv[count],"-gta")==0)
  86.  {
  87.   for (count=0;count<maxsize;count++)
  88.   {
  89.     total=total+values[count];
  90.   }
  91.   average=total/2;
  92.  
  93.   maxA=average
  94.   for (count=average; count<maxsize;count++)
  95.   {
  96.    if (values[count]>maxA)
  97.    {
  98.     maxA=values[count];
  99.    }
  100.     fprintf(output,"values greater then the average are: %f\n", maxA);
  101.   }
  102.  }
  103.  
  104.  if (strcmp(argv[count],"-gt")==0)
  105.  {
  106.   if (argc<4)
  107.   {
  108.   fprintf(output,"please enter a  value\n");
  109.   }
  110.   else
  111.   {
  112.    valueX = atof(argv[count+1]);//takes value off the command line and assigns to valuex
  113.  
  114.    maxX=valueX;
  115.    for (count=valueX; count<maxsize;count++)
  116.    {
  117.     if (values[count]>maxX)
  118.     {
  119.      maxX=values[count];
  120.      fprintf(output,"values which are greater: %f\n",maxX);
  121.     }
  122.    }
  123.  }
  124. }
  125. }
  126. }
  127.  

hi, i guess ur requirement will work with this code...

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. int  main(int argc, char ** argv)
  6. {
  7. // Mainline Variable Declarations
  8. FILE * output = stdout;
  9. FILE * input = stdin;
  10.  
  11. int count;
  12. int maxsize;
  13.  
  14. float valueX;
  15. float total;
  16. float average;
  17. float max;
  18. float min;
  19. float maxX;
  20. float maxA;
  21.  
  22.  fprintf(output,"please enter the number of values that will be given: "); fflush(output);
  23.  fscanf(input,"%d",&maxsize);
  24.  
  25.  float values[maxsize];
  26.  
  27.  
  28.  if (maxsize>0 && maxsize<=1000)
  29.  {
  30.   for (count=0;count<maxsize;count++)
  31.   {
  32.    fprintf(output,"please enter a value: "); fflush(output);
  33.    fscanf(input,"%f",&values[count]);
  34.   }
  35.  }
  36.  else
  37.  {
  38.   fprintf(output,"please enter a value greater then 0 or 1000 or less\n");
  39.  }
  40.  
  41. for (count=0; count<=argc-1;count++)
  42. {
  43.  if (strcmp(argv[count],"-help")==0)
  44.  {
  45.   fprintf(output,"help\n");
  46.   return 0;
  47.  }
  48.  
  49.  if (strcmp(argv[count],"-max")==0)
  50.  {
  51.   max=values[0];
  52.   for (count=0;count<maxsize;count++)
  53.   {
  54.    if ( values[count]>max)
  55.    {
  56.     max=values[count];
  57.    }
  58.   }
  59.   fprintf(output,"the max value is: %f\n",max);
  60.  }
  61.  
  62.  if (strcmp(argv[count],"-min")==0)
  63.  {
  64.   min=values[0];
  65.   for (count=0;count<maxsize;count++)
  66.   {
  67.    if (values[count]<min)
  68.    {
  69.     min=values[count];
  70.    }
  71.   }
  72.   fprintf(output,"the min value is: %f\n",min);
  73.  }
  74.  if (strcmp(argv[count],"-average")==0)
  75.  {
  76.   total=values[0];
  77.   for (count=0;count<maxsize;count++)
  78.   {
  79.    total+=values[count];
  80.   }
  81.   average=total/maxsize;
  82.  
  83.   fprintf(output,"the average for these values is: %f\n",average);
  84.  }
  85.  if (strcmp(argv[count],"-gta")==0)
  86.  {
  87.   maxA=average;
  88.   for (count=0; count<maxsize;count++)
  89.   {
  90.    if (values[count]>maxA)
  91.    {
  92.         fprintf(output,"values greater then the average are: %f\n", values[count]);
  93.  
  94.    }
  95.   }
  96.  }
  97.  
  98.  if (strcmp(argv[count],"-gt")==0)
  99.  {
  100.   if (argc<4)
  101.   {
  102.   fprintf(output,"please enter a  value\n");
  103.   }
  104.   else
  105.   {
  106.    valueX = atof(argv[count+1]);//takes value off the command line and assigns to valuex
  107.  
  108.    maxX=valueX;
  109.    for (count=0; count<maxsize;count++)
  110.    {
  111.     if (values[count]>maxX)
  112.     {
  113.         fprintf(output,"values which are greater: %f\n",values[count]);
  114.     }
  115.    }
  116.  }
  117. }
  118. }
  119. }
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

Ganon11
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

Post your reply

Sign in to post your reply or Sign up for a free account.