C
C#3y ago
Triufelis

Connect together [Answered]

Have one file that has: 1 2 3 6 7 8 9 11 12 13 Other one is: 3 4 5 6 9 10 11 Need to connect them in an increasing order. code attached. says that index is outside bounds of the array. where might the problem be?
15 Replies
Triufelis
TriufelisOP3y ago
i don't really see how the index2 increases. Because in the first if statement. the one where i compare words1 to words2[index2]. I only go to the else if the condition isn't met. since index2 is set to 0 it doesn't go into the while loop until the third iteration. alright, i'm gonna get thinking ty!
mtreit
mtreit3y ago
What is the desired output in the example you gave? For instance, should duplicates be eliminated?
Triufelis
TriufelisOP3y ago
keep the duplicates so the answer would be 1 2 3 3 4 5 6 6 7 8 9 9 10 11 11 12 13
mtreit
mtreit3y ago
So you just need to merge the two arrays and sort them...I think your approach seems very complicated and you should step back and think of how to simplify it.
Triufelis
TriufelisOP3y ago
the problem is that this is not a simple sorting alogirthm. its just a primitive version of what the end product needs to be
mtreit
mtreit3y ago
I don't know what that means.
Triufelis
TriufelisOP3y ago
alright, i'm gonna elaborate i need to read from the first file until i find a duplicate number in this instance, then start printing from the other file, until i find a duplicate in the first one i need to do this with words e.x with words would be file1: my name is john; file2: is this the name the output would be: my name is this the name john
mtreit
mtreit3y ago
Well, in the example you gave using numbers your two inputs and your described desired output is literally "concat and sort".
var first = new int[] { 1, 2, 3, 6, 7, 8, 9, 11, 12, 13 };
var second = new int[] { 3, 4, 5, 6, 9, 10, 11 };

var joinedAndSorted = first.Concat(second).OrderBy(x => x);

foreach (var item in joinedAndSorted)
{
Console.Write(item);
Console.Write(" ");
}
var first = new int[] { 1, 2, 3, 6, 7, 8, 9, 11, 12, 13 };
var second = new int[] { 3, 4, 5, 6, 9, 10, 11 };

var joinedAndSorted = first.Concat(second).OrderBy(x => x);

foreach (var item in joinedAndSorted)
{
Console.Write(item);
Console.Write(" ");
}
[10:48:16] ✓ dotnet run
1 2 3 3 4 5 6 6 7 8 9 9 10 11 11 12 13
[10:48:16] ✓ dotnet run
1 2 3 3 4 5 6 6 7 8 9 9 10 11 11 12 13
Your second description with words, I didn't really follow.
Triufelis
TriufelisOP3y ago
Alright. I will try to make myself a bit clearer. I have two files. In one file, lets call it File1.txt there is a line of text. f.e. My name is John. In the second file, lets call it File2.txt there's also a line of text. F.e. Is this my name. I need to copy the first file until i find the first match. So it would go like this My => name => is(this is the point where i need to switch files, as i have found a duplicate) =>is => this => my => name(since the second file ended i now need to print the rest of file1) =>John
mtreit
mtreit3y ago
That seems quite different than your number example...
Triufelis
TriufelisOP3y ago
Not necessarily. With the numbers im trying to do the exact same thing. I read from the first file until i encounter the first uncopied number from the second one. Then print from the second one until i encounter the next number from where i left off in the first. And etc. The numbers are only to help me understand if its working as intended. You could exchange the numbers for the corresponding words and it would still have to be the exact same result.
mtreit
mtreit3y ago
I would have thought based on your names description the numeric output would be: 1 2 3 3 4 5 6 9 10 11 6 7 8 9 11 12 13
Triufelis
TriufelisOP3y ago
Oh. I see why you would think that. Maybe i was not specific enough. Let me give you another example with a bit more text. File1: my name is john and i like dancing File2: john is my name and dancing is what i like Result: my name is john john is my name and and i like dancing dancing
mtreit
mtreit3y ago
"is what i like" just gets thrown away? I think maybe you need to translate the algorithm into a bit of a more formal description to make it more clear 🙂
Accord
Accord3y ago
✅ This post has been marked as answered!
Want results from more Discord servers?
Add your server