Question

1. Calculate the sum of a randomly populated array sequentially in add_serial( ) 2. Use OpenMP...

1. Calculate the sum of a randomly populated array sequentially in add_serial( )

2. Use OpenMP calculate the sum of the same array in add_parallel( )

a. Use a tree sum by dividing the array in some fashion among the threads.

Main( ) will output the resulting calculation times.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include < stdio.h >
#include < omp.h >

int add_serial( int arr[], int n)

{

    int sum = 0;

    for (int i = 0; i < n; i++)

    sum += arr[i];

  

    return sum;

}

  

int main()

{

    int arr[] = {12, 3, 4, 15};

    int n = sizeof(arr) / sizeof(arr[0]);

    printf("Sum of given array is %d", add_serial(arr, n));

    return 0;

}

Add a comment
Know the answer?
Add Answer to:
1. Calculate the sum of a randomly populated array sequentially in add_serial( ) 2. Use OpenMP...
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
  • C++: Write a program that uses a 3 X 3 array and randomly place each integer...

    C++: Write a program that uses a 3 X 3 array and randomly place each integer from 1 to 9 into the nine squares. The program calculates the magic number by adding all the numbers in the array and then dividing the sum by 3. The 3 X 3 array is a magic square if the sum of each row, each column, and each diagonal is equal to the magic number. Your program must contain at least the following functions:...

  • IN C++ Write a program that uses a 3x3 array and randomly places each integer from...

    IN C++ Write a program that uses a 3x3 array and randomly places each integer from 1 to 9 into the nine squares. The program calculates the "magic number" by adding all the numbers in the array and then dividing the sum by 3. The 3x3 array is said to be a "magic square" if the sum of each row, each column, and each diagonal is equal to the magic number. Your program must use at least the following functions:...

  • //In this assignment, we use multiple threads to calculate the sum // 1*1 + 2*2 +...

    //In this assignment, we use multiple threads to calculate the sum // 1*1 + 2*2 + 3*3 + 4*4 + ... + n*n // Note we should know from CSE2500 that this sum is // n*(n+1)*(2*n+1)/6 // We a n value, we will create 2*n threads to do the calculation so that // we can have a race condition. // Before you change the code, read the code and run the code and see what // happens. Do we already...

  • 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 small C program including the following C functions/subroutines. Your main routine should use these...

    write a small C program including the following C functions/subroutines. Your main routine should use these functions to generate a small-ish array of random values and show that the standard deviation lowers as the array is smoothed. • void random_array(double* array, int size, double scale); – load an array with random double values scaled by scale (random number generators generate double values between 0 and 1). Note: array should point to memory already allocated. • double sum(double* array, int size);...

  • Write a C program convert.c that converts each number in an array by the sum of...

    Write a C program convert.c that converts each number in an array by the sum of that number plus 6 modulus 10. A sample input/output: Enter the length of the array: 5 Enter the elements of the array: 3 928 4 14 77 Output: 9 4 0 0 3 The program should include the following function: void convert(int *a1, int n, int *a2) The function converts every element in array a1 of length n to an output array a2. The...

  • Task 1 Main Function: Declare and fill an array with 1000 random integers between 1 -...

    Task 1 Main Function: Declare and fill an array with 1000 random integers between 1 - 1000. You will pass the array into functions that will perform operations on the array. Function 1: Create a function that will output all the integers in the array. (Make sure output is clearly formatted. Function 2: Create a Function that will sum and output all the odd numbers in the array. Function 3: Create a Function that will sum and output all the...

  • Write a main program that will randomly fill a 2 dimensional array with integer values in...

    Write a main program that will randomly fill a 2 dimensional array with integer values in the range of 0 to 50. Use the Rand_Int function and constants to do this. These values represent the height of a terrain. Write a function that will determine what part of the terrain will flood. A. Create a global constant called MAX and assign it the value of 10. B. Prompt the user in main, at what height do they consider it to...

  • Lab Topics • The basics of Array object Use the following Coding Guidelines • When declaring...

    Lab Topics • The basics of Array object Use the following Coding Guidelines • When declaring a variable, you usually want to initialize it. Remember you cannot initialize a number with a string. Remember variable names are case sensitive. Use tabs or spaces to indent code within blocks (code surrounded by braces). Use white space to make your program more readable. Use comments after the ending brace of classes, methods, and blocks to identify to which block it belongs. Problem...

  • Matlab! Use Matlab to calculate array X and array Y. The first column is time (1,...

    Matlab! Use Matlab to calculate array X and array Y. The first column is time (1, 2, 3 ...), and the second column is value (0 and 1). When the value of array X is 0, the value of array Y is itself at the same time. When the value of array X is 1, the value of array Y will become the value of the previous Y(i-1) The data of the array is given, just how to change the...

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