Question

In: Computer Science

Write a C program to take two integer arrays in ascending order and combine them in...

Write a C program to take two integer arrays in ascending order and combine them in a new array in descending order. Remove any duplicates in the final array, if any. Your code must store the arrays as a linked list of objects, each put in a “struct.” All operations should also be carried out using linked lists.

Input: 2 sorted arrays, each on a new line.

Output: An array sorted in descending order, without duplicates.

There is a single white space after each number, even after the last number. There is no new line at the end. Use the following struct as a reference:

struct ELEMENT {

int value;

struct ELEMENT *next;

};

=== Sample test case 1 ===

Input:

2 15 18 34 67 87 88

3 8 18 33 67 89 91

Output:

91 89 88 87 67 34 33 18 15 8 3 2

=== Sample test case 2 ===

Input:

28 36 57 59 68 69 420

17 37 57 59 68

Output:

420 69 68 59 57 37 36 28 17

Solutions

Expert Solution

Code---->

#include <stdio.h>
#include <stdlib.h>
struct ELEMENT {
int value;
struct ELEMENT *next;
};

void merge(int arr1[],int arr2[],int sz1,int sz2)
{
struct   ELEMENT *temp=NULL;
   struct   ELEMENT *head;
   struct ELEMENT *prev=NULL;
   // declare a node
   int cnt=0;
   while(sz1>0&&sz2>0)
   {
       temp = (struct ELEMENT*)malloc(sizeof(struct ELEMENT)); //allocate memory
   temp->next = NULL;
       if(arr1[sz1-1]>arr2[sz2-1])//when arr1 element is greater than arr2
       {
           if(cnt==0)//head pointer
           {
       temp->value=arr1[sz1-1];
       sz1--;
      
       head=temp;
       prev=temp;
           }
           else
           {
       temp->value=arr1[sz1-1];
           sz1--;
           prev->next=temp;//link to previous pointer
           prev=temp;
           }
       }
       else if(arr1[sz1-1]<arr2[sz2-1])//when arr2 element is greater than arr1
       {
           if(cnt==0)//head pointer
           {
       temp->value=arr2[sz2-1];
           sz2--;
           head=temp;
           prev=temp;
          
           }
           else
           {
       temp->value=arr2[sz2-1];
           sz2--;
           prev->next=temp;//link to previous pointer
           prev=temp;
          
           }
       }
       else//when arr2 element is equal than arr1
       {
           if(cnt==0)//head pointer
           {
       temp->value=arr1[sz1-1];
       printf("%d\n",arr1[sz1-1]);
           sz1--;
           sz2--;
           head=temp;
           prev=temp;
           }
           else
           {
       temp->value=arr1[sz1-1];
       sz1--;
           sz2--;
           prev->next=temp;//link to previous pointer
           prev=temp;
          
           }
       }
       cnt++;
   }
   while(sz1>0)
   {
       temp = (struct ELEMENT*)malloc(sizeof(struct ELEMENT));
       temp->next = NULL;
       temp->value=arr1[sz1-1];
      
           sz1--;
           prev->next=temp;//link to previous pointer
           prev=temp;
   }
   while(sz2>0)
   {
       temp = (struct ELEMENT*)malloc(sizeof(struct ELEMENT));
       temp->next = NULL;
           temp->value=arr2[sz2-1];
          
           sz2--;
           prev->next=temp;//link to previous pointer
           prev=temp;
          
   }
  
   temp=head;
   while (temp->next != NULL)
{
printf("%d ", temp->value);
temp = temp->next;
}
}
int main()
{
int arr1[100],arr2[100];
int sz1=0,M;
char x;
//Take input until next line and store in arr1
while(scanf("%d", &M)!= EOF)
   {
arr1[sz1]=M;
scanf("%c",&x);
sz1++;
if(x=='\n')// Check for new line then break
break;
}
int sz2=0;
//Take input until next line and store in arr2
while(scanf("%d", &M)!= EOF)
   {
arr2[sz2]=M;
scanf("%c",&x);
sz2++;
if(x=='\n')// Check for new line then break
break;
}
merge(arr1,arr2,sz1,sz2);
return 0;
}

Input:

28 36 57 59 68 69 420
17 37 57 59 68

output:

420 69 68 59 57 37 36 28

Ideone LINK :https://ideone.com/7qYbPy

Any doubt ask in comments section.Please Upvote.Thanks and regards


Related Solutions

Given two integer arrays sorted in the ascending order, code the function SortArrays to merge them...
Given two integer arrays sorted in the ascending order, code the function SortArrays to merge them into one array in the descending order. You need to make sure if the values in the arrays are changed, it will still work. (25 points) #include using namespace std; void SortArrays (int a[], int b[], int c[], int size); void printArray(int m[], int length); const int NUM = 5; int main() { int arrA[NUM] = {-2, 31, 43, 55, 67}; int arrB[NUM] =...
Given two integer arrays sorted in the ascending order, code the function SortArrays to merge them...
Given two integer arrays sorted in the ascending order, code the function SortArrays to merge them into one array in the descending order. You need to make sure if the values in the arrays are changed, it will still work. (25 points) #include <iostream> using namespace std; void SortArrays (int a[], int b[], int c[], int size); void printArray(int m[], int length); const int NUM = 5; int main() { int arrA[NUM] = {-2, 31, 43, 55, 67}; int arrB[NUM]...
Given two integer arrays sorted in the ascending order, code the function SortArrays to merge them...
Given two integer arrays sorted in the ascending order, code the function SortArrays to merge them into one array in the descending order. You need to make sure if the values in the arrays are changed, it will still work. (25 points) #include <iostream> using namespace std; void SortArrays (int a[], int b[], int c[], int size); void printArray(int m[], int length); const int NUM = 5; int main() { int arrA[NUM] = {-2, 31, 43, 55, 67}; int arrB[NUM]...
in C++ Given two integer arrays sorted in the ascending order, code the function SortArrays to...
in C++ Given two integer arrays sorted in the ascending order, code the function SortArrays to merge them into one array in the descending order. You need to make sure if the values in the arrays are changed, it will still work. (25 points) #include <iostream> using namespace std; void SortArrays (int a[], int b[], int c[], int size); void printArray(int m[], int length); const int NUM = 5; int main() { int arrA[NUM] = {-2, 31, 43, 55, 67};...
This phyton program takes in three integer parameters and displays them in ascending order. You can...
This phyton program takes in three integer parameters and displays them in ascending order. You can assume that the numbers entered are integers. You CAN'T use lists, min(), max(), or the sort utility. The point is to use if tests. sortThreeIntegers( 39, 2, 5 ) should display: The numbers in order are: 2 5 39 sortThreeIntegers( 14, -12, -1000 ) should display: The numbers in order are: -1000 -12 14 def sortThreeIntegers( x, y, z ): < your code goes...
This is JAVA PROGRAMMING Sort the contents of the two files in ascending order and combine...
This is JAVA PROGRAMMING Sort the contents of the two files in ascending order and combine them into one new file (words.txt). When comparing string order, you must use the compareTo method and make an exception.The source code below is a code that only combines files. Use the comparedTo method to sort the contents of the two files in ascending order.(ex. if(str1.compareTo(str2)<0) {}) <file1.txt> at first   castle   consider   considerable   enlighten   explain   explanation   female   <file2.txt> consideration   considering that   education   educational   endow  ...
***C++ Coding*** Write a program for sorting a list of integers in ascending order using the...
***C++ Coding*** Write a program for sorting a list of integers in ascending order using the bubble sort algorithm. Please include comments to understand code. Requirements Implement the following functions: int readData( int **arr) arr is a pointer to pointer for storing the integers. The function returns the number of integers. The function readData reads the list of integers from a file call data.txt into the array arr. The first integer number in the file is the number of intergers....
C++ Write a program for sorting a list of integers in ascending order using the bubble...
C++ Write a program for sorting a list of integers in ascending order using the bubble sort algorithm Requirements Implement the following functions: Implement a function called readData int readData( int **arr) arr is a pointer for storing the integers. The function returns the number of integers. The function readData reads the list of integers from a file call data.txt into the array arr. The first integer number in the file is the number of intergers. After the first number,...
Write a program that takes two integer arrays a and b of size n from the...
Write a program that takes two integer arrays a and b of size n from the user, the use a method product to find the product of a and b and return the results after storing them in an array c, then prints the returned results to the screen. (Note: c[i] = a[i] * b[i], for i = 0, ..., n-1) Sample Output: Enter the size of your arrays: 5 Enter the integer values of the first array a: 1...
Write C program Multidimensional Arrays Design a program which uses two two-dimensional arrays as follows: an...
Write C program Multidimensional Arrays Design a program which uses two two-dimensional arrays as follows: an array which can store up to 50 student names where a name is up to 25 characters long an array which can store marks for 5 courses for up to 50 students The program should first obtain student names and their corresponding marks for a requested number of students from the user. Please note that the program should reject any number of students that...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT