Setters, Getters, into Array

I teach tech (databases) but c++ is not my specialty. This was given to me as preparation for a mini workshop and I am having a great deal of trouble turning it from concept to code. I know that there will need to be several COUT and CIN statements to input and output information. This will be int based. I know I will need to use O(1) to evaluate for even/odd integers. Would I use if then else statements to handle whether the array meets the requirements? I think I can use the void for copy. I understand bits and pieces but not the big picture. I'm sorry to take up your time but if you could give me any guidance, I would appreciate it greatly!


Create a setter that receives an array as argument. The array should contain only positive, even integers. Ifthe array contains any negative integers, output to the user a message saying The array should only contain positive
integers. If the array contains zero, let the user know that the array cannot contain any zeros. If the array contains odd integers, also let the user know that the array can only contain even integers. Each case should have a separate check and output statement. If the array matches the requirements, then you can create a dynamic array and copy the content of the new array into it.
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
int * checker( const int a[], int n )
{
   if ( std::any_of( a, a + n, [] ( int x ) { return ( x < 0 ); } ) )
   {
      std::cout << "bla...bla...bla...\n";
      return ( NULL );
   }

   if ( std::any_of( a, a + n, [] ( int x ) { return ( x == 0 ); } ) )
   {
      std::cout << "bla...bla...bla...\n";
      return ( NULL );
   }

   if ( std::any_of( a, a + n, [] ( int x ) { return ( x % 2 ); } ) )
   {
      std::cout << "bla...bla...bla...\n";
      return ( NULL );
   }

   int *p = new int[n];

   std::copy( a, a + n, p );

   return ( p );
}
Last edited on
Ok... I think I understand this. The first "if" will remove the possibility of any negative number and prevent anything from being created because the array would not meet the requirements . The second if will remove the possibility of any 0's and will prevent the array from being created because it contains 0's and therefore doesn't meet the requirements (ending in a null). The 3rd if will test to see if the integer is even. If it is not even, again, we would inform the user that the array was not successful because it didn't meet the requirements. The next two statements:

int *p = new int[n];
std::copy( a, a + n, p );

take all of the elements and create the new array using pointer *p.

That's amazing!!
Here's a couple of questions :
Does the new array get a new name?
If so, is that the *p pointer?
Here is a major noob question, what do the (a, a+n, []) represent? I understand that they are the integers that make up the array but I feel like I should know more about them.
I'm not sure I understand why there are so many ")" after the if's. What do they represent?
Thank you in advance. I can't stand not understanding something. I want to eventually present this to my classes but I don't want to talk about something I am unsure about.
This function returns a pointer to a dynamically allocated array.
std::any_of is a standard algorithm that declared in header <algorithm>.
Here

std::any_of( a, a + n, [] ( int x ) { return ( x == 0 ); } )

a - pointer to the first element of the array
a + n - pointer to the "after the last' element of the array

[] ( int x ) { return ( x == 0 ); } - a lambda expression
Again, thank you so much! I taught SQL last year and this is NOTHING like SQL.
Topic archived. No new replies allowed.