-1

I have written a function definition to get the scalar multiplication for a vector. I was given a prototype to work with but am having confusion with understanding pointers in this. here is the prototype.

void scalar_mult(double k, const threeVec_t *src, threeVec_t *dest);
// REQUIRES: src and dest point to threeVec_t objects.
// PROMISES: *dest contains the result of scalar multiplication
//   of k and *src.

struct threeVec {
  double x;
  double y;
  double z;
};

typedef struct threeVec threeVec_t;

and here is my code, what am I doing wrong?

void scalar_mult(double k, const threeVec_t *src, threeVec_t *dest)
{
    int i, j, result;
    for (i = 0; i < src; i++) {
                for (j = 0; j < dest; j++) {
                        result[i][j] = k * result[i][j];
}
NoNaMe
  • 6,020
  • 30
  • 82
  • 110
john john
  • 1
  • 1

1 Answers1

0

You declare result as

int i, j, result;

But your are treatingresult as 2D array which is wrong.

Are you looking for something like this?

// it is the callers duty whether the src is null or not
void scalar_mult(double k, const threeVec_t *src, threeVec_t *dest)
{
    dest->x = src->x * k;// arrow operator as member accessed by pointer 
    dest->y = src->y * k;
    dest->z = src->z * k;
}

You can call scalar_mult from main like this:

int main() {
    threeVec_t src = {2.0, 3.0, -1.0};
    threeVec_t dest;
    double k = 3.0;

    scalar_mult(k, &src, &dest);

    printf("x = %lf, y = %lf, z = %lf", dest.x, dest.y, dest.z); // Dot operator as member accessed by object 
    return 0;
}

Click here to run live.

mazhar islam
  • 5,561
  • 3
  • 20
  • 41