Optimize the code in so that it runs in less than 11.5 seconds or faster. The ba
ID: 3530008 • Letter: O
Question
Optimize the code in so that it runs in less than 11.5 seconds or faster. The basic idea is to change the contents of the inner loop to make the computation run faster while having it still repeatedly compute the sum of the array elements. #include #include #define N_TIMES 600000 #define ARRAY_SIZE 10000 int main (void) { double *array = calloc(ARRAY_SIZE, sizeof(double)); double sum = 0; int i; printf("CS201 - Asgmt 4 - "); for (i = 0; i < N_TIMES; i++) { // do not change anything above this comment, // except for your name int j; for (j = 0; j < ARRAY_SIZE; j++) { sum += array[j]; } // do not change anything below this comment } return 0; } thank you.Explanation / Answer
int main (void) {
double *array = calloc(ARRAY_SIZE, sizeof(double));
double sum = 0;
int i;
printf("CS201 - Asgmt 4 - ");
for (i = 0; i < N_TIMES; i++) {
// do not change anything above this comment, // except for your name
int j;
for (j = 0; j < ARRAY_SIZE/2; j++) {
sum += (array[j] + array[ARRAY_SIZE-j-1]);
}
if( ARRAY_SIZE % 2 != 0)
sum += array[j];
// do not change anything below this comment
}
return 0;
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.