Thread: Converting code

  1. #1
    Registered User
    Join Date
    Apr 2017
    Posts
    2

    Converting code

    Hi!

    I have a piece of code in C that I need to convert to python. The problem is that I don't know C! So I need some help to understand the code. The specific part I need help with is the nested loop and how the variables are defined that goes into that. I'm extremely thankful for any help!

    Code:
    #include<stdio.h> #include<math.h> main() { FILE *infp; infp=fopen("Cu3-_140520_RAES_5_binned_WOBG.Asc", "r"); FILE *outfp; outfp=fopen("Cu3-_140520_RAES_5b.log", "w"); int r; double truet, bint; double truects, bincts; double tmax; double first, second, step; fscanf(infp, "%lf %lf", &first, &truects); fprintf(outfp, "%lf\t%lf\n", first, truects); fscanf(infp, "%lf %lf", &second, &truects); step = second - first; fprintf(outfp, "%lf\t%lf\n", second, truects); int nbin=1; step = step * .5; r = fscanf(infp, "%lf %lf", &truet, &truects); while (r != EOF) { bint=truet; bincts=truects; tmax= truet + truet * step; r = fscanf(infp, "%lf %lf", &truet, &truects); while (r != EOF && truet < tmax) { bincts = bincts + truects; bint = bint + truet; r = fscanf(infp, "%lf %lf", &truet, &truects); nbin++; } bincts= bincts / nbin; bint= bint / nbin; fprintf(outfp, "%lf\t%lf\n", bint, bincts); nbin=1; } fclose(infp); fclose(outfp); return(0); }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    You should be able to make an educated guess as to what it's doing.

    Say what you think, then we'll help with any mis-understanding.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    That code looks pretty crappy, especially the inner loop. It might be better to give an example input file and explain the kind of result/output you're looking for.

  4. #4
    Registered User
    Join Date
    Apr 2017
    Posts
    2
    Thank you both for your answers.

    Here's my problem: this is a code for what I want to do but in python. That nested loop is where I'm stuck. I don't understand qualetively what it is I should do so I figured if I could understand the code I could re-do this myself in python.

    I know that this code opens a file (which is made up of two columns), then it defines a few variables, I dont understand

    Code:
    fscanf(infp, "%lf %lf", &first, &truects); fprintf(outfp, "%lf\t%lf\n", first, truects); fscanf(infp, "%lf %lf", &second, &truects); step = second - first; fprintf(outfp, "%lf\t%lf\n", second, truects); int nbin=1; step = step * .5;
    this part, and then goes into this nested loop where r is the iterator I think, so while r is not equal to end of file it's going through the loop, then adding the truet together which equals the tmax. After that it goes into the inner loop which is a while truet is smaller than tmax the bins are added together. Then it goes out of the loop and closes the file and program I think. So I get the big picture pretty much but not the details of it enough to recreate it if that makes sense. That's why I need some help!

  5. #5
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    You didn't really answer my question. What does the input file look like (give an (short) example). What is the result you want?

    r is not an "iterator". Did you look up the fscanf function? Don't guess! In this code, r is just telling you if you've hit the end of the file.

    The code doesn't entirely make sense to me, particularly the loop control, which seems possibly be where you're having trouble, too. Here's a somewhat simplified rewrite.
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        FILE *infp  = fopen("Cu3-_140520_RAES_5_binned_WOBG.Asc", "r"); 
        FILE *outfp = fopen("Cu3-_140520_RAES_5b.log", "w"); 
    
        double first, truects;
        fscanf(infp, "%lf %lf", &first, &truects);
        fprintf(outfp, "%lf\t%lf\n", first, truects);
    
        double second;
        fscanf(infp, "%lf %lf", &second, &truects);
        fprintf(outfp, "%lf\t%lf\n", second, truects);
    
        double step = (second - first) * .5;
    
        double truet;
        int r = fscanf(infp, "%lf %lf", &truet, &truects);
    
        while (r != EOF)
        {
            double bint = truet, bincts = truects;
            double tmax = truet + truet * step;
            int nbin = 1;
    
            r = fscanf(infp, "%lf %lf", &truet, &truects);
            while (r != EOF && truet < tmax)
            {
                bint += truet;
                bincts += truects;
                nbin++;
                r = fscanf(infp, "%lf %lf", &truet, &truects);
            }
    
            fprintf(outfp, "%lf\t%lf\n", bint / nbin, bincts / nbin);
        }
    
        fclose(infp);
        fclose(outfp);
    
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting, pumping Raw C code, Source code
    By secretcode in forum C Programming
    Replies: 5
    Last Post: 06-03-2016, 09:01 AM
  2. Help converting C# into apple code
    By jeromey55 in forum C# Programming
    Replies: 8
    Last Post: 08-17-2011, 02:19 PM
  3. Converting C code to DLX assembly code
    By xyz3 in forum C Programming
    Replies: 2
    Last Post: 05-17-2010, 02:01 PM
  4. C Code for converting generic to binary code
    By vanilla in forum C Programming
    Replies: 5
    Last Post: 11-05-2009, 03:34 AM
  5. Converting C++ code to C
    By alixi in forum C Programming
    Replies: 3
    Last Post: 09-21-2004, 02:23 PM

Tags for this Thread