简体   繁体   English

使用循环外的变量,值在循环内变化

[英]Using a variable outside of a loop, value changes within the loop

I am relatively new to programming and I have come across an issue whilst trying to write a function that utilizes loops. 我是编程新手,在尝试编写利用循环的函数时遇到了一个问题。 The function should work as such: 该函数应这样工作:

The array Data consists of a 51X2 set of doubles, which are all used in the program through the initial for loop, the a variable and the b variable. 数据数组由一组51X2的双精度数组成,它们在程序中都通过初始for循环,a变量和b变量使用。 The X and Y variables are both set to the minimum values of the array. X和Y变量都设置为数组的最小值。

Using the a and X variables as an example, the X value is compared to a and incremented until it surpasses a. 以a和X变量为例,将X值与a进行比较,并递增直到它超过a。 The number of loop iterations are tracked by the c variable. 循环迭代次数由c变量跟踪。 This c variable is then used in the Graph array. 然后在Graph数组中使用此c变量。 The same procedure occurs for the b and Y variables. b和Y变量发生相同的过程。 This process is repeated for every value of S to analyse all data points from the Data array. 对S的每个值重复此过程,以分析数据数组中的所有数据点。

The issue I'm having is that the c and d variables don't change relative to the changes inside the loops. 我遇到的问题是cd变量相对于循环内的更改不会更改。 The variables will not change from their initialized value. 变量将不会从其初始值更改。 I am looking to find a solution that allows for the c and d variables to change in relation to the number of iterations of the for loop. 我正在寻找一种解决方案,该解决方案允许cd变量根据for循环的迭代次数进行更改。

The relevant function code can be seen below: 相关功能代码如下所示:

void Data_Plot(double Data[51][2], char Graph[44][56])
{
    int N = 50;
    int S,q,r;
    int c = 0;
    int d = 0;
    double a = Data[S][0];
    double b = Data[S][1];
    double X = Data[0][0];
    double Y = Data[0][1];
    for (S=0;S<N;S++)
    {   
        for(X;X<a;X+=0.1428571429)
        {
            c++;
        }
        for(Y;Y<b;Y+=2)
        {
            d++;
        }

        Graph[c][d] = '*'; 
    }

I am aware that my code is very unoptimized and messy, but I can fix those issues up with future projects after I have finished this one. 我知道我的代码没有经过优化和凌乱,但是我可以在完成以后的项目中解决这些问题。

Edit: I would like to note that I have attempted this with c and d being set to other values, as well as being left as NULL. 编辑:我想指出的是,我尝试将cd设置为其他值,以及将其保留为NULL。 The same result occurred regardless of the variable initialization. 无论变量初始化如何,都会产生相同的结果。

Since a and b depend on S , which you change during the for loop, you need to move those variables inside the loop. 由于ab取决于S ,您需要在for循环中更改S ,因此您需要在循环内移动这些变量。

void Data_Plot(double Data[51][2], char Graph[44][56])
{
    int N = 50;
    int S,q,r;
    int c = 0;
    int d = 0;
    double X = Data[0][0];
    double Y = Data[0][1];
    for (S=0;S<N;S++)
    {   
        double a = Data[S][0];
        for(X;X<a;X+=0.1428571429)
        {
            c++;
        }

        double b = Data[S][1];
        for(Y;Y<b;Y+=2)
        {
            d++;
        }

        Graph[c][d] = '*'; 
    }

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM