Mute
Mute
CC#
Created by Mute on 3/9/2024 in #help
The program should connect 2 sorted arrays into 1 sorted array
Thank you
10 replies
CC#
Created by Mute on 3/9/2024 in #help
The program should connect 2 sorted arrays into 1 sorted array
Random random = new Random();
int N = 7;// random.Next(5, 10);
int M = 6;// random.Next(5, 10);
//int[] A = new int[N];
//int[] B = new int[M];
int[] A = { 1, 2, 5, 6, 10, 11, 13 };
int[] B = { 3, 8, 12, 15, 20, 30 };

//for (int i = 0; i < N; i++)
//{
// A[i] = random.Next(-30,50);
//}
//for (int i = 0; i < M; i++)
//{
// B[i] = random.Next(-30, 50);
//}

Array.Sort(A);
Array.Sort(B);

Console.WriteLine("Первый отсортированный массив");
int i;

for (i = 0; i < N; i++)
{
Console.Write(A[i] + "\t");
}

Console.WriteLine();

Console.WriteLine("Второй отсортированный массив");
for (i = 0; i < M; i++)
{
Console.Write(B[i] + "\t");
}
int Y = M + N;
int[] C = new int[Y];
int aIndex = 0;
int bIndex = 0;

Console.ReadLine();
int f1 = 0, f2 = 0;
for (i = 0; i < Y && (f1 == 1 || f2 == 1); i++)
{
if (A[aIndex] > B[bIndex])
{
if (bIndex < M)
{
C[i] = B[bIndex];
bIndex++;

}
else if (bIndex > M)
{
f2 = 1;
}


}
else
{
if (aIndex < N)
{
C[i] = A[aIndex];
aIndex++;
}
else if (aIndex > N)
{
f1 = 1;
}

}
}
if (f1 == 1)
{
for (int g = i; g < Y; g++)
{
C[g] = B[bIndex];
}
}
if (f2 == 1)
{
for (int t = i; t < Y; t++)
{
C[t] = A[aIndex];
}
}


Console.WriteLine();
Console.WriteLine();
Console.WriteLine();
for (i = 0; i < Y; i++)
{
Console.Write(C[i] + "\t");
}
Console.WriteLine();
// code here
Random random = new Random();
int N = 7;// random.Next(5, 10);
int M = 6;// random.Next(5, 10);
//int[] A = new int[N];
//int[] B = new int[M];
int[] A = { 1, 2, 5, 6, 10, 11, 13 };
int[] B = { 3, 8, 12, 15, 20, 30 };

//for (int i = 0; i < N; i++)
//{
// A[i] = random.Next(-30,50);
//}
//for (int i = 0; i < M; i++)
//{
// B[i] = random.Next(-30, 50);
//}

Array.Sort(A);
Array.Sort(B);

Console.WriteLine("Первый отсортированный массив");
int i;

for (i = 0; i < N; i++)
{
Console.Write(A[i] + "\t");
}

Console.WriteLine();

Console.WriteLine("Второй отсортированный массив");
for (i = 0; i < M; i++)
{
Console.Write(B[i] + "\t");
}
int Y = M + N;
int[] C = new int[Y];
int aIndex = 0;
int bIndex = 0;

Console.ReadLine();
int f1 = 0, f2 = 0;
for (i = 0; i < Y && (f1 == 1 || f2 == 1); i++)
{
if (A[aIndex] > B[bIndex])
{
if (bIndex < M)
{
C[i] = B[bIndex];
bIndex++;

}
else if (bIndex > M)
{
f2 = 1;
}


}
else
{
if (aIndex < N)
{
C[i] = A[aIndex];
aIndex++;
}
else if (aIndex > N)
{
f1 = 1;
}

}
}
if (f1 == 1)
{
for (int g = i; g < Y; g++)
{
C[g] = B[bIndex];
}
}
if (f2 == 1)
{
for (int t = i; t < Y; t++)
{
C[t] = A[aIndex];
}
}


Console.WriteLine();
Console.WriteLine();
Console.WriteLine();
for (i = 0; i < Y; i++)
{
Console.Write(C[i] + "\t");
}
Console.WriteLine();
// code here
10 replies