Hi there,
Well, before I begin, I should mention that one useful thing to know
about google answers is that if you want more information about an
answer, or the answer did not quite answer your question, then you do
not have to post an entirely new question. Instead, just use the
"request clarification" button and you will be able to ask for a new
answer without having to post a whole new question.
Now for the actual question.
The question is asking for a routine that, given an array of floating
point values and the length of the array, returns the index of the
maximum value in that array (actually, it returns the lowest such
index in the event of repeated maximum values).
The code you gave had a couple of minor issues:
1. The line:
max_data = data[1];
actually sets max_data to the value of the second element of the array
data, because in C, the first (or "initial") element of an array is
indexed at zero.
2. Regarding the line,
max_index = *data[i];
I am not sure what the intention here is, but what you want is just
max_index=i;
3. The line
data[i]++
increments the value in the array data by one, which is definitely not
what you want to do.
I have also made a minor stylistic change: I have removed an
unnecessary pair of curly braces after the first for statement, and I
have removed the unnecessary parentheses around the value to be
returned.
Here is a corrected version of the function. Note that if this
function is passed an empty array, that is, if the number parameter is
0, it will return 0.
/*
* function tms_calculate_max()
* ============================
* This function returns the index in TemperatureLog[]
* of the maximum of the stored temperature readings.
* number must be greater than 0;
*/
int tms_calculate_max(float *data, int number)
{
int i; /* local variable*/
int max_index; /*max index so far*/
float max_data; /* max temperature we have so far*/
max_data = data[0]; /*set max_data to the first temperature
reading*/
max_index = 0; /* initialize max_index */
for (i=0; i < number; i++) /* for each index */
if (max_data < data[i]) /* if new largest...*/
{
max_data = data[i]; /* set max_data to new max*/
max_index=i; /* set max_index to this index */
}
return max_index;
}
To test this, I created a new file and prefaced the function above
with the following simple test code:
#include<stdlib.h>
int main(){
int i;
float test1[]= {2.3,2.4,2.5,1.0};
int number =4;
int max=tms_calculate_max(test1,number);
for (i=0;i<number;++i)
printf("Array value number: %d is: %f\n",i,test1[i]);
printf("Got max index of: %d\n",max);
}
When I compiled and ran the program, the output printed was:
Array value number: 0 is: 2.300000
Array value number: 1 is: 2.400000
Array value number: 2 is: 2.500000
Array value number: 3 is: 1.000000
Got max index of: 2
Your test procedure however will depend on what other functions you
have and your exact compilation environment.
There are a number of C tutorials on the net, and a number of
textbooks, although I think the best way to learn is just to explore
on your own. A sample tutorial
http://kdat.csc.calpoly.edu/~ltauck/THESIS/C_Tutorial.html . |
Clarification of Answer by
rbnn-ga
on
20 Sep 2002 11:06 PDT
Actually, note that the function tms_calculate_max (nor the flow chart
it implements) will work if it is passed an empty array. That is, if
the array is empty and the parameter number is 0, the behavior of the
function is undefined.
Normally we would check for this case but for simplicity of exposition
I did not; also, the original flow chart did not check for this case
either.
|
Request for Answer Clarification by
reebok-ga
on
20 Sep 2002 12:26 PDT
in addition, if the array contained some invalid readings, there are
two parts to the maximum function that needs altering, the first is
the section that initializes the running maximum value to the first
temperature in the array. The second is the section that checks if
each temperature is greater than this running maximum value.
i, the first mod above invloves adding a C construct that will test
the initial value of the running maximum value, and set it to ---999.9
if it contains an invalid temperature. Write down this additional C
construct such that it can be placed in the tms_calculate_max()
function immediately after the statement that sets the running maximum
to the first temperature value. ( there is a note saying it is not a
loop construct).
ii, Write down a modification to the for construct so that max_data
and max_index are only altered if the temperature is higher than the
running maximum and the temperature is not valid. (note, this can be
done by either extending the test condition or by incorporating
another construct to carry out the additional test.)
|
Clarification of Answer by
rbnn-ga
on
20 Sep 2002 15:11 PDT
Regarding your request for clarification: it is necessary to specify
how the routine is supposed to know whether a particular temperature
is invalid.
In addition, in your phrase:
Write down a modification to the for construct so that max_data
and max_index are only altered if the temperature is higher than the
running maximum and the temperature is not valid
I think "not valid" should read "valid".
|
Request for Answer Clarification by
reebok-ga
on
20 Sep 2002 16:08 PDT
Yes, that should have been, not invalid. Sorry. The question dosent
specify, but in other parts of the supplied program there is a
bad_count, which states, data[i] > 120.0. Therefore I took anything
above 120.0 to be invalid data. What I have done so far:
int tms_calculate_max(float *data, int number)
{
int i, max_index;
float max_data;
max_data = data[0];
if (max_data > 120.0)
{
max_data = -999.90;
}
max_index = 0;
for (i=0; i < number; i++)
{
if ((max_data < data[i]) && (max_data > 120.0))
{
max_data = data[i];
max_index = i;
}
}
return (max_index);
}
/*
* end of tms_calculate_max()
*/
WHat this does, which I think is not quite right, if data is invalid,
say No.5 out of 10, then it will give you the highest temperature
before No.5, even if there was a higher temperature after No.5. And if
the first value is invalid it returns 999.9, and not -999.9.
|
Clarification of Answer by
rbnn-ga
on
20 Sep 2002 23:04 PDT
The following line from your program does not compute what you
describe the functionality as being:
if ((max_data < data[i]) && (max_data > 120.0))
Recall that data[i] is the temperature. Therefore, what you want to
test is:
if ((max_data < data[i]) && (data[i] > 120.0))
With this change I believe the program will work. However, you can
also change the lines:
max_data = data[0];
if (max_data > 120.0)
{
max_data = -999.90;
}
to
max_data=-999.9
if you want.
Regarding your comment:
And if
the first value is invalid it returns 999.9, and not -999.9.
Since the function returns an integer index, it cannot ever return
999.9 (or, for that matter, -999.9).
Finally, in your comments you might want to specify that the function
returns 0 if all values in the array are invalid.
|
Clarification of Answer by
rbnn-ga
on
21 Sep 2002 00:06 PDT
Oops, in my last message, the line:
if ((max_data < data[i]) && (data[i] > 120.0))
should read:
if ((max_data < data[i]) && (data[i] <= 120.0))
That is, we want to execute the body of the if statement if the data
is valid; and we want to ignore it if it is invalid.
(Although you might want to check that arbitrarily low temperatures
are not allowed as well; it seems like they should not be for the
whole "-999" hack to work.
|
Request for Answer Clarification by
reebok-ga
on
21 Sep 2002 08:04 PDT
Thankyou very much, I did try putting data[i] as 2nd part of the if
statment, but it did not work for me, I must have had the signs round
the wrong way. I did not know how to add the comments to this page so
optided for this method. Thank you again for your quick response feel
so much more confident with my answers now. Keep up the good work.
|
Clarification of Answer by
rbnn-ga
on
23 Sep 2002 13:38 PDT
No clarification is needed; just responding to the "request for
clarification" in order to satisfy the posting program.
|