-2

I am translating some code from Python to deepen my understanding of programming. I have got an array P_array with 1000 floating point numbers that are increscent. My task is to write a function that will return index of the first instance where P_array[x] is greater than 720. Here is both python and C code, I got stuck at last three lines of python code.

import numpy as np
import matplotlib.pyplot as plt

# Initializations

Dt = 1/32                              # timestep Delta t
P_init= 30                             # initial population
t_init = 0                              # initial time
t_end = 30                               # stopping time
n_steps = int(round(t_end/Dt))

t_array = np.zeros(n_steps+1)
P_array = np.zeros(n_steps+1)
t_array[0] = t_init
P_array[0] = P_init

#Eulers method

for i in range (1, n_steps + 1):
    P = P_array[i-1]
    t = t_array[i-1]
    dPdt = 0.7 * P * (1-(P/750)) - 20
    P_array[i] = P + Dt * dPdt
    t_array[i] = t + Dt

index = np.where(P_array>=720)

x = ([x[0] for x in index])
print (x)

C code

int main() {
    int i, j, x = 1;
    float dt, P_init, t_init, t_end;

    dt = 0.03125;
    P_init = 30;
    t_init = 0;
    t_end = 30;

    int n_steps = 0;
    n_steps = t_end/(float)dt;

    float Parray[n_steps+1];
    float Tarray[n_steps+1];

    for (i=0; i<n_steps+1; i++) {
       Parray[i]=0;
       Tarray[i]=0;
    }

    Parray[0] = P_init;
    Tarray[0] = t_init;

    float P,t,dpdt,s,d;

    while (x < n_steps+1) {
        P = Parray[x-1];
        t = Tarray[x-1];
        dpdt = 0.7 * P * (1-(P/750)) - 20;
        s = P + (dt * dpdt);
        Parray[x] = s;
        d = t + dt;
        Tarray[x] = d;
        x++;
        printf("%f  %f \n ",s,d);
    }

    return(0);
}
Javier Silva Ortíz
  • 2,864
  • 1
  • 12
  • 21
dransy
  • 17
  • 4
  • 2
    “Increscent” isn’t a normal English word. Do you mean “sorted in increasing order”? – Jonathan Leffler Nov 23 '19 at 18:16
  • 1
    @JonathanLeffler, well, [actually](https://www.collinsdictionary.com/dictionary/english/increscent)... But it says it mostly refers to the moon, so probably doesn't fit that well here, but it should still be valid – ForceBru Nov 23 '19 at 18:20
  • 2
    Tarik Sidran, "I got stuck at last three lines of python code." is vague. What is the issue? Are you looking for someone to code `index = np.where(P_array>=720)` in C for you? – chux - Reinstate Monica Nov 23 '19 at 18:24
  • OT: ` increscent` means to progressively be brighter, for instance during the `waxing` phase of the moon more and more of the moon is lighted – user3629249 Nov 23 '19 at 18:32
  • The most efficient way to do it is with a [binary search](https://en.wikipedia.org/wiki/Binary_search_algorithm). The easiest way to do it is with a [linear search](https://en.wikipedia.org/wiki/Linear_search). – user3386109 Nov 23 '19 at 18:36
  • OT: regarding: `dt = 0.03125;` this is jamming a `double` literal into a `float` variable. Suggest: `dt = 0.03125f;` Note; the trailing `f` – user3629249 Nov 23 '19 at 18:36
  • 1
    OT: regarding; `n_steps = t_end/(float)dt;` why the cast to `float` when that variable is declared, already, as a `float`? – user3629249 Nov 23 '19 at 18:38
  • 2
    Instead of `float` in C, use [`double`](https://stackoverflow.com/a/34960770/2410359) – chux - Reinstate Monica Nov 23 '19 at 18:42
  • 1
    OT: regarding: `dpdt = 0.7 * P * (1-(P/750)) - 20;` the `dpdt` is a `float` the `0.7` is a `double` the `750` and the `20` and the `1` are integers. All those implicit conversions are a potential problem. Suggest: `dpdt = 0.7f * P * (1.0f-(P/750.0f)) - 20.0f;` – user3629249 Nov 23 '19 at 18:45
  • OT: regarding: `Parray[i]=0;` and `Tarray[i]=0;` These statements are trying to stuff a `int` value into a `float`. Suggest: `Parray[i]=0.0f;` and `Tarray[i]=0.0f;` – user3629249 Nov 23 '19 at 18:48

2 Answers2

0

regarding: *C function that returns index of smallest number greater than given from increscent array`

the following proposed code:

  1. performs the desired functionality

and now, the proposed code:

// C function that returns index of smallest number greater than given from increscent array

#include <stdio.h>

int main( void )
{
    //
    // assume code to generate array, sorted ascending
    size_t arraySize;
    float sortedArray[ arraySize ];
    //

    //
    // assume code to input target value
    float targetValue;
    //

    for( size_t i=0; i<arraySize; i++ )
    {
        if( targetValue > sortedArray[i] )
        {
            printf( "index into array = %zu\n", i );
            break;
        }
    }
}
user3629249
  • 16,402
  • 1
  • 16
  • 17
0

thanks user3629249, this is my final code for loop, and it works perfectly.

    "double n = 720.00;
     int m = 0;

     for( j=0; j <n_steps+1; j++ ){

         if( n < Parray[j]){
             m = j;
             break;
         }
     }

      printf("%d\n", m);"
dransy
  • 17
  • 4