Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1086229
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:47:13+00:00 2026-05-16T22:47:13+00:00

I am trying to convert this C code I have into a python script

  • 0

I am trying to convert this C code I have into a python script so it’s readily accessible by more people, but I am having problems understanding this one snippet.

int i, t;
for (i = 0; i < N; i++) {
   t = (int)(T*drand48());
   z[i] = t;
   Nwt[w[i]][t]++;
   Ndt[d[i]][t]++;
   Nt[t]++;
}

N is a value (sum of one column from an array. Elemental corrected me).

T is just a numerical value.

z, w, and d are memory allocations created from the N array. They were created with this method.

w = ivec(N);
d = ivec(N);
z = ivec(N);

int *ivec(int n) //
{
   int *x = (int*)calloc(n,sizeof(int));
   assert(x);
   return x;
}

Nwt & Ndt are both arrays too, with each element being a memory allocation? (Not sure). At least, each one of them was created by using the following method, passing in two different int’s.

Nwt = dmat(W,T);
Ndt = dmat(D,T);

double **dmat(int nr, int nc) //
{
   int N = nr*nc;
   double *tmp = (double*) calloc(N,sizeof(double));
   double **x  = (double**)calloc(nr,sizeof(double*));
   int r;
   assert(tmp);
   assert(x);
   for (r = 0; r < nr; r++) x[r] = tmp + nc*r;
   return x;
}

So looking at the first loop I posted, what are the following lines doing? I would like to accomplish the same thing in python, but since no memory allocation is needed, not sure what those three lines do, or how I would duplicate it in python.

Nwt[w[i]][t]++;
Ndt[d[i]][t]++;
Nt[t]++;

This is what I have so far:

for i in range(self.N):
        t = self.T * random.random()
        self.z[i] = t
        //** INCORRECT BELOW **
        //self.Nwt[self.N[i]] = t + 1 
        //self.Ndt[i] = t + 1
        //self.Nt[t + 1] += 1
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T22:47:13+00:00Added an answer on May 16, 2026 at 10:47 pm

    In your translation, the first thing I would worry about is making sensical variable names, particularly for those arrays. Regardless, much of that translates directly.

    Nwt and Ndt are 2D arrays, Nt is a one dimensional array. It looks like you’re looping over all the ‘columns’ in the z array, and generating a random number for each one. Then you increment whichever column was picked in Nwt (row w[i]), Ndt (row d[i]) and Nt. The actual random value is stashed in z.

    #Literal translation
    for i in range(N):
        t = Random.randint(0,T) #Not sure on this... but it seems likely.
        z[i] = t
        Nwt[w[i]][t] += 1
        Ndt[d[i]][t] += 1
        Nt[t] += 1
    
    #In place of w= ivec(N);
    w = [0]*N
    d = [0]*N
    z = [0]*N
    
    #In place of Nwt = dmat(W,T)
    Nwt = [[0.0] * T] * W
    Ndt = [[0.0] * T] * D
    

    EDIT: corrected w/d/z initialization from “n” to “N”

    Note that there are still some things wrong here, since it looks like N must equal W, and D… so tread carefully.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to convert my shell scripts into python code, but I am
Trying to convert this c code into MIPS and run it in SPIM. int
I'm trying to convert this code fragment into Delphi and I got stuck on
I am using this code for on android just trying to convert string to
I am trying to convert this to python. I just really need help with
I'm trying to convert this line into Batch how would one start this exe
I'm trying to convert this 2012-03-27 16:01:48 CEST into 1 hour and 22 minutes
im trying to convert a date format into a new format in this example
I'm trying to convert an int into a byte in Processing 1.0.9. This is
I am trying to convert this piece of html code to javascript so that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.