function returns 0

This is a discussion on function returns 0 within the C++ Programming forums, part of the General Programming Boards category; Hello, I am trying to make a function which will return the maximum value in an array. This function returns ...

  1. #1
    Registered User
    Join Date
    Dec 2006
    Location
    Jacksonville, AR
    Posts
    91

    function returns 0

    Hello,

    I am trying to make a function which will return the maximum value in an array. This function returns 0. I figured the for loop will iterate over all the elements of the array. It looks like its not. Please help.

    Thanks..

    Code:
    float Maximum(float* item, int el)
    {
    	float max = 0.0f;
    	for (int i = 0; i < el; ++i)
    	{
    		if (item[i] > item[i]+1)
    			max = item[i];
    		++item[i];
    	}
    	return max;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Well. How often is a number going to be greater than one larger than itself? (I mean you're testing whether 8 > 9, or -4 > -3; it's just never going to happen.)

    Also, you don't care whether the number is larger than its neighbor; you care whether it's the largest number of all of them.

  3. #3
    Registered User
    Join Date
    Dec 2006
    Location
    Jacksonville, AR
    Posts
    91
    Code:
    Well. How often is a number going to be greater than one larger than itself? (I mean you're testing whether 8 > 9, or -4 > -3; 
    it's just never going to happen.)
    Thanks for pointing that out, tabstop.. I think I've fixed this part. Anyway, this time, it always returns the last element of the array,
    whatever that is. Shouldn't the iterator move from element to element?

    Code:
    float FindMax(float* item, int el)
    {
    	float max = 0.0f;
    	for (int i = 0; i < el; ++i)  //<-- iterator working?
    	{
    		if (item[i] > item[i+1])
    			max = item[i];
    		else
    			max = item[i+1];
    		//++item[i];
    	}
    	return max;
    }

  4. #4
    Registered User
    Join Date
    Nov 2008
    Posts
    6
    alyeska,
    is your array ordered in increasing order?

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by alyeska View Post
    Code:
    Well. How often is a number going to be greater than one larger than itself? (I mean you're testing whether 8 > 9, or -4 > -3; 
    it's just never going to happen.)
    Thanks for pointing that out, tabstop.. I think I've fixed this part. Anyway, this time, it always returns the last element of the array,
    whatever that is. Shouldn't the iterator move from element to element?
    Yes. But that doesn't mean that you want to check the number next to it. Consider the array
    1 3 7 5 4.
    5 is bigger than 4 (the number after it), but that doesn't mean it's the maximum of the whole list.

  6. #6
    Registered User
    Join Date
    Nov 2008
    Posts
    6
    Sorry alyeska,
    I see now what the problem is.
    Every time runtime exits the for loop the max variable is newly assigned. A possible solution'd be to create a tmpmax variable, assign the value to this tmpmax variable and transfer tmpmax value to max if it's bigger.

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    No, that's not the problem. tabstop's post explains the problem.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    Registered User
    Join Date
    Nov 2008
    Posts
    6
    Yes, it is actually the problem. The for loop is running through all members of the array, so you check one number and then the next after it.
    tabstop is right saying that 5 is not the maximum of the whole list, hence the suggestion of creating a temporary variable.
    Code:
    float FindMax(float* item, int el)
    {
    	float max = 0.0f;
    	for (int i = 0; i < el; i++)  //<-- iterator working?
    	{
    		float tmpmax = max;
    		if (item[i] > item[i+1])
    		{
    			cout << item[i] << " > " << item[i+1];
    			tmpmax = item[i];
    			cout << " max: " << max << endl;			
    		}
    		
    		else
    		{
    			cout << item[i] << " < " << item[i+1];
    			tmpmax = item[i+1];
    			cout << " max:" << max << endl;
    		}
    		if (tmpmax > max)
    		{
    			max= tmpmax;
    		}
    
    	}
    	return max;
    }

  9. #9
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Code:
    if (item[i] > item[i+1])
    In addition to testing the wrong thing, your last test is always going to be testing the element beyond the end of the array, which is a huge bug.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  10. #10
    Registered User
    Join Date
    Nov 2008
    Posts
    6
    Oops, sorry tabstop... I was being thick. I couldn't see the obvious.

  11. #11
    Registered User
    Join Date
    May 2008
    Location
    Australia
    Posts
    230
    Code:
    for (int i = 0; i < ARRAY_SIZE-1; i++) {
    	maxval = array[i];
    	if (maxval < array[i+1])
    		maxval = array[i+1]
    }
    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    for (int i = 0; i < ARRAY_SIZE-1; i++) {
    	maxval = array[i];
    	if (maxval < array[i+1])
    		maxval = array[i+1]
    }
    This will definitely fail if you have a sequence of 1 3 7 4 2, as the result will be 4. Think about it.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  3. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  4. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 08:10 AM
  5. Change this program so it uses function??
    By stormfront in forum C Programming
    Replies: 8
    Last Post: 11-01-2005, 07:55 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21