943,864 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 381
  • C++ RSS
Aug 8th, 2009
0

Is my homework Correct :P

Expand Post »
Hello

I have this task below & I need someone to tell me if I have done it correctly. The part where I am not sure is if I am correctly checking if the array is empty.

Quote ...
Write a function to find the maximum value in an array of ints a.

int findMax(int a[], int n)
// precondition : a[0..n-1] is an array of ints
// postcondition : returns the maximum value in array or
// INT_MIN if array is empty


Recall that INT_MIN is a predefined constant indicating the minimum value of an integer.
C++ Syntax (Toggle Plain Text)
  1. int findMax(int a[], int n) {
  2. /// Homework 2 ///
  3.  
  4. int max = 0;
  5.  
  6. if (n >= 0) { // does this check if the array is empty??
  7. for (int i=0; i<n; i++) {
  8. if (a[i] > max) { max = a[i]; }
  9. }
  10. return max;
  11. }
  12. else
  13. return INT_MIN; // array is empty so return INT_MIN
  14. }
Similar Threads
Reputation Points: 10
Solved Threads: 7
Junior Poster
gretty is offline Offline
158 posts
since Apr 2009
Aug 8th, 2009
1

Re: Is my homework Correct :P

Consider what happens if all the numbers in the array are negative. Then the if(a[i]>max) will never return true. There are two ways you can do this. Either set max to INT_MIN or set max to a[0] and loop from 1 to n then.
Reputation Points: 453
Solved Threads: 57
Posting Virtuoso
twomers is offline Offline
1,873 posts
since May 2007
Aug 8th, 2009
0

Re: Is my homework Correct :P

The check is fine.

The code is broke, if all the elements of the array are negative though.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Aug 8th, 2009
0

Re: Is my homework Correct :P

Another option is set max to a[0] .
Reputation Points: 769
Solved Threads: 128
Banned
ithelp is offline Offline
1,910 posts
since May 2006
Oct 13th, 2010
0
Re: Is my homework Correct :P
one third divided by three fifths equa two forths_
Reputation Points: 10
Solved Threads: 0
Newbie Poster
hello2 is offline Offline
2 posts
since Oct 2010
Oct 13th, 2010
-1
Re: Is my homework Correct :P
one third divided by three fiths equal two forths
Reputation Points: 10
Solved Threads: 0
Newbie Poster
hello2 is offline Offline
2 posts
since Oct 2010
Oct 13th, 2010
0
Re: Is my homework Correct :P
Click to Expand / Collapse  Quote originally posted by hello2 ...
one third divided by three fiths equal two forths
Aug 8th, 2009
Is my homework Correct



Please READ the original posts date before reviving old threads!!!
Reputation Points: 115
Solved Threads: 80
Posting Pro
Akill10 is offline Offline
569 posts
since Sep 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Writing to file - corrupted data?
Next Thread in C++ Forum Timeline: Designing Classes





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC