对于循环不会从负面开始

时间:2021-10-26 16:50:52

So I need to make an array which I can reference, from [-2:25]. If I start at 0 the code works just fine, but I need to perform a low pass filter so I need the -2 and -1 values of x(n) as well and I need to be able to reference it like A[-2]

所以我需要制作一个我可以参考的数组,从[-2:25]开始。如果我从0开始,代码工作得很好,但我需要执行一个低通滤波器,所以我需要x(n)的-2和-1值,我需要能够像A [ - 一样引用它2]

Code:

#include <stdio.h>
#include <math.h>
#include <complex.h>
int main(void)
{
    int N=25;          //Setting Number of Iterations
    int i;              //Initializing Variables
    double xn;  //
    double hn;
    double A[27];

    FILE *fp;
    fp=fopen("data3.txt","w");      //Save to File

    do { i=-2;i<N;i++;

    { xn = 2*exp(-0.01*i)*sin(0.2*i);
        A[i] = xn;}
    } while (i>0);

     (i=0;i<N;i++)      //For loop for i=
    {
        xn = 2*exp(-0.01*i)*sin(0.2*i);
        A[i] = xn;
        fprintf(fp,"%d\t%f\t%f\n",i,xn,A[i]); //Saving results to File
         printf("xn= %d\t%f\n",i, A[i]);


    }
    printf("xn1 = %f\n", A[1]);
    printf("xn5 = %f\n", A[5]);



    return(0);

3 个解决方案

#1


4  

This makes little sense. A[-2] is undefined behavior. Trying to assign to it probably overwrites other variables. If A has 27 elements the indices are 0 - 26. Use A[i+2] wherever you have A[i] in your code.

这没什么意义。 A [-2]是未定义的行为。尝试分配给它可能会覆盖其他变量。如果A有27个元素,则索引为0 - 26.如果代码中有A [i],请使用A [i + 2]。

#2


1  

An array is a pointer, and you can do p - 2 just fine. However, you need to manage your indexes carefully.

数组是一个指针,你可以做p - 2就好了。但是,您需要仔细管理索引。

What I'd suggest you do is create two variables. The first is your array A. Size it to be the number of elements, not the indexes: if your range is -2 to 25, then that should be an array of length 27 (25 - -2).

我建议你做的是创建两个变量。第一个是你的数组A.将它的大小设置为元素的数量,而不是索引:如果你的范围是-2到25,那么它应该是一个长度为27(25 - -2)的数组。

Then, create a second variable P, which would be:

然后,创建第二个变量P,它将是:

double *P = &A[2];

Then you can do P[-2], which would point to the address of A[0].

然后你可以做P [-2],这将指向A [0]的地址。

#3


0  

Learn your loop constructions. Equivalently:

学习你的循环结构。等价的:

// (init; break condition; step)
for (i = ...; i < N; ++i) {
    ... // do stuff
}

int i = ...; // init
if(i < N) { // initial check (may be unnecessary by construction)
    do {
        ... // do stuff
        ++i; // step
    } while(i < N); // break condition
}

int i = ...; // init
while(i < N) { // break condition
    ... // do stuff
    ++i; // step
}

#1


4  

This makes little sense. A[-2] is undefined behavior. Trying to assign to it probably overwrites other variables. If A has 27 elements the indices are 0 - 26. Use A[i+2] wherever you have A[i] in your code.

这没什么意义。 A [-2]是未定义的行为。尝试分配给它可能会覆盖其他变量。如果A有27个元素,则索引为0 - 26.如果代码中有A [i],请使用A [i + 2]。

#2


1  

An array is a pointer, and you can do p - 2 just fine. However, you need to manage your indexes carefully.

数组是一个指针,你可以做p - 2就好了。但是,您需要仔细管理索引。

What I'd suggest you do is create two variables. The first is your array A. Size it to be the number of elements, not the indexes: if your range is -2 to 25, then that should be an array of length 27 (25 - -2).

我建议你做的是创建两个变量。第一个是你的数组A.将它的大小设置为元素的数量,而不是索引:如果你的范围是-2到25,那么它应该是一个长度为27(25 - -2)的数组。

Then, create a second variable P, which would be:

然后,创建第二个变量P,它将是:

double *P = &A[2];

Then you can do P[-2], which would point to the address of A[0].

然后你可以做P [-2],这将指向A [0]的地址。

#3


0  

Learn your loop constructions. Equivalently:

学习你的循环结构。等价的:

// (init; break condition; step)
for (i = ...; i < N; ++i) {
    ... // do stuff
}

int i = ...; // init
if(i < N) { // initial check (may be unnecessary by construction)
    do {
        ... // do stuff
        ++i; // step
    } while(i < N); // break condition
}

int i = ...; // init
while(i < N) { // break condition
    ... // do stuff
    ++i; // step
}