Question

In: Computer Science

Please implement the 5 questions in source code: #include <stdio.h> #include <stdlib.h> #include <math.h> int main(...

Please implement the 5 questions in source code:

#include <stdio.h>

#include <stdlib.h>

#include <math.h>

int main( int argc, char* argv[] )

{

// Size of vectors

int n = 10000;

// Input vectors

double *restrict a;

double *restrict b;

// Output vector

double *restrict c;

// Size, in bytes, of each vector

size_t bytes = n*sizeof(double);

/* Q1: Allocate memory for vector a (10 points)*/

/* Q2: Allocate memory for vector b (10 points)*/

/* Q3: Allocate memory for vector c (10 points)*/

int i;

/* Initialize content of input vectors, vector a[i] = sin(i)^2 vector b[i] = cos(i)^2

*

*

* Q4: Please implement it.(30 points)

*

*

*

*/

#pragma acc kernel copyin(a[0:n],b[0:n]), copyout(c[0:n])

/* sum component wise and save result into vector c

*

*

*

* Q5: Please implement it.(30 points)

*

*

*

*

*/

// Sum up vector c and print result divided by n, this should equal 1 within error

double sum = 0.0;

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

   sum += c[i];

sum = sum/n;

printf("final result: %f\n", sum);

// Release memory

free(a);

free(b);

free(c);

return 0;

Solutions

Expert Solution

OUTPUT:

CODE:

#include <stdio.h>

#include <stdlib.h>

#include <math.h>

int main(int argc, char* argv[])

{

   // Size of vectors

   int n = 10000;

   // Input vectors

   double* a = NULL;

   double* b = NULL;

   // Output vector

   double* c = NULL;

   // Size, in bytes, of each vector

   size_t bytes = n * sizeof(double);

   /* Q1: Allocate memory for vector a (10 points)*/
   a = (double*)malloc(bytes);

   /* Q2: Allocate memory for vector b (10 points)*/
   b = (double*)malloc(bytes);

   /* Q3: Allocate memory for vector c (10 points)*/
   c = (double*)malloc(bytes);


   int i;

   /* Initialize content of input vectors, vector a[i] = sin(i)^2 vector b[i] = cos(i)^2

   *

   *

   * Q4: Please implement it.(30 points)

   *

   *

   *

   */
   for (int i = 0;i < 10000;++i)
   {
       a[i] = pow(sin(i), 2);
       b[i] = pow(cos(i), 2);
   }

#pragma acc kernel copyin(a[0:n],b[0:n]), copyout(c[0:n])

   /* sum component wise and save result into vector c

   *

   *

   *

   * Q5: Please implement it.(30 points)

   *

   *

   *

   *

   */
   for (int i = 0;i < 10000;++i)
   {
       c[i] = a[i] + b[i];
   }
   // Sum up vector c and print result divided by n, this should equal 1 within error

   double sum = 0.0;

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

       sum += c[i];

   sum = sum / n;

   printf("final result: %f\n", sum);

   // Release memory

   free(a);

   free(b);

   free(c);

   return 0;
}


Related Solutions

CODE A #include<stdio.h> #include<math.h> #include<stdlib.h> #define PI 3.14159265358979323846 int main(){ int diameter; printf("Enter value of diameter...
CODE A #include<stdio.h> #include<math.h> #include<stdlib.h> #define PI 3.14159265358979323846 int main(){ int diameter; printf("Enter value of diameter between 8 to 60 inches: "); scanf("%d",&diameter); // if(diameter>60 || diameter<=8){ // printf("Error! invalid input"); // exit(0); // } // else{ // float radius = diameter/2; // float volume = (4/3)*PI*radius*radius*radius; // printf("%.2f",volume); // } //check through the while loop if it is valid or in valid while(diameter>60 || diameter<=8){ printf("Invalid input Enter again: "); scanf("%d",&diameter); }    //caluclate the volume of sphere float...
#include <stdio.h> #include <math.h> int fun(int); int main(void)    {     int i = 5, x...
#include <stdio.h> #include <math.h> int fun(int); int main(void)    {     int i = 5, x = 3;     i = fun(x);     printf("%d\n", i);     return 0; } int fun(int i) {      int res = 0;      res = pow (i , 3.0);      return ( res); }
My current code that is not working correctly #include<stdio.h> #include<math.h> int main() { //variable declaration int...
My current code that is not working correctly #include<stdio.h> #include<math.h> int main() { //variable declaration int a,b,c,D,n; double x1,x2; double realPart, imagPart; do { // this set of code blocks prompts a message to the user and then read the integer entered and stores it as an integer printf("Enter a value for a:\n"); scanf("%d",&a); printf("Enter a value for b:\n"); scanf("%d",&b); printf("Enter a value for c:\n"); scanf("%d",&c); printf("You entered the Equation \n"); printf("%dx^2%+dx%+d=0\n",a,b,c); D = b*b - 4*a*c;    if (D<0)...
please fix code #include <stdio.h> #include <stdlib.h> #include <string.h> // function declarations int getValidJerseyNumber(); int getValidRating();...
please fix code #include <stdio.h> #include <stdlib.h> #include <string.h> // function declarations int getValidJerseyNumber(); int getValidRating(); int main() { // declaring variables int size = 5; int jerseyNo[size]; int rating[size]; int i = 0, jno, rate; char option; /* Getting the inputs entered by the user * and populate the values into arrays */ for (i = 0; i < size; i++) { printf("Enter player %d's jersey number:", i + 1); jerseyNo[i] = getValidJerseyNumber(); printf("Enter player %d's rating:\n", i +...
Can you translate this C code into MIPS assembly? #include <stdio.h> #include <math.h> #include <stdlib.h> double...
Can you translate this C code into MIPS assembly? #include <stdio.h> #include <math.h> #include <stdlib.h> double fact (double); void main () { int angle_in_D; double term, angle_in_R; float sine = 0; unsigned int i = 1; double sign = 1; int n = 1000; printf ("Please enter an angle (Unit: Degree): "); scanf ("%d", &angle_in_D); angle_in_R = angle_in_D * M_PI / 180.0; do { term = pow(-1,(i-1)) * pow (angle_in_R, (2*i - 1)) / fact (2*i - 1); sine =...
#include <stdio.h> #include <unistd.h> #include <stdlib.h> int main(int argc, char **argv) { int count; if ((argc...
#include <stdio.h> #include <unistd.h> #include <stdlib.h> int main(int argc, char **argv) { int count; if ((argc != 2) || (sscanf(argv[1],"%d",&count) != 1)) { fprintf(stderr,"Usage: %s <integer>\n", argv[0]); exit(1); } pid_t pid1, pid2; while (count > 0) { pid1 = fork(); if (pid1 > 0) { pid2 = fork(); count = count - 2; } else if (pid1 == 0) { count = count - 1; } } exit(0); } Question #1 [2 pts] If the command-line argument passed to this...
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> int main(int argc, char *argv[]) {     FILE *myFile;...
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> int main(int argc, char *argv[]) {     FILE *myFile;     char fname[20];     //int sum = 0;     int i, j, k, tmp =0;     int num = 0;     int mass = 0;     int count = 0;     int fuel = 0;     int total = 0;     int M[1000];     char ch;     char buffer[32];     printf(" Input the filename to be opened : ");     scanf("%s",fname);     myFile = fopen(fname, "r");     if(myFile == NULL)     {         printf("Can't open file\n");     } while(1)     {         ch =...
#include <stdio.h> #include <stdlib.h> #include <math.h> #include "../include/cis1057.h" /* * Programmer: << MA >> * Class:...
#include <stdio.h> #include <stdlib.h> #include <math.h> #include "../include/cis1057.h" /* * Programmer: << MA >> * Class: Introduction to C Programming 1057 Spring 2019 Section 004 * Assignment: Number 5 “estimate the value of a factorial using Gosper's algorithm." * Date: << 02-19-2019 >> * Version: 1 * Description: Program will prompt for some data, read in the values, perform the calculation using library math functions, and display the result. * File: lab5.c */ # define M_PI 3.14159265358979323846 /* pi */...
example_thread.c #include <stdio.h> #include <stdlib.h> #include <pthread.h> int shared= 0; void race(void); int main(){     pthread_t...
example_thread.c #include <stdio.h> #include <stdlib.h> #include <pthread.h> int shared= 0; void race(void); int main(){     pthread_t player1, player2, player3;     pthread_create(&player1, NULL, (void *)race, NULL);     pthread_create(&player2, NULL, (void *)race, NULL);     pthread_create(&player3, NULL, (void *)race, NULL);     pthread_join(player1, NULL);     pthread_join(player2, NULL);     pthread_join(player3, NULL);     printf("Total Number = %d\n", shared);     return 0; } void race(void) {     long i,tmp;     for(i=1; i<=200000; i++) {         tmp = shared;         tmp = tmp + 1;         shared =...
#include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <sys/time.h> int main(int argc, char **argv) { pid_t pid;...
#include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <sys/time.h> int main(int argc, char **argv) { pid_t pid; // Main process's PID=42 pid = fork(); // creates process with PID=36 if (pid == 0) { pid_t pid2 = fork(); // creates process with PID=99 sleep(10); if (pid2 > 0) { sleep(10); exit(0); } else { sleep(30); printf("** ONE **\n"); exit(0); } } else { pid_t pid3 = fork(); // creates process with PID=71 if (pid3 == 0) { sleep(30); exit(0); } pid_t...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT