Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

can someone please help! task is to optimize code and get it to run faster using

ID: 3626102 • Letter: C

Question

can someone please help!

task is to optimize code and get it to run faster using C program and GCC.

this code needs to be compiled WITHOUT gcc optimization feature. Compile the code like this: gcc -Wall -g Code.c -o Code
code needs to run less then 5.0 seconds.

sample run:

$ gcc -Wall test.c -0 test
$ time ./test

real 0m4.412s
user 0m4.390s
sys 0m0.000s


// This is the starting code.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

// Submitted code must have the correct values for these two constants:
// N_TIMES: 200000
// ARRAY_SIZE: 9973

#define N_TIMES 200000
#define ARRAY_SIZE 9973

int main (void)
{
int *array = calloc(ARRAY_SIZE, sizeof(int));
int sum = 0;
int checksum = 0;
int i;
int j;
int x;

// Initialize the array with random values 0 to 13.
//
srand(time(NULL));
for (j=0; j < ARRAY_SIZE; j++) {

x = rand() / (int)(((unsigned)RAND_MAX + 1) / 14);
array[j] = x;
checksum += x;
}
//printf("Checksum is %d. ",checksum);

for (i = 0; i < N_TIMES; i++) {

// ---------------------------------------------------------------
// Do not alter anything above this line, except for any variables that need to be declared
// outside of the i loop. Here is where you make your changes to
// optimize performance.


for (j=0; j < ARRAY_SIZE; j++) {
sum += array[j];
// printf("Sum is now: %d ",sum);
}


// Do not alter anything below this line.
// ---------------------------------------------------------------

// Check each iteration.
//
if (sum != checksum) {
printf("Checksum error! ");
}
sum = 0;

}
return 0;

Explanation / Answer

try optimizing by manually unrolling the loop the loop code is for (j=0; j
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at drjack9650@gmail.com
Chat Now And Get Quote