473,386 Members | 1,775 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

need help with my code.

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
8 1676
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
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
If that is the case...
above code should work..
Feb 19 '07 #4
-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
well i got it.

just testing -gt now.
Feb 19 '07 #6
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
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
3,652 Expert 2GB
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

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

Similar topics

15
by: drdoubt | last post by:
using namespace std In my C++ program, even after applying , I need to use the std namespace with the scope resolution operator, like, std::cout, std::vector. This I found a little bit...
19
by: James Fortune | last post by:
I have a lot of respect for David Fenton and Allen Browne, but I don't understand why people who know how to write code to completely replace a front end do not write something that will automate...
11
by: my-wings | last post by:
I think I've painted myself into a corner, and I'm hoping someone can help me out. I have a table of books (tblBooks), which includes a field (strPubName) for Publisher Name and another field...
7
by: Timothy Shih | last post by:
Hi, I am trying to figure out how to use unmanaged code using P/Invoke. I wrote a simple function which takes in 2 buffers (one a byte buffer, one a char buffer) and copies the contents of the byte...
48
by: Chad Z. Hower aka Kudzu | last post by:
A few of you may recognize me from the recent posts I have made about Indy <http://www.indyproject.org/indy.html> Those of you coming to .net from the Delphi world know truly how unique and...
4
by: Phil | last post by:
k, here is my issue.. I have BLOB data in SQL that needs to be grabbed and made into a TIF file and placed on the client (could be in temp internet dir). The reason we need it in TIF format is...
2
by: Keith Kowalski | last post by:
I anm opening up a text file reading the lines of the file that refer to a tif image in that file, If the tif image does not exist I need it to send an email stating that the file doesn't exist...
15
by: Cheryl Langdon | last post by:
Hello everyone, This is my first attempt at getting help in this manner. Please forgive me if this is an inappropriate request. I suddenly find myself in urgent need of instruction on how to...
4
by: Quas.co.ua | last post by:
Hello all. I need your help. I need C compler to make demo of some technologie. This C compiler I need to write program which after run will be located in one segment of memory and it...
20
by: mike | last post by:
I help manage a large web site, one that has over 600 html pages... It's a reference site for ham radio folks and as an example, one page indexes over 1.8 gb of on-line PDF documents. The site...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.