funtion dealing with positive array elements

This is a discussion on funtion dealing with positive array elements within the C++ Programming forums, part of the General Programming Boards category; The function I have written is an attempt to return true if all elements in an array are positive and ...

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    61

    funtion dealing with positive array elements

    The function I have written is an attempt to return true if all elements in an array are positive and false otherwise. When I run it it keeps running it as true even though I have one negative number in the array. And depending on what code adjustments I make I get a warning that says:
    Code:
      warning C4715: 'pos' : not all control paths return a value
    I know it has something to do with me not setting up a condition if an array element happens to be negative but i thought the return false if not true statement took care of that. Any help is always appreciated.

    Code:
    #include <iostream>
    using namespace std;
    bool pos(int a[],int size );//prototype
    int main()
    {
    
     
     const int size=10;
    int a[size]={1,2,3,4,5,6,-7,8,9,10};
    
    
     cout<<"All elements in array are positive";
     cout<<pos(a,size);//call
    
      return 0;
    
    }
    
    
     bool pos(int a[], int size) //print number of even elements
     {
        
    	 for(int i=0;i<size;i++){
             if((a[i]%2)==0)
            
    	 
    		   
    		return true;
    		else {
    			 return false;
    		       }	                        
       
    	                                   }                             
    
     
     }

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Well, don't return if it is true, because you want to keep searching, put the return true statement after the loop (return true if and only if the condition was never false). Additionally, your condition tests for even numbers, not positive or negative.

  3. #3
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    >>if((a[i]%2)==0)

    a[i]%2 divides the element by two and returns the remainder. Thus, that if statement tests whether the number is even or not.

    Change that statement to test whether the number is less than zero (assuming 0 counts as positive in your function)

    Then you must also change your loop, because it will return the first time the loop runs. If your condition is true (the number is less than 0), then return false, otherwise don't put an else statement, just let the loop run again. Outside of the loop put return true

    Edit: Beaten
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  4. #4
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Your loop is checking if all elements are even, not if they are positive. If they're positive, they're > 0 There's a few other things you need to check up on too, the first being your indentation. Ouch, you code is hurting my eyes! lol... nothings lined up or anything. Anyhoo, here goes...
    Code:
    bool pos(int a[], int size) //print number of even elements
    {
       for(int i=0;i<size;i++)
       {
    	  if((a[i ]%2)==0) //You're checking if it's even, not positive
    		 return true; //Hey, we're only on the first array element and already we're returning?
    	  else
    		 return false; //Same as above
       } 
    }
    Here's the how it should be:
    Code:
    bool pos(int a[], int size) //print number of even elements
    {
       for(int i=0;i<size;i++)
       {
    	  if(a[i ] <= 0) //Check if it's not positive
    		 return false; //If this element isn't positive, return false
     
    	  //if it's positive, keep going
       }
     
       //None of the elements were not positive
       return true; //So they must all be positive.
    }
    **EDIT**
    Beaten * 2...
    Last edited by Hunter2; 07-08-2004 at 06:11 PM.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  5. #5
    Registered User
    Join Date
    Jul 2004
    Posts
    61

    Thumbs up

    thanks guys. It works now, I appreciate it. The if loop condition was a silly mistake that results from me being tired. I always get locked up on return statements in loops. Thanks a lot.


    Thanks for the step by step illustration! Really made it more clear.
    Last edited by dantestwin; 07-08-2004 at 06:30 PM.

  6. #6
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    No problem Always glad to swat those bugs.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. coping elements not in array
    By lord in forum C++ Programming
    Replies: 2
    Last Post: 08-04-2008, 08:53 PM
  2. Replies: 2
    Last Post: 07-11-2008, 08:39 AM
  3. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 05:15 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  5. A simple question about selecting elements in an array
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 08-30-2001, 11:37 PM

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