Click here to Skip to main content
15,886,734 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am trying write C++ programs that compute the dot product of two given vectors. In vectors a and b only nonzero elements will be stored into array of structures(row and col). Each time I am getting irrelevant results. The correct result should be 50. Please advice.
Thank you in advance.
C++
<pre>#include <iostream>
#include <vector>
using namespace std;

const int n=10; /* vector size */
struct element {
int index; /* original index of non-zero array element */
int value ; /* integer non-zero value at index x */
};

element row[n];
element col[n];

vector<int> a={0,0,7,0,5,0,0,8,0,4,-1};
vector<int> b={0,0,0,5,6,0,0,0,0,5,-1};

void generate_row_and_col()
{
    for (int i=0; i<=n; i++)
    {
        if(a[i]!=0)
        {
            row[i].index=i;
            row[i].value=a[i];
        }
    }
    for (int j=0; j<=n; j++)
    {
        if(b[j]!=0)
        {
           col[j].index=j;
           col[j].value=b[j];
        }
    }
}
int dotproduct()
{
/* calculate the dot product of row and col output the result*/
int i=0;
int j=0;
int product=0;
while(row[i].index!=-1 && col[j].index!=-1)
{
    if(row[i].index == col[j].index)
    {
        product=product+row[i].value*col[j].value;
        i++;
        j++;
    }
    else if(row[i].index<col[j].index)
    {
        i++;
    }
    else
    {
        j++;
    }
}
return product;
}

int main()
{
    generate_row_and_col() ;
    int r;
    r=dotproduct();
    cout<<"result="<<r<<endl;
    return 0;
}


What I have tried:

I think i am not able to read the vectors properly into array of structures.
Posted
Updated 19-Sep-17 22:20pm
Comments
Nelek 20-Sep-17 1:01am    
I am not understanding what you try to do. What I think you are saying doesn't match what I see in your code.

It would be nice if you edit the question and give a bit more explanation. And a "what it is supposed to be" example of the matrix / vectors in each step.
CPallini 20-Sep-17 4:12am    
Sorry, what's the logic of discarding zero values in favour of garbage?

There are three errors in your code.

The first two ones: You have a fixed array size of 10 for row and col but are accessing 11 elements (out of bound access) and did not initialise all elements:
for (int i=0; i<=n; i++)
{
    // Not all row elements are initialised
    if(a[i]!=0)
    {
        // Out of bound access here when i == 10
        row[i].index=i;
        row[i].value=a[i];
    }
}
// And similar for col
for (int j=0; j<=n; j++)
{
    if(b[j]!=0)
    {
        col[j].index=j;
        col[j].value=b[j];
    }
}
The next is here:
while(row[i].index!=-1 && col[j].index!=-1)
where you check the index member for being -1. But that will never be the case because the index is just running from 0 to n (assuming all elements has been initialised). You might want to check the value member for -1 here.

So you would have to:

  • Set n to 11
  • Copy all elements from the vectors to row and col (optionally check for the array size and break after -1 has been copied)
  • Check the value member for being -1 instead of the index member (or set both members in the above step to -1 when the vector element is -1)
 
Share this answer
 
Comments
Mwater07 20-Sep-17 13:14pm    
Thank you. I have done the changes that you have suggested on the code, it works fine. But as you have stated, during initialization row and col arrays on generate_row_and_col function , I only initialize indexes with non zero elements, rest of them are not initialized. This is not causing any error in this program, but in terms of good programming practice,those indexes with 0 values are not initiated and the arrays will look like(garbage, garbage,7, garbage, 5, garbage, garbage....). Can we just limit the generate_row_and_col function, so We can only store non zero value indexes.
Thank you
Jochen Arndt 21-Sep-17 2:51am    
Where do you read "in terms of good programming practice,those indexes with 0 values are not initiated"?
It is good practice to initialise all variables.

If the array would be later used for other purposes you would have problems and might not remember that they are sourced by not initialising all members.
You are trying to complicate a simple matter. Try
C++
#include <iostream>
#include <vector>
using namespace std;


// Please note: error handling (e.g. a.size()!=b.size()) left as exercise
int dot(const vector<int> & a, const vector <int> &b)
{
  int dp = 0;
  for (size_t n = 0; n< a.size(); ++n)
  {
    dp += a[n]*b[n];
  }
  return dp;
}


int main()
{
  vector<int> a={0,0,7,0,5,0,0,8,0,4};
  vector<int> b={0,0,0,5,6,0,0,0,0,5};
  int dp = dot(a,b);

  cout << "result = " << dp << endl;
}
 
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