Ninjas Edits Ninjas Edits - 20 days ago 9
C# Question

C# array combination

I'm working on my homework. I got the code so far, but on the last line, I need to combine both input values. It works somehow, then an error error occurs. I think the problem is in the last

for
loop. Here's the code, and btw the text in the code is in Latvian.

Console.Write("Ievadiet pirma masīva izmeru: ");
int first = Convert.ToInt32(Console.ReadLine());
string[] n = new string[pirmais];

Console.Write("Ievadiet pirma masīva izmeru: ");
int second = Convert.ToInt32(Console.ReadLine());
string[] m = new string[otrais];



for (int i = 0; i < n.Length; i++)
{
Console.Write("Ievadiet 1. masiva {0} vertibu: ",i);

n[i] = Console.ReadLine();
}

for (int j = 0; j < m.Length; j++)
{
Console.Write("Ievadiet 2. masiva {0} vertibu: ", j);

m[j] = Console.ReadLine();
}
for (int i = 0; i < n.Length; i++)
{
Console.WriteLine("1. masiva {0} vertiba ir: " + n[i], i);
}
for (int j = 0; j < m.Length; j++)
{
Console.WriteLine("2. masiva {0} vertiba ir: "+m[j],j);
}

Console.Write("1. un 2. masīva apvienotā simbolu virkne: ");
for (int i = 0; i < n.Length+m.Length; i++)
{
Console.WriteLine(n[i] + m[i]);
}

Answer

There are many problems in your code. First you should post question in English not in other languages. Second you are trying to access the item of array beyond its range.

You can do in the following way.

    Console.Write("Ievadiet pirma masīva izmeru: ");
    int first = Convert.ToInt32(Console.ReadLine());
    string[] n = new string[first];

    Console.Write("Ievadiet pirma masīva izmeru: ");
    int second = Convert.ToInt32(Console.ReadLine());
    string[] m = new string[second];

    for (int i = 0; i < n.Length; i++)
    {
        Console.Write("Ievadiet 1. masiva {0} vertibu: ", i);

        n[i] = Console.ReadLine();
    }

    for (int j = 0; j < m.Length; j++)
    {
        Console.Write("Ievadiet 2. masiva {0} vertibu: ", j);

        m[j] = Console.ReadLine();
    }
    for (int i = 0; i < n.Length; i++)
    {
        Console.WriteLine("1. masiva {0} vertiba ir: " + n[i], i);
    }
    for (int j = 0; j < m.Length; j++)
    {
        Console.WriteLine("2. masiva {0} vertiba ir: " + m[j], j);
    }

    Console.Write("1. un 2. masīva apvienotā simbolu virkne: ");

    if (n.Length < m.Length)
    {
        for (int i = 0; i < n.Length; i++)
        {
            Console.WriteLine(n[i] + m[i]);
        }
        for (int i = n.Length; i < m.Length; i++)
        {
            Console.WriteLine(m[i]);
        }

    }
    else if (n.Length > m.Length)
    {
        for (int i = 0; i < m.Length; i++)
        {
            Console.WriteLine(n[i] + m[i]);
        }
        for (int i = m.Length; i < n.Length; i++)
        {
            Console.WriteLine(n[i]);
        }
    }
    else
    {
        for (int i = 0; i < n.Length; i++)
        {
            Console.WriteLine(n[i] + m[i]);
        }
    }