CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

View Poll Results: convert c code to java code

Voters
3. You may not vote on this poll
  • no

    0 0%
  • no

    0 0%
  • no

    1 33.33%
  • no

    2 66.67%
Results 1 to 7 of 7
  1. #1
    Join Date
    Nov 2019
    Posts
    1

    convert c code to java code

    Code:
    /* File:  
     *    ex3.11a_serial_prefix_sums.c
     *
     * Purpose:  
     *    Compute the prefix sums of an input vector
     *
     * Compile:
     *    gcc -g -Wall -o ex3.11a_serial_prefix_sums ex3.11a_serial_prefix_sums.c
     * Run:
     *    ./ex3.11a_serial_prefix_sums <order of vector>
     *
     * Input:
     *    A vector
     * Output:
     *    The prefix sums of the vector
     *
     * IPP:   Exercise 3.11a
     */
    #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 */
    Last edited by 2kaud; November 21st, 2019 at 09:37 AM. Reason: Added code tags

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: convert c code to java code

    Is there a point to this post?

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: convert c code to java code

    Quote Originally Posted by GCDEF View Post
    Is there a point to this post?
    I think he wants someone to convert the c code to Java? but why a poll??
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  4. #4
    Join Date
    Feb 2017
    Posts
    677

    Re: convert c code to java code

    Quote Originally Posted by 2kaud View Post
    but why a poll??
    Maybe it's to find out how many didn't want to convert the code until finally someone did.

    Anyway what I do know is the importance of the "prefix sums" the code calculates.

    When you come across an O(N*N) algorithm consisting of two nested loops in a time bottleneck you will want to replace the innermost loop with a table look-up turning the algorithm into O(N). Then quite often actually the table you are looking for is the one holding the prefix sums.

  5. #5
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

    Re: convert c code to java code

    Quote Originally Posted by wolle View Post
    Maybe it's to find out how many didn't want to convert the code until finally someone did.
    Probably...
    But why then any option is only "no"?
    Victor Nijegorodov

  6. #6
    Join Date
    Feb 2017
    Posts
    677

    Re: convert c code to java code

    Quote Originally Posted by VictorN View Post
    Probably...
    But why then any option is only "no"?
    No is not the only option, you have four identical no's to choose between . There must be some weird kind of quantum computing going on.
    Last edited by wolle; November 22nd, 2019 at 05:19 PM.

  7. #7
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

    Re: convert c code to java code

    Victor Nijegorodov

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured