Is my homework Correct :P

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2009
Posts: 149
Reputation: gretty is an unknown quantity at this point 
Solved Threads: 7
gretty gretty is offline Offline
Junior Poster

Is my homework Correct :P

 
0
  #1
Aug 8th, 2009
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.

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.
  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. }
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 1,871
Reputation: twomers has a spectacular aura about twomers has a spectacular aura about twomers has a spectacular aura about 
Solved Threads: 56
twomers's Avatar
twomers twomers is offline Offline
Posting Virtuoso

Re: Is my homework Correct :P

 
1
  #2
Aug 8th, 2009
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.
I blag!?
"Mr Kitty, you have to live in the attic now. Here, write a diary."
I am the Walrus!
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Is my homework Correct :P

 
0
  #3
Aug 8th, 2009
The check is fine.

The code is broke, if all the elements of the array are negative though.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 1,852
Reputation: ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all 
Solved Threads: 119
ithelp's Avatar
ithelp ithelp is offline Offline
Posting Virtuoso

Re: Is my homework Correct :P

 
0
  #4
Aug 8th, 2009
Another option is set max to a[0] .
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC