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

Convert the below code to C code import java.util.Random; //Please ask me if you

ID: 3723189 • Letter: C

Question

 Convert the below code to C code  import java.util.Random;  //Please ask me if you have any concerns I ll reply as soon as possible   public class MaximuSubArray {     public static int maximumSum(int[] A) {         int maximumSum = 0;         int thisSum = 0;         for (int i = 0; i < A.length; i++) {             thisSum = 0;             for (int j = i; j < A.length; j++) {                 thisSum = thisSum + A[j];                 if (thisSum > maximumSum) {                     maximumSum = thisSum;                 }             }         }         return maximumSum;     }      public static int maximumSumOptimal(int A[]) {         int maximumSum = 0;         int thisSum = 0;         for (int j = 0; j < A.length; j++) {             thisSum = thisSum + A[j];             if (thisSum > maximumSum) {                 maximumSum = thisSum;             } else if (thisSum < 0) {                 thisSum = 0;             }         }         return maximumSum; 
     }     //Generate a random array with given capacity     public static int[] readRandom(int capacity) {         int a[] = new int[capacity];         for (int i = 0; i < capacity; i++) {             a[i] = new Random().nextInt(capacity);         }         return a;     }          //estimate running time complexity     public static void estimate(int capcity) {         System.out.println();         System.out.println("Capacity : " + capcity);         int a[] = readRandom(capcity);         System.out.println("MaximumSum  ");         long startOfMaximumSum = System.nanoTime();         System.out.println("Result : " + maximumSum(a));         long endOfMaximumSum = System.nanoTime();         System.out.printf("TimeTaken : %d ns ", endOfMaximumSum - startOfMaximumSum);          System.out.println("MaximumSumOptimal  ");         long startOfMaximumSumOptimal = System.nanoTime();         System.out.println("Result : " + maximumSumOptimal(a));         long endOfMaximumSumOptimal = System.nanoTime();         System.out.printf("TimeTaken : %d ns ", endOfMaximumSumOptimal - startOfMaximumSumOptimal);     }      public static void main(String[] args) {         estimate(100);         estimate(10000);     } } 

Explanation / Answer

/*
I did some modifications note here
1. Modified both the functions and passed one more parameter len that is the capacity of the array
2. To calculate we need to use clock() function from time.h that I used
3. To return array from function you need to use dynamic array so I created array using malloc of size capacity.
4. In C , you need to declare any variables before use so I declared all the variables in starting of function.
5. C has rand() function to generate any number so to take these number we need to take mod of it by capacity.
6. These all changes I maid only.

Sample Output:
Capacity : 100
MaximumSum
Result : 4648
TimeTaken : 0.000000 ms
MaximumSumOptimal
Result : 4648
TimeTaken : 0.000000 ms

Capacity : 10000
MaximumSum
Result : 46580059
TimeTaken : 187.520000 ms
MaximumSumOptimal
Result : 46580059
TimeTaken : 0.000000 ms
*/

//Your code start from below line

#include<stdio.h>
#include <sys/time.h>
int maximumSum(int A[],int len) {

int maximumSum = 0;
int thisSum = 0;
int i,j;
for (i = 0; i < len; i++) {
thisSum = 0;
for (j = i; j < len; j++) {
thisSum = thisSum + A[j];
if (thisSum > maximumSum) {
maximumSum = thisSum;
}
}
}
return maximumSum;
}

int maximumSumOptimal(int A[],int len) {
int maximumSum = 0;
int thisSum = 0;
int i,j;
for (j = 0; j < len; j++) {
thisSum = thisSum + A[j];
if (thisSum > maximumSum) {
maximumSum = thisSum;
} else if (thisSum < 0) {
thisSum = 0;
}
}
return maximumSum;
}
//Generate a random array with given capacity
int * readRandom(int capacity) {
int i;
//Creating array of size capacity
int *a=(int *)malloc(capacity*sizeof(int));
for (i = 0; i < capacity; i++) {
a[i] = rand()%capacity;
}
return a;
}

//estimate running time complexity
void estimate(int capcity) {
struct timeval s, e;
int i;
double time_taken;
printf(" ");
printf("Capacity : %d " ,capcity);
int *a = readRandom(capcity);
printf("MaximumSum ");
gettimeofday(&s, NULL); //Getting current time
printf("Result : %d " ,maximumSum(a,capcity));
gettimeofday(&e, NULL);
time_taken = (e.tv_sec - s.tv_sec) * 1000.0f + (e.tv_usec - s.tv_usec) / 1000.0f;
printf("TimeTaken : %f ms ", time_taken);

printf("MaximumSumOptimal ");
gettimeofday(&s, NULL); //Getting current time
printf("Result : %d " ,maximumSumOptimal(a,capcity));
gettimeofday(&e, NULL); //Getting current time
time_taken = (e.tv_sec - s.tv_sec) * 1000.0f + (e.tv_usec - s.tv_usec) / 1000.0f;
printf("TimeTaken : %f ms ", time_taken);
}

int main() {
estimate(100);
estimate(10000);

return 0;
}