Click here to Skip to main content
15,867,765 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I would like to write a program that stores nonzero elements of 2 different vectors into 2 different linked lists with their indexes. Then would like t to calculate the dot product and displays the result. I am struggling to read in vector values with indexes into a link list. Please advice

#include <iostream>
#include <vector>
using namespace std;

  struct node{
    int index; /* original index of non-zero array element */
    int value ; /* integer non-zero value at index x */
    node *next;
    };

class linked_list
{
private:
    node *head,*tail;
public:
    linked_list()
    {
        head = NULL;
        tail = NULL;
    }

    void create (int x[], int n)
    {
        link head=0;
        for(int i=0; i<n;i++)
        head=new node (a[i],head);
    }


};


vector<int> x={0,0,7,0,5,0,0,8,0,4};
vector<int> y={0,0,0,5,6,0,0,0,0,5};


int dotproduct() /*Computes the stored nonzero values and returns the result to product*/
{
int product=0;
while(A!=0 && B!=0)
{
    if(A->index == B->index)
    {
        product = product + A->value * B->value;
        A=A->next;
        B=B->next;

    }
    else if(A->index < B->index)
    {
        A=A->next;
    }
    else
    {
        B=B->next;
    }
}
return product;
}

int main()
{
    generate_A_and_B() ;
    int r;
    r=dotproduct();
    cout<<"Dot Product = "<<r<<endl;
    return 0;
}


What I have tried:

I am having hard time to read in the given vectors into the linked list with their index values
Posted
Updated 21-Sep-17 16:24pm
Comments
Richard MacCutchan 22-Sep-17 3:31am    
That code will not even compile, where is the code for generate_A_and_B()? Also your linked_list::create() method generates a set of orphaned nodes.

1 solution

The code is not complete, you should use the debugger to see what your code us doing

There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900