Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
8.3 kB
4
Indexable
Never
CODE
Language Used = C++
#include <iostream>
using namespace std;

// class implementation
class UpDownNumbers
{
    // data members
private:
    int start;
    int length;
    int *numbers;

public:
    // parameterized constructor
    UpDownNumbers(int start, int length)
    {
        this->start = start;
        this->length = length;
        numbers = new int[length];
    }

    // copy constructor
    UpDownNumbers(UpDownNumbers &obj)
    {
        // copy data members
        start = obj.start;
        length = obj.length;
        numbers = new int[length];
        // copying array elements
        for (int i = 0; i < length; i++)
        {
            numbers[i] = obj.numbers[i];
        }
    }

    // function to set start
    void setStart(int start)
    {
        this->start = start;
    }

    // function to generate array elements
    void generate()
    {
        // first numbers = start
        numbers[0] = start;
        for (int i = 1; i < length; i++)
        {
            // if previous number is even, next number is num / 2 + 5
            if (numbers[i - 1] % 2 == 0)
            {
                numbers[i] = numbers[i - 1] / 2 + 5;
            }
            // if previous number is odd, next number is 3*num - 1
            else
            {
                numbers[i] = 3 * numbers[i - 1] - 1;
            }
        }
    }

    // function to print array elements
    void print()
    {
        for (int i = 0; i < length; i++)
        {
            cout << numbers[i] << " ";
        }
        cout << endl;
    }
    // destructor
    ~UpDownNumbers()
    {
        delete[] numbers;
    }
};

// main function
int main()
{

    // declaring variables
    int start, length;
    cout << "Enter start and length for Object A: ";
    cin >> start >> length;

    // creating object A
    UpDownNumbers objectA(start, length);
    // calling generate function
    objectA.generate();

    // printing array elements
    cout << "Object A (start: " << start << ", length: " << length << "): ";
    objectA.print();

    // creating object B using copy constructor
    UpDownNumbers objectB(objectA);
    cout << "Object B (b=a): ";
    // printing array elements
    objectB.print();

    cout << "Enter new start for Object B: ";
    cin >> start;
    // setting new start for objectB
    objectB.setStart(start);
    cout << "b.start is " << start << " now" << endl;
    objectB.generate(); // calling generate function

    // printing array elements for both the objects
    cout << "Object A (start: " << start << ", length: " << length << "): ";
    objectA.print();
    cout << "Object B (start: " << start << ", length: " << length << "): ";
    objectB.print();
    return 0;
}

Explanationfor step 1
C++ program to create a class UpDownNumbers and declare the required methods, then creating an object and another object of this class using copy constructor of class.
Final answer
OUTPUT

Enter start and length for Object A: 2 15
Object A (start: 2, length: 15): 2 6 8 9 26 18 14 12 11 32 21 62 36 23 68 
Object B (b=a): 2 6 8 9 26 18 14 12 11 32 21 62 36 23 68 
Enter new start for Object B: 13
b.start is 13 now
Object A (start: 13, length: 15): 2 6 8 9 26 18 14 12 11 32 21 62 36 23 68 
Object B (start: 13, length: 15): 13 38 24 17 50 30 20 15 44 27 80 45 134 72 41 



Enter start and length for Object A: 1 10
Object A (start: 1, length: 10): 1 2 6 8 9 26 18 14 12 11 
Object B (b=a): 1 2 6 8 9 26 18 14 12 11 
Enter new start for Object B: 5
b.start is 5 now
Object A (start: 5, length: 10): 1 2 6 8 9 26 18 14 12 11 
Object B (start: 5, length: 10): 5 14 12 11 32 21 62 36 23 68



Enter start and length for Object A: 5 5
Object A (start: 5, length: 5): 5 14 12 11 32
Object B (b=a): 5 14 12 11 32
Enter new start for Object B: 10
b.start is 10 now
Object A (start: 10, length: 5): 5 14 12 11 32
Object B (start: 10, length: 5): 10 10 10 10 10 


Enter start and length for Object A: 20 10
Object A (start: 20, length: 10): 20 15 44 27 80 45 134 72 41 122
Object B (b=a): 20 15 44 27 80 45 134 72 41 122
Enter new start for Object B: 20
b.start is 20 now
Object A (start: 20, length: 10): 20 15 44 27 80 45 134 72 41 122
Object B (start: 20, length: 10): 20 15 44 27 80 45 134 72 41 122


Enter start and length for Object A: 41 21
Object A (start: 41, length: 21): 41 122 66 38 24 17 50 30 20 15 44 27 80 45 134 72 41 122 66 38 24
Object B (b=a): 41 122 66 38 24 17 50 30 20 15 44 27 80 45 134 72 41 122 66 38 24
Enter new start for Object B: 23
b.start is 23 now
Object A (start: 23, length: 21): 41 122 66 38 24 17 50 30 20 15 44 27 80 45 134 72 41 122 66 38 24
Object B (start: 23, length: 21): 23 68 39 116 63 188 99 296 153 458 234 122 66 38 24 17 50 30 20 15 44 



c++ code for the task mentioned above in the question
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
class NumbersClass
{
private:
    int *numbers;
    int length;
    bool isPrime(int num)
    {
        if (num == 1)
            return false;
        for (int i = 2; i < num; i++)
        {
            if (num % i == 0)
                return false;
        }
        return true;
    }

public:
    NumbersClass()
    {
        length = 0;
        numbers = new int[length];
    }
    NumbersClass(int len)
    {
        length = len;
        numbers = new int[length];
    }
    NumbersClass(NumbersClass &obj)
    {
        length = obj.length;
        numbers = new int[length];
        for (int i = 0; i < length; i++)
        {
            numbers[i] = obj.numbers[i];
        }
    }
    void setLength(int len)
    {
        length = len;
        numbers = new int[length];
    }
    void generate()
    {
        srand(time(0));
        for (int i = 0; i < length; i++)
        {
            numbers[i] = rand() % 1000;
        }
    }
    void filter(string type)
    {
        if (type == "prime")
        {
            for (int i = 0; i < length; i++)
            {
                if (!isPrime(numbers[i]))
                {
                    numbers[i] = -1;
                }
            }
        }
        else if (type == "nonprime")
        {
            for (int i = 0; i < length; i++)
            {
                if (isPrime(numbers[i]))
                {
                    numbers[i] = -1;
                }
            }
        }
    }
    void print()
    {
        for (int i = 0; i < length; i++)
        {
            cout << numbers[i] << " ";
        }
        cout << endl;
    }
};

int main()
{
    int len;
    cout << "Enter length: ";
    cin >> len;
    NumbersClass A(len);
    A.generate();
    NumbersClass B(A);
    NumbersClass C(A);
    B.filter("prime");
    C.filter("nonprime");
    cout << "A: ";
    A.print();
    cout << "B: ";
    B.print();
    cout << "C: ";
    C.print();
    return 0;
}


Explanationfor step 1
/* Here is the explanation for the code above:
1. The constructor with no parameters initializes the length to zero and creates an array of length zero.
2. The constructor with one parameter initializes the length to the given value and creates an array of that length.
3. The copy constructor initializes the length to the length of the given object and creates an array of that length. It then copies the values of the given object to the new array.
4. The setLength method changes the length of the array and creates a new array of that length.
5. The generate method generates random numbers between 0 and 999 and stores them in the array.
6. The filter method filters out the numbers according to the given type. If the type is prime, then all non-prime numbers are replaced by -1. If the type is nonprime, then all prime numbers are replaced by -1.
7. The print method prints the numbers in the array.
8. The main function first takes the length of the array from the user. It then creates an object of the class and generates random numbers. It then creates two new objects by passing the first object to the copy constructor. It then calls the filter method on the two new objects to filter out prime and non-prime numbers. It then prints the original array and the filtered arrays. */
Final answer

output:

Enter length: 15
A: 689 927 248 24 679 255 834 916 834 656 964 284 431 444 839
B: 689 839 67
C: 272 480 246 792 558 349 168 346 569 642 844 314 995 568 54