By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,897 Members | 1,955 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,897 IT Pros & Developers. It's quick & easy.

max value in an array.

P: 14
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.  
Feb 19 '07 #1
Share this Question
Share on Google+
10 Replies


Expert 100+
P: 1,510
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
Feb 19 '07 #2

P: 14
shit. thanks for that.

funny how something so small, can drive you nuts for so long.
Feb 19 '07 #3

P: 14
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.
Feb 19 '07 #4

Expert 100+
P: 1,510
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.
Feb 19 '07 #5

P: 14
well i initialised it to element 0 in the array,
but i still started the array from 0. does that matter?
Feb 19 '07 #6

P: 14
if i try negative numbers it causes a segmentation fault.
Feb 19 '07 #7

Expert 100+
P: 1,510
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
Feb 19 '07 #8

Expert 100+
P: 1,510
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
Feb 19 '07 #9

P: 14
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.  
Feb 19 '07 #10

Expert 100+
P: 1,510
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
Feb 19 '07 #11

Post your reply

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