Jump to content

program to calculate dot product

- - - - -

  • Please log in to reply
3 replies to this topic

#1
aruwin

aruwin

    Learning Programmer

  • Members
  • PipPipPip
  • 42 posts
I tried to make a program to calculate the dot product or inner product of 2 vectors but the results won't turn out right.
Tell me what is wrong.

#include <stdio.h>
#include <math.h>
#include <stdlib.h>

int main()
{
int i;
double ip = 0.0;
double u[1];
double v[1];

for (i=0; i<3; i++) {
ip = ip + u[i]*v[i];
return ip;

const int size = 1;
double v1[size], v2[size];

printf("Enter the components of%f\n");
printf("the first vector: %f\n");
for(int j=0; j<size; j++);
printf("the second vector: %f\n");
for(int j=0; j<size; j++);

double dot = (v1, v2, size);

printf("dot product of the vectors:%f\n",ip);

return 0;
}}

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
You have a return statement before you enter the vector. Why?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
aruwin

aruwin

    Learning Programmer

  • Members
  • PipPipPip
  • 42 posts

WingedPanther said:

You have a return statement before you enter the vector. Why?

I have erased the return statement but it still didn't work.Why?

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
Immediate issues I see:
1) You don't have a scanf anywhere, which means you never get input from the user.
2) Your arrays only hold 1 element, which will stop you from storing the full input from the user.
3) Your loop to calculate the inner product performs calculations prior to getting input.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users