Question:
c++ (C Plus Plus) Topic: Merging and Sorting the arrays?
Jawad
2009-11-17 03:01:45 UTC
Please any body help me, i have try many time but i can not do this, Please solve the programme.

My question is

Problem Statement: Merging and Sorting of character arrays
You are required to write a program that takes character values in two different arrays as input from user. After getting input, make a third array and merge both these array in third array. Output array must be sorted in ascending order.

Detailed Description:
1. Declare three character type arrays.
2. Use two arrays to take input from user.
o Take input as char type.
o Take 10 values in each array.

3. Merge both these input arrays.
o If an input character is in both arrays, then it should appear once in resulted array.
4. Store result in third array.
5. Sort resulted array in ascending order.
o Ascending order means string values starting from ‘a’ will come first, and then starting from ‘b’ and so on.
6. Display them after sorting

Sample Output
Enter values in first array: a h b c u v I j k e
Enter values in Second array: y u d f g k I w q a

Merged array: a y h u b d c f g v k I j w q e

Sorted Array in ascending order: a b c d e f g h I j k q u v w y




And The Solution that i have solve is as under. but is is uncorrect please answer the full programme





#include
#include

int main()
{
char array1[10];
char array2[10];
char array3[20];
int i=0;
cout<<"Enter value in first array";
for(int i=0; i<10; i++)
{
cin>>array1[i];
}
cout<<"Enter value in second array";
for(int i=0; i<10; i++)
{
cin>>array2[i];
}
for(int i=0; i<20; i++);
{
for(int k=0; k<10; k++)
{
if(array1[k]==array2[k])
{
array3[i]=array1[k];i++;
//continue;
}
else
{
array3[++i]=array2[k];
array3[++i];
for(int i=0; i<20; i++);
{
cout< }
}
}
}
getche();
}
Three answers:
AnalProgrammer
2009-11-17 03:52:29 UTC
To merge the arrays requires one for loop and an additional destination subscript.



int k=0;

for (int i=0;i<10;i++) {

array3[k] = array1[i];

k++;

array3[k] = array2[i];

k++;

} // End for i



This loop does not however check for duplicates. For that you need to add a function or another for loop.

int k=0;

boolean found;

for (int i=0;i<10;i++) {

found = false;

for (int j=0;j
if (array3[j] == array1[i]) {

found = true;

break;

} // End if

} // End for j

if (!found) {

array3[k] = array1[i];

k++;

} // End if

found = false;

for (int j=0;j
if (array3[j] == array2[i]) {

found = true;

break;

} // End if

} // End for j

if (!found) {

array3[k] = array2[i];

k++;

} // End if

} // End for i



Have fun.
rosson
2016-10-02 11:05:10 UTC
for sure, this could nicely be a homework question, so we gained't provide you comprehensive solutions, yet listed right here are some tricks: declare character arrays like this: char array1[10]; char array2[10]; char array3[20]; through fact the 1st 2 arrays would possibly no longer have any letters in hardship-unfastened, array3 desires to be as long as the two one in each and every of the different arrays mixed. Use a for loop to get characters one-via-one from the consumer. you need to use "cin >> c" the place c is declared as a char variable to get a single character. reckoning on how progressed your classification is, use the C customary function qsort, or the C++ customary set of rules form, or use an straight forward test over the alphabet with a nested loop to test over the two arrays to ascertain whether each and each letter is contemporary: int suits = 0; for( char letter = 'a'; letter <= 'z'; ++letter ) { bool stumbled on = fake; for( int i = 0; i < 10; ++i ) { if( array1[i] == letter || array2[i] == letter) stumbled on = real; } if( stumbled on ) array3[suits++] = letter; }
anonymous
2009-11-17 03:06:32 UTC
strcat()



bubble sort


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...