Showing posts with label generic vector creation modification and multiplication. Show all posts
Showing posts with label generic vector creation modification and multiplication. Show all posts

Wednesday, May 11, 2022

Write a class template to represent a generic vector. Include member functions to perform the following tasks: (a) To create two vectors. (b) To modify the value of a given element. (c) To multiply the two vectors (d) To display the vectors in the form (10, 20, 30,......):

 it is to be noted that this codes shows unexpected solutions for different sized vectors because i wrote this just for solving the question for two equal sized vectors

......oh yeah,,,,,,,




#include <iostream>

using namespace std;
template <class T1, class T2, class T3>
class vect1
{

public:
    T1 arr1[100];
    T2 arr2[100];
    T3 arr3[100];
    float sum;
    int size1, size2, i, j, k;
    void array1()
    {
        cout << "vector 1 size\n";
        cin >> size1;

        cout << "vector 2 size\n";
        cin >> size2;

        if (size1 != size2)
        {
            cout << "vector size should be of same size for multipication\n";
        }
        else if (size1 == size2)
        {

            cout << "enter vector 1\n";
            for (i = 0; i < size1; i++)
            {
                cin >> arr1[i];
            }
            cout << "enter vector 2\n";
            for (j = 0; j < size2; j++)
            {
                cin >> arr2[j];
            }
        }
    }
    void modify()
    {
        int index;
        int choice, newentry;
        cout << "enter  1 to modify vector 1 and\n 2 to modify vector 2 and\n 3 to do nothing\n";
        cin >> choice;
        if (choice == 1)
        {
            cout << "enter number to modify in vector 1\n";
            cin >> index;
            cout << "enter new number for vector 1\n";

            for (i = 0; i < size1; i++)
            {
                if (arr1[i] == index)
                {

                    cin >> newentry;
                    arr2[i] = newentry;
                }
            }
        }
        else if (choice == 2)
        {
            cout << "enter number to modify in vector 2\n";
            cin >> index;
            cout << "enter new number in vector 2\n";

            for (j = 0; j < size2; j++)
            {
                if (arr2[j] == index)
                {

                    cin >> newentry;
                    arr2[j] = newentry;
                }
            }
        }
    }
    void mul()
    {
        for (k = 0; k < size1; k++)
        {
            arr3[k] = arr1[k] * arr2[k];
            sum += arr3[k];
        }
        cout << " SCALAR MULTIPLICATION IS " << sum << endl;
    }
    void display()
    {
        cout << "vector 1 is" << endl;
        cout << "( ";
        for (i = 0; i < size1; i++)
        {
            cout << arr1[i] << " , ";
        }
        cout << ")" << endl;
        ;

        cout << "vector 2 is" << endl;
        cout << "( ";
        for (j = 0; j < size2; j++)
        {
            cout << arr2[j] << " , ";
        }
        cout << ")" << endl;
    }
};
int main()
{
    vect1<int, int, int> obj;
    obj.array1();
    cout << "\nbefore any modifications \n" << endl;
    obj.display();

    obj.modify();
    cout << "\n\nafter each modifications\n\n " << endl;

    obj.display();
    obj.mul();

    return 0;
}

HackCache ’26: A 48-Hour Online Hackathon Open to Every Student

  Hackathons are often associated with computer science students, experienced developers, and teams that already know exactly what they are...

Popular Posts