max value in an array. | Newbie | | Join Date: Feb 2007
Posts: 14
| |
i cannot get my max value code to work.
all it does is prints out the last value entered, not the max value. -
for (count=0;count<maxsize;count++)
-
{
-
if ( values[count]>max);
-
{
-
max=values[count];
-
}
-
}
-
fprintf(output,"the max value is: %f\n",max);
-
| | Expert | | Join Date: Nov 2006 Location: UK
Posts: 1,320
| | | 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. -
for (count=0;count<maxsize;count++)
-
{
-
if ( values[count]>max);
-
{
-
max=values[count];
-
}
-
}
-
fprintf(output,"the max value is: %f\n",max);
-
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
| | | 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
| | | 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
| | | 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
| | | 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
| | | 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
| | | 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
| | | 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
| | | 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 -
#include <stdio.h>
-
#include <stdlib.h>
-
#include <string.h>
-
-
int main(int argc, char ** argv)
-
{
-
// Mainline Variable Declarations
-
FILE * output = stdout;
-
FILE * input = stdin;
-
-
int count;
-
int maxsize=1000;
-
-
float total;
-
float average;
-
float max;
-
float min;
-
float values[maxsize];
-
-
max=values[0];
-
min=values[0];
-
total=values[0];
-
-
fprintf(output,"please enter the number of values that will be given: "); fflush(output);
-
fscanf(input,"%d",&maxsize);
-
-
if (maxsize<=1000 || maxsize>0)
-
{
-
for (count=0;count<maxsize;count++)
-
{
-
fprintf(output,"please enter a value: "); fflush(output);
-
fscanf(input,"%f",&values[count]);
-
}
-
}
-
else
-
{
-
fprintf(output,"please enter a value greater then 0 or 1000 or less\n");
-
}
-
-
for (count=0; count<=argc-1;count++)
-
{
-
if (strcmp(argv[count],"-help")==0)
-
{
-
fprintf(output,"help\n");
-
return 0;
-
}
-
-
if (strcmp(argv[count],"-max")==0)
-
{
-
for (count=0;count<maxsize;count++)
-
{
-
if ( values[count]>max)
-
{
-
max=values[count];
-
}
-
}
-
fprintf(output,"the max value is: %f\n",max);
-
}
-
-
...
-
| | Expert | | Join Date: Nov 2006 Location: UK
Posts: 1,320
| | | 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 -
max=values[0];
-
min=values[0];
-
at the moment you initialse to element 0 before you have read any data
your test -
if (maxsize<=1000 || maxsize>0)
-
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
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,449 network members.
|