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 +...
Please paraphrase this c code #include <stdio.h> #include <stdlib.h> #include <string.h> void sortGrades(int arr[], int size,...
Please paraphrase this c code #include <stdio.h> #include <stdlib.h> #include <string.h> void sortGrades(int arr[], int size, int status, char names[][20]); void printer(int grades[], int size, char names[][20]); void sortNames(char arr[][20], int size, int status, int grades[]); void nameSearch(int grades[], int size, char names[][20]); void numSearch(int grades[], int size, char names[][20]); int main() { int i; int size; int option; do { printf("\n\nInput Number of Students or 0 to exit : "); scanf("%d", &size); if (size == 0) { break; }...
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 "pch.h" #include <iostream> #include <stdio.h> #include <stdlib.h> #include <stdbool.h> int main() {        FILE...
#include "pch.h" #include <iostream> #include <stdio.h> #include <stdlib.h> #include <stdbool.h> int main() {        FILE *fp;        char c;        errno_t err;        err = 0;        err = fopen_s(&fp,"Desktop/test.txt", "r"); file is on my desktop but I err=2 but I don't think it is opening the file?        printf("%d\n", err);        if (err == 2)        {            printf("The file was opened\n");            while (1)       ...
Please Work on the commented parts in the code #include <stdio.h> #include <stdlib.h> /* * */...
Please Work on the commented parts in the code #include <stdio.h> #include <stdlib.h> /* * */ void printArray(int *arr, int size){ int i; for( i = 0; i < size; i++) { // Print each element out printf("%d ", *(arr+i)); //Print addresses of each element printf("%p", (arr+i)); //Printing a new line printf("\n"); } } int main() { // Allows user to specify the original array size, stored in variable n1. printf("Enter original array size: "); int n1 = 0; scanf("%d",...
#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 =...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT