#include #include #include #define NUM_THREADS 4 // Number of threads to use #define VECTOR_SIZE 100 // Size of the array // Structure to pass information to threads typedef struct { int start; // Starting index for this thread int end; // Ending index for this thread int *vector; // Pointer to the array } ThreadData; // Function that each thread will execute void *increment_vector(void *arg) { ThreadData *data = (ThreadData *)arg; // Increment the array elements in the specified range for (int i = data->start; i < data->end; i++) { data->vector[i]++; } pthread_exit(NULL); } int main() { int vector[VECTOR_SIZE]; pthread_t threads[NUM_THREADS]; ThreadData thread_data[NUM_THREADS]; int segment_size = VECTOR_SIZE / NUM_THREADS; // Initialize the array for (int i = 0; i < VECTOR_SIZE; i++) { vector[i] = i; } // Create threads and assign them segments of the array for (int i = 0; i < NUM_THREADS; i++) { thread_data[i].start = i * segment_size; thread_data[i].end = (i == NUM_THREADS - 1) ? VECTOR_SIZE : (i + 1) * segment_size; thread_data[i].vector = vector; pthread_create(&threads[i], NULL, increment_vector, (void *)&thread_data[i]); } // Wait for all threads to complete for (int i = 0; i < NUM_THREADS; i++) { pthread_join(threads[i], NULL); } // Display the incremented array for (int i = 0; i < VECTOR_SIZE; i++) { printf("%d\n", vector[i]); } return 0; }