Compute the Dot and Cross Product in C

CBeginner
Practice Now

Introduction

In this lab, you will learn how to compute the dot and cross product of 3D vectors in C programming. The lab covers the following steps:

  1. Read vector components: Develop a program that allows users to input the components of two 3D vectors.
  2. Compute dot or cross product: Implement functions to calculate the dot and cross product of the input vectors.
  3. Print the result: Display the computed dot or cross product to the user.

By the end of this lab, you will have a solid understanding of vector operations and their implementation in C.

Read Vector Components

In this step, you will learn how to read vector components in C programming for performing dot and cross product calculations. We'll create a program that allows users to input vector components and store them for further mathematical operations.

First, let's create a new C file for our vector operations:

cd ~/project
nano vector_operations.c

Now, add the following code to define a function for reading vector components:

#include <stdio.h>

#define VECTOR_SIZE 3

// Function to read vector components
void readVector(float vector[], int size) {
    printf("Enter %d vector components (separated by space): ", size);
    for (int i = 0; i < size; i++) {
        scanf("%f", &vector[i]);
    }
}

int main() {
    float vector1[VECTOR_SIZE];
    float vector2[VECTOR_SIZE];

    printf("Vector 1:\n");
    readVector(vector1, VECTOR_SIZE);

    printf("Vector 2:\n");
    readVector(vector2, VECTOR_SIZE);

    return 0;
}

Let's compile and run the program to test vector input:

gcc vector_operations.c -o vector_operations
./vector_operations

Example output:

Vector 1:
Enter 3 vector components (separated by space): 1 2 3
Vector 2:
Enter 3 vector components (separated by space): 4 5 6
Explanation

In this code:

  • We define a constant VECTOR_SIZE set to 3 for 3D vectors
  • readVector() function takes an array and its size as parameters
  • scanf() is used to read floating-point vector components
  • The main() function demonstrates reading two vectors

Compute Dot or Cross Product

In this step, you will learn how to compute dot and cross products for 3D vectors in C programming. We'll extend the previous program to include mathematical operations on vectors.

Update the vector_operations.c file with the following code:

cd ~/project
nano vector_operations.c

Add the implementation for dot and cross product calculations:

#include <stdio.h>

#define VECTOR_SIZE 3

void readVector(float vector[], int size) {
    printf("Enter %d vector components (separated by space): ", size);
    for (int i = 0; i < size; i++) {
        scanf("%f", &vector[i]);
    }
}

// Compute dot product
float computeDotProduct(float vector1[], float vector2[], int size) {
    float dotProduct = 0.0;
    for (int i = 0; i < size; i++) {
        dotProduct += vector1[i] * vector2[i];
    }
    return dotProduct;
}

// Compute cross product
void computeCrossProduct(float vector1[], float vector2[], float result[]) {
    result[0] = vector1[1] * vector2[2] - vector1[2] * vector2[1];
    result[1] = vector1[2] * vector2[0] - vector1[0] * vector2[2];
    result[2] = vector1[0] * vector2[1] - vector1[1] * vector2[0];
}

int main() {
    float vector1[VECTOR_SIZE];
    float vector2[VECTOR_SIZE];
    float crossProductResult[VECTOR_SIZE];

    printf("Vector 1:\n");
    readVector(vector1, VECTOR_SIZE);

    printf("Vector 2:\n");
    readVector(vector2, VECTOR_SIZE);

    // Compute and display dot product
    float dotProduct = computeDotProduct(vector1, vector2, VECTOR_SIZE);
    printf("Dot Product: %.2f\n", dotProduct);

    // Compute and display cross product
    computeCrossProduct(vector1, vector2, crossProductResult);
    printf("Cross Product: [%.2f, %.2f, %.2f]\n",
           crossProductResult[0],
           crossProductResult[1],
           crossProductResult[2]);

    return 0;
}

Compile and run the updated program:

gcc vector_operations.c -o vector_operations
./vector_operations

Example output:

Vector 1:
Enter 3 vector components (separated by space): 1 2 3
Vector 2:
Enter 3 vector components (separated by space): 4 5 6
Dot Product: 32.00
Cross Product: [-3.00, 6.00, -3.00]
Explanation
  • computeDotProduct() calculates the dot product by multiplying corresponding vector components
  • computeCrossProduct() calculates the cross product using the standard 3D vector cross product formula
  • The main() function demonstrates computing and displaying both dot and cross products

In this final step, you will enhance the vector operations program by adding formatted output and creating a function to print vector results in a clear, readable format.

Update the vector_operations.c file with the following improved implementation:

cd ~/project
nano vector_operations.c

Add the new printing function and modify the main program:

#include <stdio.h>

#define VECTOR_SIZE 3

void readVector(float vector[], int size) {
    printf("Enter %d vector components (separated by space): ", size);
    for (int i = 0; i < size; i++) {
        scanf("%f", &vector[i]);
    }
}

float computeDotProduct(float vector1[], float vector2[], int size) {
    float dotProduct = 0.0;
    for (int i = 0; i < size; i++) {
        dotProduct += vector1[i] * vector2[i];
    }
    return dotProduct;
}

void computeCrossProduct(float vector1[], float vector2[], float result[]) {
    result[0] = vector1[1] * vector2[2] - vector1[2] * vector2[1];
    result[1] = vector1[2] * vector2[0] - vector1[0] * vector2[2];
    result[2] = vector1[0] * vector2[1] - vector1[1] * vector2[0];
}

// New function to print vector with formatting
void printVector(const char* label, float vector[], int size) {
    printf("%s: [", label);
    for (int i = 0; i < size; i++) {
        printf("%.2f%s", vector[i], (i < size - 1) ? ", " : "");
    }
    printf("]\n");
}

int main() {
    float vector1[VECTOR_SIZE];
    float vector2[VECTOR_SIZE];
    float crossProductResult[VECTOR_SIZE];

    // Input vectors
    printf("Vector Input:\n");
    printVector("Vector 1", vector1, VECTOR_SIZE);
    readVector(vector1, VECTOR_SIZE);

    printVector("Vector 2", vector2, VECTOR_SIZE);
    readVector(vector2, VECTOR_SIZE);

    // Compute results
    float dotProduct = computeDotProduct(vector1, vector2, VECTOR_SIZE);
    computeCrossProduct(vector1, vector2, crossProductResult);

    // Print formatted results
    printf("\nVector Operations Results:\n");
    printVector("Vector 1", vector1, VECTOR_SIZE);
    printVector("Vector 2", vector2, VECTOR_SIZE);
    printf("Dot Product: %.2f\n", dotProduct);
    printVector("Cross Product", crossProductResult, VECTOR_SIZE);

    return 0;
}

Compile and run the updated program:

gcc vector_operations.c -o vector_operations
./vector_operations

Example output:

Vector Input:
Vector 1: [0.00, 0.00, 0.00]
Enter 3 vector components (separated by space): 1 2 3
Vector 2: [0.00, 0.00, 0.00]
Enter 3 vector components (separated by space): 4 5 6

Vector Operations Results:
Vector 1: [1.00, 2.00, 3.00]
Vector 2: [4.00, 5.00, 6.00]
Dot Product: 32.00
Cross Product: [-3.00, 6.00, -3.00]
Explanation
  • Added printVector() function to create consistent, formatted vector output
  • Enhanced main() function to demonstrate vector input and result printing
  • Improved readability of vector and computation results
  • Provides a clean, professional output format for vector operations

Summary

In this lab, you learned how to read vector components in C programming and perform dot and cross product calculations. You created a program that allows users to input vector components and then computes the dot or cross product of the vectors. The key learning points include defining a function to read vector components, implementing the dot and cross product formulas, and printing the results.

The program demonstrates how to work with 3D vectors in C and apply fundamental vector operations. By following the step-by-step instructions, you gained practical experience in handling vector data structures and performing common vector arithmetic operations.