I am new to this, can someone help me with this, this is a C program optimizatio
ID: 3626230 • Letter: I
Question
I am new to this, can someone help me with this, this is a C program optimization problem.for (j=0; j //modify here
no matter what I do I am getting “checksum error” please see the entire code below
// sample run:
// $ gcc -Wall test.c -0 test
// $ time ./test
#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
// Feel free to play with these numbers below during test,
// but ensure you submit with the correct numbers.
//
#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;
printf("optimize performance ");
// 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,
// Here is where you make your changes to
// optimize performance.
for (j=0; j // modify here
sum += array[j] + array[j+1] + array[j+2];
sum += array[j+3] + array[j+4] + array[j+5];
sum += array[j+6] + array[j+7] + array[j+8] + array[j+9];
}
// Do not alter anything below this line.
// ---------------------------------------------------------------
// Check each iteration.
//
if (sum != checksum) {
printf("Checksum error! ");
}
sum = 0;
}
return 0;
}
Explanation / Answer
#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
// Feel free to play with these numbers below during test,
// but ensure you submit with the correct numbers.
#define N_TIMES 200000
#define ARRAY_SIZE 9973
int main (void)
{
int *array = (int *)calloc(ARRAY_SIZE, sizeof(int));
int sum = 0;
int checksum = 0;
int i;
int j;
int x;
printf("optimize performance ");
// 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,
// Here is where you make your changes to
// optimize performance.
for (j=0; j<ARRAY_SIZE; j=j+10) {
sum += array[j] + array[j+1] + array[j+2];
if(j<9970){
sum += array[j+3] + array[j+4] + array[j+5];
sum += array[j+6] + array[j+7] + array[j+8] + array[j+9];
}}
printf(" sum: %d and checksum: %d at loop: %d",sum,checksum,i+1);
// Do not alter anything below this line.
// ---------------------------------------------------------------
// Check each iteration.
if (sum != checksum) {
printf("Checksum error! ");
}
sum = 0;
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.