Google Answers Logo
View Question
 
Q: Open University T223 TMA 4 Q3 in full, further explanation of the question ( Answered 5 out of 5 stars,   0 Comments )
Question  
Subject: Open University T223 TMA 4 Q3 in full, further explanation of the question
Category: Computers > Programming
Asked by: reebok-ga
List Price: $15.00
Posted: 20 Sep 2002 04:50 PDT
Expires: 20 Oct 2002 04:50 PDT
Question ID: 67196
Am getting errors when I compile, one has been resolved, see previous
question, but then another appered, so I have tried to expand on the
question, i hope this makes sense.
/*
 * function tms_calculate_max()
 * ============================
 * This function returns the index in TemperatureLog[]
 * of the maximum of the stored temperature readings.
 */
int tms_calculate_max(float *data, int number)
	
		{
		int i, max_index;
		float max_data;
		max_data = data[1];
		max_index = 0;
		for (i=0; i < number; i++)
			{
			if (max_data < data[i])
(line  404)			{
				max_data = data[i];
				max_index = *data[i];
				data[i]++;
				}
			}
		return (max_index);
		}

/*
 * end of tms_calculate_max()
 */

Thank you, but yes I was hoping for the whole TMA question to be
answered,but a bit tricky to reproduce on here. Your solution did work
for that problem but did through up another. Illegal use of floating
point line 406. I will try and express the question a bit further, but
there is a flow chart, which I will try and put into words. Local
variables: i  for loop counter, max_data  for the running maximum
temperature, max_data  for the array index of the running maximum
temperature, data[] for the array containig the floating point
temperatures, number for the number of readings stored in the data[]
array. Flow chart: Start - Set max_data to the first temperature
reading - Set
max_index to zero - Set array index to zero - All readings tested? -
Yes - Return max_index - No --- Is reading > max_data ? - No -
Increment array index (return to all readings tested?) - Yes -
max_data = reading - max_index = array index of reading(which was what
I was trying to do in line 406) - Increment array index (return to all
readings tested?, in the flow chart).  I have never used google answer
before and not sure if I am compling by puting this hear any advice
welcome.
Answer  
Subject: Re: Open University T223 TMA 4 Q3 in full, further explanation of the question
Answered By: rbnn-ga on 20 Sep 2002 10:58 PDT
Rated:5 out of 5 stars
 
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.
reebok-ga rated this answer:5 out of 5 stars
Excelent, but I came online to remove the question as I had managed to
get some where, max_index = i, I have been feeling very ill, not easy
sitting here trying to progress something that needs to go in the post
very soon, and sneezing over the screen. data[1] I left in by mistake,
I used [0] and was getting the result of the initial value in the
array first off, so I tried to see if I could get another number. Mine
still worked with the data[i]++, but I do remember now what it does. I
do need some simple index to C functions etc. But thankyou, it nice to
have what I have done comfirmed, and there is support out there, you
have been very efficient, and I am most impressed, I whish I knew of
this service at the start of the course. I will definatly use it for
next years course, something about databases...? I have past exam
papers to go through for my exam in 2 weeks, so htere could be some
more questions coming. Thanks again.

Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy