C
C#9mo ago
Mute

The program should connect 2 sorted arrays into 1 sorted array

The program should connect 2 sorted arrays into 1 sorted array (and sorting should be done during connection). The error of my program is that it outputs zeros instead of a connected, sorted array.
No description
No description
6 Replies
SinFluxx
SinFluxx9mo ago
$code
MODiX
MODiX9mo ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat For longer snippets, use: https://paste.mod.gg/
Mute
MuteOP9mo ago
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
SinFluxx
SinFluxx9mo ago
int f1 = 0, f2 = 0;
for (i = 0; i < Y && (f1 == 1 || f2 == 1); i++)
int f1 = 0, f2 = 0;
for (i = 0; i < Y && (f1 == 1 || f2 == 1); i++)
f1 and f2 are initialised to 0. So (f1 == 1 || f2 == 1) will evaluate to false straight away. It's a bit hard to follow what you're trying to do with all the single character variable names.
Kaos
Kaos9mo ago
it seems like you are trying to track if one of the arrays are finished but, you don't actually need those variables You could do something like this
using System;

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;
i = 0;

Console.WriteLine();
while( aIndex < A.Length && bIndex < B.Length)
{
if (A[aIndex] > B[bIndex])
{
C[i] = B[bIndex];
bIndex++;
i++;
}
else
{
C[i] = A[aIndex];
aIndex++;
i++;
}
}

while(aIndex < A.Length)
C[i++] = A[aIndex++];
while(bIndex < B.Length)
C[i++] = B[bIndex++];


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

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;
i = 0;

Console.WriteLine();
while( aIndex < A.Length && bIndex < B.Length)
{
if (A[aIndex] > B[bIndex])
{
C[i] = B[bIndex];
bIndex++;
i++;
}
else
{
C[i] = A[aIndex];
aIndex++;
i++;
}
}

while(aIndex < A.Length)
C[i++] = A[aIndex++];
while(bIndex < B.Length)
C[i++] = B[bIndex++];


Console.WriteLine();
Console.WriteLine();
Console.WriteLine();
for (i = 0; i < Y; i++)
{
Console.Write(C[i] + "\t");
}
Console.WriteLine();
// code here
since I know that the a index and the b index are going to be less than C's length, I can break as soon as one of those is finished. Then I can add whatever the remaining one has left in it. If A is exhausted, then aIndex < A.Length will be false and that else will be skipped. Then it will add the rest of what is in B. You can shorten the body of the if/else incrementing to be like my while statements, or expand the while statement, but they should be consistent either way. If you have any questions, feel free to ping
Mute
MuteOP9mo ago
Thank you
Want results from more Discord servers?
Add your server