Connecting Tech Pros Worldwide Forums | Help | Site Map

max value in an array.

Newbie
 
Join Date: Feb 2007
Posts: 14
#1: Feb 19 '07
i cannot get my max value code to work.
all it does is prints out the last value entered, not the max value.

Expand|Select|Wrap|Line Numbers
  1. for (count=0;count<maxsize;count++)
  2.   {
  3.    if ( values[count]>max);
  4.    {
  5.     max=values[count];
  6.    }
  7.   }
  8.   fprintf(output,"the max value is: %f\n",max);
  9.  

Expert
 
Join Date: Nov 2006
Location: UK
Posts: 1,320
#2: Feb 19 '07

re: max value in an array.


Quote:

Originally Posted by wastedhello

i cannot get my max value code to work.
all it does is prints out the last value entered, not the max value.

Expand|Select|Wrap|Line Numbers
  1. for (count=0;count<maxsize;count++)
  2.   {
  3.    if ( values[count]>max);
  4.    {
  5.     max=values[count];
  6.    }
  7.   }
  8.   fprintf(output,"the max value is: %f\n",max);
  9.  

you appear to have a spare ; on the end of the if() statement which will terminate it at that point
Newbie
 
Join Date: Feb 2007
Posts: 14
#3: Feb 19 '07

re: max value in an array.


shit. thanks for that.

funny how something so small, can drive you nuts for so long.
Newbie
 
Join Date: Feb 2007
Posts: 14
#4: Feb 19 '07

re: max value in an array.


if they are negative numbers though,

it will print out zero.

and for my min value.

it will only find min of negative numbers.

why is that.
Expert
 
Join Date: Nov 2006
Location: UK
Posts: 1,320
#5: Feb 19 '07

re: max value in an array.


Quote:

Originally Posted by wastedhello

if they are negative numbers though,

it will print out zero.

and for my min value.

it will only find min of negative numbers.

why is that.

what do you initialise max to? for example if it was initialised to 0 and your numbers were negative you could would return max as 0 as you would never find an element > 0. usually one would initialise max to the value of element 0 then search the array from element 1.
Newbie
 
Join Date: Feb 2007
Posts: 14
#6: Feb 19 '07

re: max value in an array.


well i initialised it to element 0 in the array,
but i still started the array from 0. does that matter?
Newbie
 
Join Date: Feb 2007
Posts: 14
#7: Feb 19 '07

re: max value in an array.


if i try negative numbers it causes a segmentation fault.
Expert
 
Join Date: Nov 2006
Location: UK
Posts: 1,320
#8: Feb 19 '07

re: max value in an array.


Quote:

Originally Posted by wastedhello

well i initialised it to element 0 in the array,
but i still started the array from 0. does that matter?

not critical - the first itteration would compare element 0 with itself - just not efficent
Expert
 
Join Date: Nov 2006
Location: UK
Posts: 1,320
#9: Feb 19 '07

re: max value in an array.


Quote:

Originally Posted by wastedhello

if i try negative numbers it causes a segmentation fault.

very strange - are you overrunning the end of the array? e.g. is the actual array length allocated in memory less than maxsize
Newbie
 
Join Date: Feb 2007
Posts: 14
#10: Feb 19 '07

re: max value in an array.


no i dont think so.

whats strange is i only get the segmentation fault when i enter in 2 values..

and if i input a negative number it just gives back the max as 0.

the other problem is that it doesnt matter how many values i say i want to input it will still go through with it, when ive told it to only go through if there are 0 to 1000 values.

heres all my coding

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=1000;
  13.  
  14. float total;
  15. float average;
  16. float max;
  17. float min;
  18. float values[maxsize];
  19.  
  20. max=values[0];
  21. min=values[0];
  22. total=values[0];
  23.  
  24.  fprintf(output,"please enter the number of values that will be given: "); fflush(output);
  25.  fscanf(input,"%d",&maxsize);
  26.  
  27.  if (maxsize<=1000 || maxsize>0)
  28.  {
  29.   for (count=0;count<maxsize;count++)
  30.   {
  31.    fprintf(output,"please enter a value: "); fflush(output);
  32.    fscanf(input,"%f",&values[count]);
  33.   }
  34.  }
  35.  else
  36.  {
  37.   fprintf(output,"please enter a value greater then 0 or 1000 or less\n");
  38.  }
  39.  
  40. for (count=0; count<=argc-1;count++)
  41. {
  42.  if (strcmp(argv[count],"-help")==0)
  43.  {
  44.   fprintf(output,"help\n");
  45.   return 0;
  46.  }
  47.  
  48.  if (strcmp(argv[count],"-max")==0)
  49.  {
  50.   for (count=0;count<maxsize;count++)
  51.   {
  52.    if ( values[count]>max)
  53.    {
  54.     max=values[count];
  55.    }
  56.   }
  57.   fprintf(output,"the max value is: %f\n",max);
  58.  }
  59.  
  60. ...
  61.  
Expert
 
Join Date: Nov 2006
Location: UK
Posts: 1,320
#11: Feb 19 '07

re: max value in an array.


a couple of things to try - you need to initialise these values just before the for() loop to find the max and min
Expand|Select|Wrap|Line Numbers
  1. max=values[0];
  2. min=values[0];
  3.  
at the moment you initialse to element 0 before you have read any data

your test
Expand|Select|Wrap|Line Numbers
  1.  if (maxsize<=1000 || maxsize>0)
  2.  
is always true, e.g. if you enter 10 it is > 0 and less than 1000 so if you enter 2000 you program will attempt to read 2000 values
Reply