Arrays and Functions

I need help with this question please!

Please write me step by step instructions or how should I begin the problem.

Thank you for your time ;)

Write a function that takes an array and returns true if all the elements in the array are positive, otherwise, it returns false.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include<iostream>
using namespace std;

bool CheckElems( int arr [], int size )
{

  for( int i = 0; i < size; ++i )
  {
        if( arr[ i ] <= 0 )    // check arr[ i ] element
         return false;     //  negative element detected => return false
  }

   return true;          // all the elements were positive

}

int main( ) 
{

  int  vec[ 5 ] = { 1, 2, 3, 4, 5 };

  if( CheckElems( vec, 5 ) )
    cout << "True\n";

 else 
   cout << "\nFalse";

 return 0;
}
Last edited on
@TTT
nutz wrote:

Please write me step by step instructions or how should I begin the problem.
thank you so much !

but here's what i dont understand

why is there an int vec [5] = 1,2........

also i want to allow the user to input 10 numbers

if ALL of these numbers are positive then the screen should output TRUE else output FALSE
I took int vec[5] just for example, if you want to input 10 numbers then modify the main function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int main()
{

const int size = 10; 
int vec[size];

cout << "Enter 10 numbers\n:";

for( int i = 0; i < size; ++i )
 cin >> vec[ i ];

 if( CheckElems( vec, size ) )
    cout << "True\n";

 else 
   cout << "\nFalse";

 return 0;
}
Last edited on
ok great ;)
Topic archived. No new replies allowed.