V
V
Vladimir Yurchenkov2021-11-16 22:10:47
C++ / C#
Vladimir Yurchenkov, 2021-11-16 22:10:47

How to do alternation correctly?

Wasap!

There are input parameters (for example):
Start 11
End 18
Some name name

First of all, you need to get an array of numbers from 11 to 18

int start = 11, last = 18;
int[] arr = Enumerable.Range(start, last - start + 1).ToArray();
int count = arr.Count();


How can I do alternation by substituting values ​​from an array.

name11 name12 name13 name14 name15 name16 name17 name18
name12 name13 name14 name15 name16 name17 name18 name11 name13 name14
name15 name16 name17 name18 name11 name12 name14 name15 name16
name17 name18 name11 name12 name13 name15 name16
name17 name18 name11 name12 name13 name14 name16
name17 name18 name11 name12 name13 name14 name15
name17 name18 name11 name12 name13 name14 name15 name16
name18 name11 name12 name13 name14 name15 name16 name17
name11 name12 name13 name14 name15 name16 name17 name18
and so on.

for (int j = 0; j < count; j++)
{

for (int i = 11; i < 18 + 1; ++i)
{
TextBox.AppendText("name" + i.ToString() + " ");
}
TextBox.AppendText(Environment.NewLine);
}


So I get:
name11 name12 name13 name14 name15 name16 name17 name18
name11 name12 name13 name14 name15 name16 name17 name18 name11
name12 name13 name14 name15 name16 name17 name18
name11 name12 name13 name14 name15 name16
name17 name18 name11 name12 name13 name1
name4 name15 name16 name17 name18 name17 name18
name11 name12 name13 name14 name15 name16 name17 name18

And I need the first element to move to the end.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey〒., 2021-11-17
@EPIDEMIASH

The mod operation will help to correctly make a circular shift
How it works:
0 mod 8 = 0
1 mod 8 = 1
...
7 mod 8 = 7
8 mod 8 = 0
9 mod 8 = 1
10 mod 8 = 2
...
16 mod 8 = 0
and so on in a circle

int start = 11, last = 18;
int[] arr = Enumerable.Range(start, last - start + 1).ToArray();
for (int j = 0; j < count; j++) {
    for (int i = 0; i < count; i++) {
        Console.Write($"name{arr[(i + j) % count]} ");
    }
    Console.WriteLine();
}

https://onlinegdb.com/yLLXOKxDh

E
Ertanic, 2021-11-16
@Ertanic

If I understand correctly, then you just take, iterate over the array and connect the string with the value from the array:

//  Fix2
var count = 5;
var counter = 0;

var min = 1;
var max = 6;

do 
{
    var str = "";
    for (int i = min; i < max + 1; ++i)
        str += $"name{i} ";
    Console.Write(str + '\n');
    ++counter;
} while (counter < count);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question