Question

C++ Create a program that finds the dot product of two vectors. I'm currently trying to...

C++ Create a program that finds the dot product of two vectors.

I'm currently trying to display the dot product by calling the dotProduct member function however I am confused as to how to do this. What would be the proper way to display the dot product? I don't believe my dotProduct member function is set up correctly to access the proper data. Feel free to modify the dotProduct member function to allow for it to work with the other member functions. Any help would be much appreciated!

#include <iostream>
#include <vector>
#include <ctime>
#include <iomanip>
using namespace std;
class list
{
    public:
        list();
        void input(int s);
        void output();
        double dotProduct(vector <double> a, vector <double> b);
    private:
        vector <int> v;
};
list :: list() : v()
{
}
void list :: input(int s)
{
    int t;
    for(int i = 1; i <= s; i++)
    {
        t = rand() % 10;
        v.push_back(t);
    }
}
void list :: output()
{
    for(unsigned int i = 0; i < v.size(); i++)
        cout << v[i] << " ";
    cout << endl;
}
double list :: dotProduct(vector <double> a, vector <double> b)
{
    double product = 0;
    if(a.size() != b.size())
    {
        cout << "Vectors are not the same size\n";
    }
    for (unsigned int i = 0; i < a.size(); i++)
    {
        product = product + a[i] * b[i];
    }

    return product;

}
int main()
{
    list L1, L2,ob1;
    int s;
    cout << "Enter the size of list 1: \n";
    cin >> s;
    L1.input(s);
    cout << "\nEnter the size of list 2: \n";
    cin >> s;
    L2.input(s);
    cout << "\nVector 1: ";
    L1.output();
    cout << endl;
    cout << "Vector 2: ";
    L2.output();
    cout << endl;

    cout << "The dot product is: ";
    //code here to display the dot product of the two vectors
    cout << endl;   

    return 0;

}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

If you have any doubts, please give me comment...

#include <iostream>

#include <vector>

#include <ctime>

#include <iomanip>

#include <cstdlib>

using namespace std;

class list

{

public:

    list();

    void input(int s);

    void output();

    double dotProduct(const list &a);

private:

    vector<int> v;

};

list ::list() : v()

{

}

void list ::input(int s)

{

    int t;

    for (int i = 1; i <= s; i++)

    {

        t = rand() % 10;

        v.push_back(t);

    }

}

void list ::output()

{

    for (unsigned int i = 0; i < v.size(); i++)

        cout << v[i] << " ";

    cout << endl;

}

double list ::dotProduct(const list &a)

{

    double product = 0;

    if (v.size() != a.v.size())

    {

        cout << "Vectors are not the same size\n";

    }

    for (unsigned int i = 0; i < v.size(); i++)

    {

        product = product + v[i] * a.v[i];

    }

    return product;

}

int main()

{

    list L1, L2, ob1;

    int s;

    cout << "Enter the size of list 1: \n";

    cin >> s;

    L1.input(s);

    cout << "\nEnter the size of list 2: \n";

    cin >> s;

    L2.input(s);

    cout << "\nVector 1: ";

    L1.output();

    cout << endl;

    cout << "Vector 2: ";

    L2.output();

    cout << endl;


    cout << "The dot product is: ";

    double result = L1.dotProduct(L2);

    cout<<result<<endl;

    cout << endl;

    return 0;

}

Add a comment
Know the answer?
Add Answer to:
C++ Create a program that finds the dot product of two vectors. I'm currently trying to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Hi there. I tried to submit this before, but the copy/paste didn't copy my vectors correctly....

    Hi there. I tried to submit this before, but the copy/paste didn't copy my vectors correctly. Hello there. I am trying to implement the djikstras shortest path algorithm into my code in C++ with a vector of vectors. However, when I test I get an error "Exception thrown at 0x75FFC762 in graphMain.exe: Microsoft C++ exception: std::out_of_range at memory location 0x009CF26C" for line 115 in graph.cpp. Could you help me debug? I am so close! ----------------------------FILE NAME: pch.h--------------------------------------- // pch.cpp: source...

  • This is for a C++ program: I'm almost done with this program, I just need to...

    This is for a C++ program: I'm almost done with this program, I just need to implement a bool function that will ask the user if they want to repeat the read_course function. I can't seem to get it right, the instructions suggest a do.. while loop in the main function. here is my code #include <iostream> #include <cstring> #include <cctype> using namespace std; const int SIZE = 100; void read_name(char first[], char last[]); void read_course(int & crn, char des[],...

  • Remove srand(time(NULL)); from this C++ code so that it still finds random numbers correctly. Then, Write...

    Remove srand(time(NULL)); from this C++ code so that it still finds random numbers correctly. Then, Write a program that adds the following to the fixed code. • Add a function that will use the BubbleSort method to put the numbers in ascending order. – Send the function the array. – Send the function the size of the array. – The sorted array will be sent back through the parameter list, so the data type of the function will be void....

  • Can you fix this program and run an output for me. I'm using C++ #include using...

    Can you fix this program and run an output for me. I'm using C++ #include using namespace std; //function to calculate number of unique digit in a number and retun it int countUniqueDigit(int input) {    int uniqueDigitCount = 0;    int storeDigit = 0;    int digit = 0;    while (input > 0) {        digit = 1 << (input % 10);        if (!(storeDigit & digit)) {            storeDigit |= digit;       ...

  • ​c++ program that takes user input from the console and store the data into a linked list.

    c++ program that takes user input from the console and store the data into a linked list. The input made of 4 objects associated to a car: model, make, mileage and year. My program should take the how many inputs the user want to make, followed by the inputs themselves. After the input, my program should print the data inside the linked list. So far, the issue is that my program print some of the input, but not all. Input sample:3HondaSentra89002017ToyotaCamri1098271999NissanSentra87261987current...

  • I need to make a few changes to this C++ program,first of all it should read...

    I need to make a few changes to this C++ program,first of all it should read the file from the computer without asking the user for the name of it.The name of the file is MichaelJordan.dat, second of all it should print ,3 highest frequencies are: 3 words that occure the most.everything else is good. #include <iostream> #include <map> #include <string> #include <cctype> #include <fstream> #include <iomanip> using namespace std; void addWord(map<std::string,int> &words,string s); void readFile(string infile,map<std::string,int> &words); void display(map<std::string,int>...

  • I need to add something to this C++ program.Additionally I want it to remove 10 words...

    I need to add something to this C++ program.Additionally I want it to remove 10 words from the printing list (Ancient,Europe,Asia,America,North,South,West ,East,Arctica,Greenland) #include <iostream> #include <map> #include <string> #include <cctype> #include <fstream> #include <iomanip> using namespace std; void addWord(map<std::string,int> &words,string s); void readFile(string infile,map<std::string,int> &words); void display(map<std::string,int> words);    int main() { // Declaring variables std::map<std::string,int> words;       //defines an input stream for the data file ifstream dataIn;    string infile; cout<<"Please enter a File Name :"; cin>>infile; readFile(infile,words);...

  • C++ Assignment Project 1 - NodeList Building upon the the ListNode/List code I would like you...

    C++ Assignment Project 1 - NodeList Building upon the the ListNode/List code I would like you to extend the interface of a list to have these member functions as well. struct ListNode { int element; ListNode *next; } Write a function to concatenate two linked lists. Given lists l1 = (2, 3, 1)and l2 = (4, 5), after return from l1.concatenate(l2)the list l1should be changed to be l1 = (2, 3, 1, 4, 5). Your function should not change l2and...

  • C++ Assignment Project 1 - NodeList Building upon the the ListNode/List code I would like you to extend the interface of...

    C++ Assignment Project 1 - NodeList Building upon the the ListNode/List code I would like you to extend the interface of a list to have these member functions as well. struct ListNode { int element; ListNode *next; } Write a function to concatenate two linked lists. Given lists l1 = (2, 3, 1)and l2 = (4, 5), after return from l1.concatenate(l2)the list l1should be changed to be l1 = (2, 3, 1, 4, 5). Your function should not change l2and...

  • When running the program at the destructor  an exception is being thrown. Can someone help me out?...

    When running the program at the destructor  an exception is being thrown. Can someone help me out? vararray.h: #ifndef VARARRAY_H_ #define VARARRAY_H_ class varArray { public:    varArray(); // void constructor    int arraySize() const { return size; } // returns the size of the array    int check(double number); // returns index of element containg "number" or -1 if none    void addNumber(double); // adds number to the array    void removeNumber(double); // deletes the number from the array   ...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT