P
P
PurgaBot2020-11-22 12:13:25
C++ / C#
PurgaBot, 2020-11-22 12:13:25

Fill in the numbers and symbols in order with the help of stat. method (from the 1st argument to the 2nd argument). Why in the code is not a slave. nums[k]=nums[k-1]++;?

You need to fill in the numbers and symbols in order using the static method from the first argument to the second argument. Why does it not work in the code (nums[k]=nums[k-1]++;), but it works (nums[k]=nums[k-1]+1;)

Here is the code:

static int[] zuma(int a, int b)
        {
            int[] nums = new int[b-a+1];
            nums[0] = a;
            for (int k = 1; k<nums.Length-1;k++)
            {
                nums[k]=nums[k-1]+1;
            }
            nums[nums.Length - 1] = b;
            return nums;
        }
        static char[] zuma(char a, char b)
        {
            int q = b-a; 
            char[] nums = new char[q+1];
            nums[0] = a;
            for (int k = 1; k < nums.Length-1; k++)
            {
                nums[k] = (char)(nums[k - 1]+1);
            }
            nums[nums.Length - 1] = b;
            return nums;
        }
        static void Main()
        {
            int[] A;
            char[] B;
            A = zuma(3, 8);
            B = zuma('B', 'F');
            for (int k = 0; k<A.Length;k++)
            {
                Console.Write(A[k]+" ");
            }
            Console.WriteLine();
            for (int k = 0; k < B.Length; k++)
            {
                Console.Write(B[k] + " ");
            }
            Console.ReadKey();
        }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vasily Bannikov, 2020-11-22
@vabka

Something some terribly scary code you have.
The code works fine (345678 and bcdef are output to the console)

My version of the code

1. Нормально именуем сущности в коде.
2. Убираем лишний код.
3. Добавляем проверку входных аргументов
using System;

var intArray = CreateArrayFromIntRange(3, 8);
var charArray = CreateArrayFromCharRange('B', 'F');

Console.WriteLine(string.Join(' ', intArray));
Console.WriteLine(string.Join(' ', charArray));

static int[] CreateArrayFromIntRange(int start, int end)
{
    if(start > end)
        throw new ArgumentException("start must be less or equal to end");

    var length = end - start + 1;
    var buffer = new int[length];

    var current = start;
    for (var i = 0; i < buffer.Length; i++)    
        buffer[i] = current++;

    return buffer;
}

static char[] CreateArrayFromCharRange(char start, char end)
{
    if(start > end)
        throw new ArgumentException("start must be less or equal to end");
    
    var length = end - start + 1;
    var buffer = new char[length];
    
    var current = start;    
    for (var i = 0; i < buffer.Length; i++)    
        buffer[i] = current++;
    
    return buffer;
}


nums[k]=nums[k-1]++Won't work the way you expect - Arrays are indexed by reference, not by value. For example, this will not happen with List, because there an indexer is a method that returns a value by index, and not a reference to an element.
PS: If this is a school / college / university assignment, then first understand the code that I wrote before copying it

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question