Question

Write a c program to implement threads. Accept an array of 40 integers, write a thread method named sum of array elements. Di

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer: Hi!! Kindly finds your solution below. Let me know if any issue. Thanks.

This C program has an array with 40 integers. A thread method that divides the array into 4 parts and calculating the sum of each part. In the driver code, the main function creates 4 threads then joins all threads and the last print sum of all threads means all integers. Any confusion is there then feel free to ask me.

C-Code:

#include <stdio.h>
#include <pthread.h>
#include<stdlib.h>
  
// size of array
#define MAX_SIZE 40
  
// maximum number of threads
#define MAX_THREAD 4

int a[] = { 1, 5, 7, 10, 12, 14, 15, 18, 20, 22, 25, 27, 30, 64, 110, 2,3,11,4,9,6,34,10,17,19,21,13,
21,23,19,31,33,35,37,39,41,44,52,50,65}; //array with 40 integers
int sum[4] = { 0 };
int part = 0;
int arrays = 0;
  
void* sum_array(void* arg) //function
{
  
// divide array in 4 parts
int thread_part = part++;
  
for (int i = thread_part * (MAX_SIZE / 4); i < (thread_part + 1) * (MAX_SIZE / 4); i++)
sum[thread_part] += a[i];
}
  
// Driver Code
int main()
{
  
pthread_t threads[MAX_THREAD];
  
// Creating 4 threads
for (int i = 0; i < MAX_THREAD; i++)
pthread_create(&threads[i], NULL, sum_array, (void*)NULL);
  
// joining 4 threads i.e. waiting for all 4 threads to complete
for (int i = 0; i < MAX_THREAD; i++)
pthread_join(threads[i], NULL);
  
// calculate the sum of all 4 parts
  
for (int i = 0; i < MAX_THREAD; i++)
arrays += sum[i];
  
printf("Sum is: %d",arrays);
  
return 0;
}

Output:

pthread_join(threads[i], NULL); // calculate sum of all 4 parts for (int i = 0; i <MAX_THREAD; i++) arrays += sum[i]; 44 Sum

Add a comment
Know the answer?
Add Answer to:
Write a c program to implement threads. Accept an array of 40 integers, write a thread...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • You will write a program using 2 threads to calculate the Fibonacci number. Each thread will...

    You will write a program using 2 threads to calculate the Fibonacci number. Each thread will process one of the values used to calculate the result for the last iteration of the calculation. Each thread will use a recursive function to calculate its value and store its result in a global array. Each thread will return its results in a global array. The main program must wait for both threads to complete before exiting (join method must be used). The...

  • Write a C Program that inputs an array of integers from the user along-with the length...

    Write a C Program that inputs an array of integers from the user along-with the length of array. The program then prints out the array of integers in reverse. (You do not need to change (re-assign) the elements in the array, only print the array in reverse.) The program uses a function call with array passed as call by reference. Part of the Program has been given here. You need to complete the Program by writing the function. #include<stdio.h> void...

  • Use C++ to implement a program that can store and output 5 integers, where the user...

    Use C++ to implement a program that can store and output 5 integers, where the user can input its value into an array named arrayValue. The program should implement the following: 1. Calculate the sum and average of the array. (5 points) 2. Output the content (value) that stored in the array of five elements. (10 points) 3. Continue to ask the user to store for storing data into another array. (10 points)

  • Sorting Threads Assignment Overview Write a multithreaded sorting program in Java which uses the ...

    Sorting Threads Assignment Overview Write a multithreaded sorting program in Java which uses the merge sort algorithm. The basic steps of merge sort are: 1) divide a collection of items into two lists of equal size, 2) use merge sort to separately sort each of the two lists, and 3) combine the two sorted lists into one sorted list. Of course, if the collection of items is just asingle item then merge sort doesn’t need to perform the three steps,...

  • Use DevC++ to implement a program that can store and output 5 integers, where the user...

    Use DevC++ to implement a program that can store and output 5 integers, where the user can input its value into an array named arrayValue. The program should implement the following: 1. Calculate the sum and average of the array. (5 points) 2. Output the content (value) that stored in the array of five elements. (10 points) 3. Continue to ask the user to store for storing data into another array(store and output 5 integers). (10 points) Prelude to Programming...

  • Write a Program in C language for: 1. Create a C program to read 7 integers...

    Write a Program in C language for: 1. Create a C program to read 7 integers and store them in an array. Next, the program is to check if the array is symmetric, that is if the first element is equal to the last one, the value of the second one is equal to the value of the last but one, and so on. Since the middle element is not compared with another element, this would not affect the symmetry...

  • C programming Strictly - Write a program to sort an array of integers via arrays of...

    C programming Strictly - Write a program to sort an array of integers via arrays of pointers to those integers as shown in the figure. Problem 1 (68 points): Write a program to sort an array of integers via arrays of pointers to those integers as shown in the figure. The code should contain the following functions: 1)to randomly generate an array of integer numbers; 2) to allocate memory and build the arrays of pointers as shown in the figure...

  • l ask Write a C++ program that accomplishes the following tasks 1. Write a function that...

    l ask Write a C++ program that accomplishes the following tasks 1. Write a function that finds the average of current element and the next element in given array named as "elements" and stores the new found averages into another array called "averageArray". Note: For the last element you can just divide the value of last element by 2 Write a function that takes both the given array and average array you just created along with a variable "productofArrays" using...

  • Write a full C++program to do the following processes on a two dimension array of integers....

    Write a full C++program to do the following processes on a two dimension array of integers. Assume the size is 4x4 . 1- Fill the array with the numbers like this pattern: 1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4 Note: Do not accept the values as input, or hard code it. Find the math to generate these numbers and fill the array with them.    2- Reset the main diagonal elements...

  • Please write a C# program For this part of the lab, you'll be writing methods to...

    Please write a C# program For this part of the lab, you'll be writing methods to work with 1D arrays. We will expect you to know how to create an array, and store and retrieve information from them. Continue working in the same project and “Program.cs” file. You will be generating random numbers, but the class Random is part of the standard library (don’t need any additional using statements). You should consider developing the methods for this project incrementally. In...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT