Click here to Skip to main content
14,770,794 members
Please Sign up or sign in to vote.
2.00/5 (3 votes)
See more:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void Read_vector(char prompt[], double vect[], int n);
void Print_vector(char title[], double vect[], int n);
void Compute_prefix_sums(double vect[], double prefix_sums[], int n);

int main(int argc, char* argv[]) {
   double *vect, *prefix_sums;
   int n;

   if (argc != 2) {
      fprintf(stderr, "usage:  %s <order of vector>\n", argv[0]);
      exit(0);
   }
   n = strtol(argv[1], NULL, 10);
   vect = malloc(n*sizeof(double));
   prefix_sums = malloc(n*sizeof(double));

   Read_vector("Enter the vector", vect, n);
   Print_vector("Input vector", vect, n);
   Compute_prefix_sums(vect, prefix_sums, n);
   Print_vector("Prefix sums", prefix_sums, n);

   free(vect);
   free(prefix_sums);
   return 0;
}  /* main */

/*-------------------------------------------------------------------*/
void Read_vector(char prompt[], double vect[], int n) {
   int i;

   printf("%s\n", prompt);
   for (i = 0; i < n; i++)
      scanf("%lf", &vect[i]);
}  /* Read_vector */

/*-------------------------------------------------------------------*/
void Print_vector(char title[], double vect[], int n) {
   int i;

   printf("%s\n   ", title);
   for (i = 0; i < n; i++)
      printf("%.2f ", vect[i]);
   printf("\n");
}  /* Print_vector */

/*-------------------------------------------------------------------*/
void Compute_prefix_sums(double vect[], double prefix_sums[], int n) {
   int i;

   prefix_sums[0] = vect[0];
   for (i = 1; i < n; i++)
      prefix_sums[i] = prefix_sums[i-1] + vect[i];
}  /* Compute_prefix_sums */


What I have tried:

How to convert c code java code
Posted
Updated 11-Jan-21 6:33am
v2
Comments
Herman<T>.Instance 21-Nov-19 9:27am
   
CPallini 21-Nov-19 11:10am
   
Where is the trouble, on Java or C side?

C code and Java are quite different and you would need to rework it substantially. Start by learning Java, which is an object oriented, as opposed to C's procedure oriented, style.
   
You could try this converter, but don't expect a perfect conversion: C++ to Java Converter[^]
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900