Jump to content

help: undetermined integer k.

- - - - -

  • Please log in to reply
4 replies to this topic

#1
lijr07

lijr07

    Newbie

  • Members
  • Pip
  • 3 posts
I wrote a program but encounter a problem. Part of the program is
while(1)
{
part 1. lambda=...;
part 2. some codes relates to lambda; when lambda satisfies some conditions; break;
}

I would like lambda take following values (in lexicographic order, a value each time, for example, lambda=lambda, then do part 2 above, and then lambda=lambda-2a[1], then do part 2 above, and so on):
lambda,
lambda-a[1], lambda-a[2], ..., lambda-a[k-1], (the sum of coefficients of a[i] is -1)
lambda-2a[1], lambda-a[1]-a[2], lambda-a[1]-a[3], ..., lambda-a[1]-a[k-1], lambda-2a[2], lambda-a[2]-a[3], ..., lambda-a[2]-a[k-1], ..., lambda-a[k-2]-a[k-1], lambda-2a[k-1], (the sum of coefficients of a[i] is -2)
lamdba-3a[1], ... (the sum of coefficients of a[i] is -3)
...

If k is known before running the program, then it is easy. But now k is input from keyboard. It is difficult to write codes. Are there some way to do this? Thank you very much.

#2
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas
I don't understand what you mean by it's difficult with k being input from the keyboard. Parsing a number from the input stream is a relatively easy task. Could you either provide more of your code or be more specific about your question?
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.

– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#3
lijr07

lijr07

    Newbie

  • Members
  • Pip
  • 3 posts
Thank you very much.

we have to write the code before we know exact value of k. If we know k=2 for example, it is easy to write the code as:

int c[2]; //k=2
int n;

// n, a[i] are known

while(1)
{



for(c[0]=0; c[0]<n; c[0]++)
{

for(c[1]=0; c[1]<n-c[0]; c[1]++) //c[0] + c[1] = n
{
for(i=0; i<2; i++)
{
lambda=lambda-c[i]*a[i]; //this loop computes lambda-c[0]*a[0]-...-c[k-1]*a[k-1]
}


{this part have some other codes relates to lambda}

if lambda satisfies some conditions, break;


}

if lambda satisfies some conditions, break;

}

if lambda satisfies some conditions, break;

}


but now k is unknown (if k is known, we can write k loops), so I don't know how to write the codes with arbitrary k.

#4
lijr07

lijr07

    Newbie

  • Members
  • Pip
  • 3 posts
Thank you very much.

we have to write the code before we know exact value of k. If we know k=2 for example, it is easy to write the code as:

int c[2]; //k=2
int n;

// n, a[i] are known

while(1)
{



for(c[0]=0; c[0]<n; c[0]++)
{

for(c[1]=0; c[1]<n-c[0]; c[1]++) //c[0] + c[1] = n
{
for(i=0; i<2; i++)
{
lambda=lambda-c[i]*a[i]; //this loop computes lambda-c[0]*a[0]-...-c[k-1]*a[k-1]
}


{this part have some other codes relates to lambda}

if lambda satisfies some conditions, break;


}

if lambda satisfies some conditions, break;

}

if lambda satisfies some conditions, break;

}


but now k is unknown (if k is known, we can write k loops), so I don't know how to write the codes with arbitrary k.

#5
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas
I think I see what you're saying. I didn't know what you were referring to when you said "k". You are talking about declaring the maximum size of the array, c, am I right?

If you want to declare arrays of arbitrary size, you'll need to declare variable "c" as an integer pointer type instead, and, depending on whether you're working in C or C++, either use malloc() or the new operator to dynamically allocate memory from the heap. Which are you working in, C/C++?
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.

– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users