简体   繁体   English

C 函数从按递增顺序排序的数组中返回大于给定数字的最小数字的索引

[英]C function that returns index of smallest number greater than a given number from an array sorted in increased order

I am translating some code from Python to deepen my understanding of programming.我正在从 Python 翻译一些代码,以加深我对编程的理解。 I have got an array P_array with 1000 floating point numbers that are increscent.我有一个数组 P_array,其中包含 1000 个递增的浮点数。 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.我的任务是编写一个函数,该函数将返回 P_array[x] 大于 720 的第一个实例的索引。这是 python 和 C 代码,我在最后三行 python 代码中卡住了。

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);
}

regarding: *C function that returns index of smallest number greater than given from increscent array`关于:*C 函数返回大于递增数组给出的最小数字的索引

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;
        }
    }
}

thanks user3629249, this is my final code for loop, and it works perfectly.感谢 user3629249,这是我的最终循环代码,它运行良好。

    "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);"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在排序列表中查找大于给定数字的最小数字 - Find the smallest number that is greater than a given number in a sorted list 从排序列表中获取大于给定数字的第一个元素 - Get first element greater than a given number from a sorted list Function 必须返回大于等于的最小偶数的列表,按升序排列 - Function must return a list of the smallest even integers that are greater than or equal to, sorted into ascending order 在没有内置排序的情况下从给定列表中排序数字列表,如果第一个数字大于第二个数字则复制它们交换它们 - Order a list of numbers from given list without built-in sort, copy if first number is greater than second number swap them 有效地查找数量最小的索引,该索引大于大型排序列表中的某个值 - Efficiently find index of smallest number larger than some value in a large sorted list 算法(Python):找到大于k的最小数 - Algorithm (Python): find the smallest number greater than k 最小相邻像元的索引大于2D数组中的值 - Index of smallest neighbouring cell greater than a value in 2D array Python 给定一个N个整数的数组A,在O(n)时间复杂度内返回A中没有出现的最小正数integer(大于0) - Python given an array A of N integers, returns the smallest positive integer (greater than 0) that does not occur in A in O(n) time complexity 给定一个包含 N 个整数的数组 A,返回在 O(n) 时间复杂度内不会出现在 A 中的最小正 integer(大于 0)(Python Sol) - Given an array A of N integers, returns the smallest positive integer (greater than 0) that does not occur in A in O(n) time complexity (Python Sol) 在正则表达式中查找大于给定参数的数字 - Find number greater than given parameter in regex
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM