1

I have the following two loops in my program and I am trying to write them out as math equations, but I'm having some difficulty finding a concise way to do so:

// Loop1
for (int i = 0; i < nr; i++) {
    array[i] = nepr;
}

// Loop2
for (int i = 0; i < (nvr % nr); i++) {
    array[i]++;
}

The code is completely function, but I'm trying to express these loops in a document I'm writing.

Any help would be very much appreciated.

So far I have this:

array[i] = nepr, i = 0,...,(nr-1)

but I'm not sure how to incorporate the second loop into the equation, or write a second equation for it.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
Veridian
  • 3,531
  • 12
  • 46
  • 80

5 Answers5

3

Just express it as a piecewise-defined function:

 f(x) = {nepr + 1 if x <  (nvr % xr)}  
        {nepr     if x >= (nvr % xr)}

You can also define it in terms of the Heaviside step function:

 f(x) = nepr + H((nxr % xr) - x)
SirGuy
  • 10,660
  • 2
  • 36
  • 66
  • I believe your notation doesn't take into account that x is discretely valued. Your equations are for x being continuous. Correct me if I'm wrong. – Veridian Jul 06 '16 at 18:30
  • @Veridian That's not directly necessary, the equations are still valid. If you want to express that x must be an integer, just state it separately. – SirGuy Jul 06 '16 at 18:32
  • @chux It seems likely that nvr is always positive, but I changed it anyway. – SirGuy Jul 06 '16 at 19:10
  • Yes, assuming `nvr, nr` positive does simplify the problem. Doubt if OP wants a full range `int` solution. – chux - Reinstate Monica Jul 06 '16 at 19:16
1

Not entirely clear that's what you are asking, but the following does the same thing in a single loop.

for (int i = 0, j = nvr % nr; i < nr; i++) {
    array[i] = nepr + (i < j);
}


[EDIT] In casual language without any loops the above is equivalent to...

Let j = nvr % nr. Assuming positive nr and nvr, 0 <= j < nr and:

  • array[i] = nepr + 1 for i = 0 ... j-1
  • array[i] = nepr for i = j ... nr-1
dxiv
  • 16,984
  • 2
  • 27
  • 49
0

In your second loop, the condition check has the nr-1 as abound.

If i understood your problem correctly, you can express it like

array[i] = array[i] + 1 , i = nvr % nr ,...,(nr-2)

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
0

Here it is in LaTeX. To see or edit the LaTeX, click on the equation.

latex equation

The := symbol expresses assignment. You could also write it in functional fashion by replacing array_i with array(i), a function defined on an open range of integers.

Community
  • 1
  • 1
Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313
0

Thought I would try to post a full int range soukltion base on @dxiv fine answer which works well when nr, nvr are positive - the usual case.

Assume nr, nvr are int.

assert(nr != 0);
assert(nr != -1 || nvr >= -INT_MAX);  // avoid -2147483648 % -1
int j = nvr % nr;
int ij = max(i,j);
for (int i = 0; i < ij; i++) {
    array[i] = nepr + (i < j);
}
Community
  • 1
  • 1
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256