w3resource

C++ Exercises: Create an array of odd numbers and specific lengths from an array

C++ Basic Algorithm: Exercise-123 with Solution

Write a C++ program that creates a new array of odd numbers and specific lengths from a given array of positive integers.

Test Data:
({1,2,3,5,7,9,10},3) -> {1,3,5}

Sample Solution:

C++ Code :

#include <iostream>
#include <string.h>
using namespace std;
int *odds;


int * test(int * nums, int n) {
            odds = new int[n];
            int j = 0;

            for (int i = 0; j < n; i++)
            {
                if (nums[i] % 2 != 0)
                {
                    odds[j] = nums[i];
                    j++;
                }
            }

            return odds;
}


int main() {

  int nums[] = {1,2,3,4,5,6,7,8,9};
  int * result_array;
  int arr_length = sizeof(nums) / sizeof(nums[0]);
  cout << "Original array elements: " << endl;
  for (int i = 0; i < arr_length; i++) {
    std::cout << nums[i] << ' ';
  }
  int n = 3;
  odds = test(nums, n);
  cout << "\n\nNew array of odd numbers " << "(length = " << n <<")" <<" from the said array: " << endl;
  for (int i = 0; i < n; i++) {
    std::cout << odds[i] << ' ';
  }
  n = 5;
  odds = test(nums, n);
  cout << "\n\nNew array of odd numbers " << "(length = " << n <<")" <<" from the said array: " << endl;
  for (int i = 0; i < n; i++) {
    std::cout << odds[i] << ' ';
  }
  delete[] odds;
  return 0;
}

Sample Output:

Original array elements:
1 2 3 4 5 6 7 8 9

New array of odd numbers (length = 3) from the said array:
1 3 5

New array of odd numbers (length = 5) from the said array:
1 3 5 7 9

Flowchart:

Flowchart: Array from an existing array using string length.

C++ Code Editor:

Contribute your code and comments through Disqus.

Previous: Test a positive integer and return true if it contains the number 2.
Next: Each element of a given list of integers is multiplied by five.

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.

C++ Programming: Tips of the Day

What is the usefulness of `enable_shared_from_this?

It enables you to get a valid shared_ptr instance to this, when all you have is this. Without it, you would have no way of getting a shared_ptr to this, unless you already had one as a member.

class Y: public enable_shared_from_this
{
public:

    shared_ptr f()
    {
        return shared_from_this();
    }
}

int main()
{
    shared_ptr p(new Y);
    shared_ptr q = p->f();
    assert(p == q);
    assert(!(p < q || q < p)); // p and q must share ownership
}

The method f() returns a valid shared_ptr, even though it had no member instance. Note that you cannot simply do this:

class Y: public enable_shared_from_this
{
public:

    shared_ptr f()
    {
        return shared_ptr(this);
    }
}

The shared pointer that this returned will have a different reference count from the "proper" one, and one of them will end up losing and holding a dangling reference when the object is deleted.

Ref : https://bit.ly/3pwVzzz