Download the file linkedList.zip and modify the driver.cpp file by implementing
ID: 3540632 • Letter: D
Question
Download the file linkedList.zip and modify the driver.cpp file by implementing the
helper function called dotProduct(). The function will take the dot product of two lists: a and b
are the two list parameters that will passed into the function, and the result of the dot product will
be returned to the calling function. In this case main(). The prototype for this function is as
follows:
template <class Type>
int dotProduct(linkedListType<Type>& a, linkedListType<Type>& b);
Hint: You only need to modify the areas of the code in the driver.cpp file that are marked with the
TODO comments. Everything else should remain that same. You can implement this function anyway
you like, but you must use the provided lists and function prototype. The printList() function
provides an example on how to use a for-loop with an iterator for a passed in list parameter.
/**
* Driver.cpp - This file contains the driver code to test the
* linkedListType class implementation
*
* TODO: Include your name and course number here.
*/
#include <iostream>
#include <stdio.h>
#include "linkedList.h"
using namespace std;
template <class Type>
void printList(linkedListType<Type>& a);
template <class Type>
int dotProduct(linkedListType<Type>& a, linkedListType<Type>& b);
int main(int argc, char **argv)
{
// Declare two list variables for a and b
linkedListType<int> a;
linkedListType<int> b;
// Add some data to a
a.insertLast(44);
a.insertLast(34);
a.insertLast(-77);
a.insertLast(71);
a.insertLast(-89);
a.insertLast(0);
a.insertLast(-11);
// Add some data to b
b.insertLast(86);
b.insertLast(31);
b.insertLast(-64);
b.insertLast(21);
b.insertLast(-24);
b.insertLast(71);
b.insertLast(89);
// Print out the lists
cout << "a =";
printList(a);
cout << "b =";
printList(b);
// Calculate the dot product and print the result
int dp = dotProduct(a, b);
cout << "The dot product of a * b = ";
cout << dp;
cout << " ** Press any key to continue ** ";
getchar();
return 0;
}
template <class Type>
void printList(linkedListType<Type>& a)
{
cout << "[ ";
for (linkedListIterator<Type> itr = a.begin(); itr != a.end(); ++itr)
{
cout << *itr << " ";
}
cout << "]" << endl;
return;
}
template <class Type>
int dotProduct(linkedListType<Type>& a, linkedListType<Type>& b)
{
// TODO: Implement the details for the dot product for a and b.
// Consider using two loops, one embedded within the other.
return 0;
}
Explanation / Answer
template <class Type>
int dotProduct(linkedListType<Type>& a, linkedListType<Type>& b)
{
// TODO: Implement the details for the dot product for a and b.
// Consider using two loops, one embedded within the other.
int dotproduct = 0;
linkedListIterator<Type> b_itr = b.begin();
for (linkedListIterator<Type> a_itr = a.begin(); a_itr != a.end(); ++a_itr,++b_itr)
{
dotproduct = dotproduct + (static_cast<int> (*a_itr)) * (static_cast<int> (*b_itr));
}
return dotproduct;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.