Sololearn: Learn to Code
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2
Manav Roy Explanation is given inside code Merging using 3rd array https://code.sololearn.com/cOjyzOXoDGc1/?ref=app
4th Feb 2022, 8:37 AM
A͢J
A͢J - avatar
+ 10
//C language #include<stdio.h> int main() { int arr1[30], arr2[30], res[60]; int i, j, k, n1, n2; printf("\nEnter no of elements in 1st array :"); scanf("%d", &n1); for (i = 0; i < n1; i++) { scanf("%d", &arr1[i]); } printf("\nEnter no of elements in 2nd array :"); scanf("%d", &n2); for (i = 0; i < n2; i++) { scanf("%d", &arr2[i]); } i = 0; j = 0; k = 0; // Merging starts while (i < n1 && j < n2) { if (arr1[i] <= arr2[j]) { res[k] = arr1[i]; i++; k++; } else { res[k] = arr2[j]; k++; j++; } } /* Some elements in array 'arr1' are still remaining where as the array 'arr2' is exhausted */ while (i < n1) { res[k] = arr1[i]; i++; k++; } /* Some elements in array 'arr2' are still remaining where as the array 'arr1' is exhausted */ while (j < n2) { res[k] = arr2[j]; k++; j++; } //Displaying elements of array 'res' printf("\nMerged array is :"); for (i = 0; i < n1 + n2; i++) printf("%d ", res[i]); return (0); } Hope it's helpful 😃
6th Feb 2022, 1:01 AM
Vaibhav
Vaibhav - avatar
+ 5
Manav Roy note that in the line arr[i]=arr1[i]; it is indexing outside the array bounds of arr1[] when i increments past 1. Though it is harmless in this case, it is bad code. It would be better to make two loops and track the index for arr[] through both loops by using count. Move the declaration for count outside the loop so it is unnecessary to make it static. #include <iostream> using namespace std; int main() {     int arr1[]={1,2};     int arr2[]={3,4};     int arr[4];     int count = 0;     for(int i=0;i<2;i++)     {         arr[count++]=arr1[i];     }     for(int i=0;i<2;i++)     {         arr[count++]=arr2[i];     }     for(int i=0;i<count;i++)     {         cout<<arr[i]<<endl;     }     return 0; }
4th Feb 2022, 9:00 PM
Brian
Brian - avatar
+ 4
Pointers? malloc(or calloc) then ralloc? Or is that the whole discussion about creating a 3rd array instead of expanding an existing array? does C++ not have memory allocation like C? Is memory allocation called STL?
4th Feb 2022, 9:47 PM
HungryTradie
HungryTradie - avatar
+ 4
HungryTradie yes, definitely malloc and realloc would be the next level to recommend. If Manav Roy is comfortable working with pointers and dynamic arrays, C++ natively supports them. https://www.guru99.com/cpp-dynamic-array.html
4th Feb 2022, 10:02 PM
Brian
Brian - avatar
+ 4
I have made code about how to join two array.. CLICK ON THIS LINK⬇️ https://code.sololearn.com/cK81Uv4657n1/?ref=app
5th Feb 2022, 6:01 AM
Hemant
Hemant - avatar
+ 3
Otherwise, You can take 3rd array of sum of 2 array sizes, and copy by a loop..
4th Feb 2022, 8:30 AM
Jayakrishna 🇮🇳
+ 3
Runtime Terror What was misleading answer even Jayakrishna🇮🇳 also told same thing. And also you can find many answers on the internet. It means everyone are providing misleading answer. Am I right? This is also a one solution to join two array.
4th Feb 2022, 3:34 PM
A͢J
A͢J - avatar
+ 3
When it is already mentioned (Without using STL) , it's (A͢J answer) perfect alternative. These are practices of 'how to do without predefined functions'.. And in this way, the STL are implemented with improvements... These(fixed size array) are not used in real projects but everyone should learn first..
4th Feb 2022, 3:45 PM
Jayakrishna 🇮🇳
+ 3
Runtime Terror Answer this : "when it asked only join array, (without STL) explicitly and what he mean by example.. " You said "only can do this with STL ". But possible way is already answered. It is not asked better way.. Then every one can go with their own idea. You can't say, some one wrong without proof. You can advise or debate but not have to say " Misleading ". " interviewer may ask 'do it without predefined functions.. " OP not mentioned merging/join by STL, so you can implement many ways.. , also join=combining, if you want grammatically meaning.. We are not mean those are about "Merge/join in c++ STL way".. Not said a best way also.. So never it misleading.. Clearly said" copy to 3rd array.. " Before posting your mislead conclusions, take a clarity or go on to proof with wrong answers, then You don't need to care about other answers. "About downvotes : I only say if you take it ' don't care about downvotes'... for your clarity : 'I never used dislikes'.
4th Feb 2022, 4:39 PM
Jayakrishna 🇮🇳
+ 3
Runtime Terror 🌡🌡🌡🌡🌡🌡🌡🌡 🚒 cool down, man. 💦 we are not here to save the world.
5th Feb 2022, 10:08 AM
Bob_Li
Bob_Li - avatar
+ 3
Runtime Terror 😜 more like thermite and napalm. Live and let live, I say. I feel your passion, and you are right, technically. But maybe an open, relaxed discussion would get your point across better. Live and let live.♥️
5th Feb 2022, 10:48 AM
Bob_Li
Bob_Li - avatar
+ 3
Runtime Terror still you not learn your mistake of unnecessary arguing !! Research is a proof for you , that you can learn "you are wrong".. We need to proof someone wrong with proof, not just words.. I did , and also we are not wrong. Finally, some what you took turn about your words and said " 3rd array is a separate solution" , which you said not possible previously. You learn something but we are not learn anything from you, and we don't need.. We are not made any mistake. You did 'unnecessary pointing' and i hope you realize it. Others also pointed , try to understand. " Me also not interested to talk about more, I don't have time to replay on unnecessary arguments.. AJ gave more clarity, i also saying same.. ' ****End**** Manav Roy thanks for clarity that you "just want to put elements of 2 arrays in one array " for unnecessary posts by misleading disturbence. Sry i did not checked code because my mobile switched off on that day.. And because of disturbence. hope you get it solved..
7th Feb 2022, 9:52 AM
Jayakrishna 🇮🇳
+ 2
you are trying to proof "we are wrong by misleading answers, but we are not" and we are not misleading anyone. your proof : "compare the address of the **supposedly joined** array." oh. when it is clearly said "use 3rd array alternativly " where the question came of "address comparisons..? You can't understanding "is it professional problem or beginner level practice". " I'm not here to debate with you but you should stop with the confusion and move on next time when you don't have an exact answer to a question " . - you should have do it already, as we are busy with our work, but you interrupted. " It's in your best interest to learn from my answer " but i dont find any proper (and also proper way of) answer. "admit that your answer isn't **joining** but instead **A new array** "- go on read again my post, its clearly there from begining.. and it is " join with new 3rd array" , use google translate if cant understand correctly.. who behaving like stubborn now? am not. i dont know about you, so can
4th Feb 2022, 5:26 PM
Jayakrishna 🇮🇳
+ 2
Runtime Terror who behaving like stubborn now? am not. i dont know about you, so cant say. I only give answer about what only asked, with as my opinion. because i have my own business. Dont want to argue with any one. Instead of arguing, giving unnecessary advise, better to Advise yourself". its just a reply to your post, not argue or harsh reply intensional. i dont prefer thart any day.. if it seems, you can ingore.. hope you can't disturb me again.
4th Feb 2022, 5:27 PM
Jayakrishna 🇮🇳
+ 2
Oh. Man stop talking "joining "^N, who said " 3rd array is the join/merge way.. It's mentioned clearly "copying way... " many times.. Why we say this way, if OP not mention (without STL).. " "You can do a program in N no.of ways... ", better way comes in future days always... 3rd array gives needed input then it's fine. There is no other requirements mentioned there. Where are see those, and confusing others.. Oh pls stop now Mr. Professional Runtime error.., That's suite you. Yes am, OP level... I never said a professional.. I don't want to behave like what not am. Ok.. Are you happy..? https://stackoverflow.com/questions/31528605/concatenate-two-string-arrays-without-using-stl-or-vectors-in-c (3rd array usage proof.) Don't compare your answers with anyone.. You can free to answer but not to talk about others.. https://www.techiedelight.com/join-two-arrays-cpp/ You can find yourself about correctness and tell.. But it's, by 3rd array..
4th Feb 2022, 6:17 PM
Jayakrishna 🇮🇳
+ 2
Manav Roy One example, I found is : https://www.techiedelight.com/join-two-arrays-cpp/ https://stackoverflow.com/questions/31528605/concatenate-two-string-arrays-without-using-stl-or-vectors-in-c You can find yourself about correctness and tell.. But it's without STL way of practice , by 3rd array..
4th Feb 2022, 6:22 PM
Jayakrishna 🇮🇳
+ 2
Runtime Terror See asker is not confused and he didn't say anything even you are confusing him and saying not possible without STL. You are the only one who is trying to proof self as a genius person. And one more thing I didn't learn anything from you. I just learn from myself. And please don't post one more comment because I don't have time to reply on unnecessary arguments. You are correct at your point and we are correct on our point. So doesn't make sense on unnecessary arguments. *****The End *****
4th Feb 2022, 7:37 PM
A͢J
A͢J - avatar
+ 2
Runtime Terror Still same mistake of argumenting. OP clarified, he need output of By 1st array- 2,10,4,8; 2nd array- 9,2,6,1,0; Output - 2,10,4,8,9,2,6,1,0; (Without STL) And i said, you can use 3rd array and cop (you said impossible, but it is possible, so you are wrong) I added proof of link. It's copying to 3rd array.. Where the question of checking addresses came from ? who said it's same address? How it is a proof of us wrong"? Stil it is another argument, not related to my answer! Problem is yes, you didn't even knowing your unnecessary blaming us. We don't have any grammatical problem, but you are not in understanding of OP question and his clarification also.. In fact more confusing, (oh.. forcing us to disturb others) but saying saving. "from wrong answers". No. I didnot give any wrong answer. I clearly said. I stand on my point. Infact, if you feel saving us, better to go on " Report the SL" .. Any one can report on wrong answers also... Pls go on, instead of disburbing and blaming or saving us"
7th Feb 2022, 11:44 AM
Jayakrishna 🇮🇳
+ 2
Ctd; If you have really better things than argument with us Or blame us, go on. Else report. But don't say same things. Stop the unnecessary discussion. I won't reply anymore.. Not interested to say anything more. "It's an alternative copy way to get the answer without STL" Simple. Don't pull wrong meanings from it. Voting : I don't know anyone.. Those are reply from community, Speaking about correctness in their way .. Correct person get upvotes, and if wrong they may get downvotes.. That's the purpose of voting. I never used down voting, till now on anyone. Upvotes, also, I don't care much.. "I just do what's right and focus on myself", --you didnot. That's the problem from you here. Better you did. Thats what I telling from my first post. But you have all eyes on "our replays and in false prooving it". Better focus on yourself, from now, and let me do focus on myself.. Hope you understand from my last reply, don't force to me again.. Let us don't waste our and community time.. ..............,
7th Feb 2022, 1:58 PM
Jayakrishna 🇮🇳