简体   繁体   中英

How to create an array of variables generated within a loop in C programming

Within C programming,

I have an outer loop that cycles through index i=1,...,N. Within this loop there is another loop that for each i produces a variable x. For different values of i, x will change. How do I store these values before the next update of the index i, such that at the end of the i loop, I have a set of x values, one for each value of i, that I can then form into an array?

for(i=0,i<=N,i++)
{ 
{loop that produces a value of variable x for a given value of i};
};

Later in my code I wish to use an array of each value of x for a given value of i, x[i] .

I am very new to programming so your help is very much appreciated, thank you, apologies if it is a trivial thing, it is non-trivial to me I assure you.

Later in my code I wish to use an array of each value of x for a given value of i, x[i].

That's the answer right there -- make x an array instead of a single value, so that you can store different values for each i :

int x[N];
for(i=0,i<N,i++)
{
    x[i] = i^2;  // some computation of the ith value of x, i^2 just an example
}

Now x is an array of N elements, each with some value calculated in the i th iteration of the loop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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