Question

using C coding write a program that takes the numbers from the array {1,2,3,4,5,6,7,8,9,10} and uses...

using C coding

write a program that takes the numbers from the array {1,2,3,4,5,6,7,8,9,10} and uses 2 threads at the same time. One thread will find the addition of the numbers, and the other will find the multiplication of the numbers. The two different threads will run at the same time and will have an end run of:

the addition is: 55

the multiplication is: 3628800

Homework Answers

Answer #1

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h> //Header file for sleep(). man 3 sleep for details.
#include <pthread.h>

// size of array
#define MAX 16

// maximum number of threads
#define MAX_THREAD 4


int a[] = { 1,2,3,4,5,6,7,8,9,10 };
int sum=0;
int mul=1;
void* sum_array(void* arg)
{

// Each thread computes sum of 1/4th of array

for (int i =0;i<10; i++)
sum =sum+a[i];
}

void* mul_array(void* arg)
{

// Each thread computes sum of 1/4th of array

for (int i =0;i<10; i++){
mul =mul* a[i];
}
}

// Driver Code
int main()
{

pthread_t threads[2];

// Creating 4 threads

pthread_create(&threads[0], NULL, sum_array, (void*)NULL);


pthread_join(threads[0], NULL);

pthread_create(&threads[1], NULL, mul_array, (void*)NULL);


pthread_join(threads[1], NULL);



printf("the addition is: %d\n",sum);
printf("the multiplication is %d\n",mul);



return 0;
}

===================================

akshay@akshay-Inspiron-3537:~/CODE$ gcc pthread.c -pthread
akshay@akshay-Inspiron-3537:~/CODE$ ./a.out
the addition is: 55
the multiplication is 3628800

Know the answer?
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
Similar Questions
For this assignment, you need to write a parallel program in C++ using OpenMP for vector...
For this assignment, you need to write a parallel program in C++ using OpenMP for vector addition. Assume A, B, C are three vectors of equal length. The program will add the corresponding elements of vectors A and B and will store the sum in the corresponding elements in vector C (in other words C[i] = A[i] + B[i]). Every thread should execute an approximately equal number of loop iterations. The only OpenMP directive you are allowed to use is:...
For this assignment, you need to write a parallel program in C++ using OpenMP for vector...
For this assignment, you need to write a parallel program in C++ using OpenMP for vector addition. Assume A, B, C are three vectors of equal length. The program will add the corresponding elements of vectors A and B and will store the sum in the corresponding elements in vector C (in other words C[i] = A[i] + B[i]). Every thread should execute an approximately equal number of loop iterations. The only OpenMP directive you are allowed to use is:...
For this assignment you need to write a parallel program in C++ using OpenMP for vector...
For this assignment you need to write a parallel program in C++ using OpenMP for vector addition. Assume A, B, C are three vectors of equal length. The program will add the corresponding elements of vectors A and B and will store the sum in the corresponding elements in vector C (in other words C[i] = A[i] + B[i]). Every thread should execute approximately equal number of loop iterations. The only OpenMP directive you are allowed to use is: #pragma...
Write a C program Design a program that uses an array to store 10 randomly generated...
Write a C program Design a program that uses an array to store 10 randomly generated integer numbers in the range from 1 to 50. The program should first generate random numbers and save these numbers into the array. It will then provide the following menu options to the user: Display 10 random numbers stored in the array Compute and display the largest number in the array Compute and display the average value of all numbers Exit The options 2...
C program question: Write a small C program connect.c that: 1. Initializes an array id of...
C program question: Write a small C program connect.c that: 1. Initializes an array id of N elements with the value of the index of the array. 2. Reads from the keyboard or the command line a set of two integer numbers (p and q) until it encounters EOF or CTL - D 3. Given the two numbers, your program should connect them by going through the array and changing all the entries with the same name as p to...
C++ Write a program that takes two integer arrays of different sizes from the user (this...
C++ Write a program that takes two integer arrays of different sizes from the user (this means that the user can tell the program the size of each array), and then computes the intersection and union of those arrays. Note: the arrays can be of different lengths and the intersection/union should have unique elements in sorted order. Use pointer notation of arrays for this question. Eg: *(arr+1) [10]
Solve the following using java Write a program that runs three threads, each thread randomizes a...
Solve the following using java Write a program that runs three threads, each thread randomizes a number between 1 and 100. The main thread waits for all the others to finish, calculates the maximum of the numbers, which were randomized, and prints it. Random number 1: 10 Random number 2: 38 Random number 3: 81 Max: 81 Note: You should create 3 threads in adition to the main thread. Also, you can use a single thread class and create 3...
Write a C program that prompts the user to enter a line of text on the...
Write a C program that prompts the user to enter a line of text on the keyboard then echoes the entire line. The program should continue echoing each line until the user responds to the prompt by not entering any text and hitting the return key. Your program should have two functions, writeStr and readLn, in addition to the main function. The text string itself should be stored in a char array in main. Both functions should operate on NUL-terminated...
In c++ Write a program that creates a dynamically allocated array to store students’ grades. The...
In c++ Write a program that creates a dynamically allocated array to store students’ grades. The user should be able to decide the size of the array during run time. Write code to fill this array from the keyboard and then print out the values of your array,
1.Write pseudocode for a program that allows the user to input two numbers (X and Y)...
1.Write pseudocode for a program that allows the user to input two numbers (X and Y) and a code C. If the code has value 1, the program should output the larger of X and Y and otherwise output the smaller. Now convert your pseudocode to an assembly language program. Enter the program into the lab software assembler. Assemble the program and execute it with different input data. 2. Write pseudocode for a program that allows the user to input...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT