Question

Write a C program to combine two arrays of the same size arranged in order descendant....

Write a C program to combine two arrays of the same size arranged in order
descendant.
Test data :
Enter the number of elements to be stored in the first array: 3
Input 3 elements in the arrangement:
element [0]: 1
element [1]: 2
element [2]: 3
Enter the number of elements to be stored in the second array: 3
Input 3 elements in the arrangement:
element [0]: 1
element [1]: 2
element [2]: 3
Expected output:
The combined array in descending order is:
3 3 2 2 1 1

Homework Answers

Answer #1

Please find the C code for the following:

Code:

#include <stdio.h>
int main()
{
int size1,size2,index=0;
  
//Prompt the user to enter the size of the first array
printf("Enter the number of elements to be stored in the first array: ");
scanf("%d",&size1);
int array1[size1];
printf("Input %d elements in the arrangement:\n",size1);
//Read the first array elements
for(int i=0;i<size1;i++)
{
  
printf("element[%d]:",i);
scanf("%d",&array1[i]);
}
  
//Prompt the user to enter the size of the second array
printf("Enter the number of elements to be stored in the second array: ");
scanf("%d",&size2);
int array2[size2];
printf("Input %d elements in the arrangement:\n",size2);
//Read the second array elements
for(int i=0;i<size2;i++)
{
  
printf("element[%d]:",i);
scanf("%d",&array2[i]);
}

//Copy both the arrays into a single array named combinedArray
int combinedArray[size1+size2];
for(int i=0;i<size1;i++)
combinedArray[index++]=array1[i];
for(int i=0;i<size2;i++)
combinedArray[index++]=array2[i];
  
//Sort the array in descending order
for(int i=0;i<size1+size2;i++)
{
for(int j=i+1;j<size1+size2;j++)
{
if(combinedArray[i]<combinedArray[j])
{
int temp=combinedArray[j];
combinedArray[j]=combinedArray[i];
combinedArray[i]=temp;
}
}
}
  
//Print out the elements of the combinedArray in descending order
printf("The combined array in descending order is:\n");
for(int i=0;i<size2+size1;i++)
printf("%d ",combinedArray[i]);
  
return 0;
}

Please check the compiled program and its output for your reference:

Output:

(I believe that I made the code simple and understandable. If you still have any query, Feel free to drop me a comment)


Hope this Helps!!!
Please upvote as well, If you got the answer?
If not please comment, I will Help you with that...

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
WRITE C++ PROGRAM FOR 1,2,3,4 PARTS of question, DO ADD COOMENTS AND DISPLAY THE OUTPUT OF...
WRITE C++ PROGRAM FOR 1,2,3,4 PARTS of question, DO ADD COOMENTS AND DISPLAY THE OUTPUT OF A RUNNING COMPILER QUESTION: 1) Fibonacci sequence is a sequence in which every number after the first two is the sum of the two preceding ones. Write a C++ program that takes a number n from user and populate an array with first n Fibonacci numbers. For example: For n=10 Fibonacci Numbers: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 2): Write...
(C++) 5.15 LAB: Two smallest numbers with arrays Write a program that reads a list of...
(C++) 5.15 LAB: Two smallest numbers with arrays Write a program that reads a list of integers, and outputs the two smallest integers in the list, in ascending order. The input begins with an integer indicating the number of integers that follow. Ex: If the input is: 5 10 5 3 21 2 the output is: 2 3 You can assume that the list of integers will have at least 2 values. To achieve the above, first read the integers...
Arrays, loops, functions: Lotto Element Repeated Function Write a function that that takes as parameters an...
Arrays, loops, functions: Lotto Element Repeated Function Write a function that that takes as parameters an array of ints, an int value named element, and an int value named end. Return a bool based on whether the element appears in the array starting from index 0 and up to but not including the end index. Generate Random Array Write a function that takes as parameters an array of integers and another integer for the size of the array. Create a...
Topics Arrays Accessing Arrays Description Write a C++ program that will display a number of statistics...
Topics Arrays Accessing Arrays Description Write a C++ program that will display a number of statistics relating to data supplied by the user. The program will ask the user to enter the number of items making up the data. It will then ask the user to enter data items one by one. It will store the data items in a double array. Then it will perform a number of statistical operations on the data. Finally, it will display a report...
Using C++, Python, or Java, write a program that: In this programming exercise you will perform...
Using C++, Python, or Java, write a program that: In this programming exercise you will perform an empirical analysis of the QuickSort algorithm to study the actual average case behavior and compare it to the mathematically predicted behavior. That is, you will write a program that counts the number of comparisons performed by QuickSort on an array of a given size. You will run the program on a large number of arrays of a certain size and determine the average...
Assignment #4 – Student Ranking : In this assignment you are going to write a program...
Assignment #4 – Student Ranking : In this assignment you are going to write a program that ask user number of students in a class and their names. Number of students are limited to 100 maximum. Then, it will ask for 3 test scores of each student. The program will calculate the average of test scores for each student and display with their names. Then, it will sort the averages in descending order and display the sorted list with students’...
Write a program to determine the minimum element in an array of ten elements. The program...
Write a program to determine the minimum element in an array of ten elements. The program should have the following: 1. Class Name as ArrayProcessing. The main method should create an array of size 10 2. There should be two methods besides the main method in the class namely inputArray and MinimumElement 3. InputArray method should assign the ten elements in the array. Use scanner to input these elements. The array of 10 numbers in the method "InputArray" should be...
In C programming language write a function that takes two arrays as input m and n...
In C programming language write a function that takes two arrays as input m and n as well as their sizes: size_m and size_n, respectively. Then it checks for each element in m, whether it exists in n. The function should update a third array c such that for each element in m: the corresponding position/index in c should be either 1, if this element exists in m, or 0, if the element does not exist in n
C LANGUAGE CODE WITH COMMAND-LINE ARGUMENTS (NO SCANF TO BE USED ) Q]. Write a program...
C LANGUAGE CODE WITH COMMAND-LINE ARGUMENTS (NO SCANF TO BE USED ) Q]. Write a program that displays all the prime numbers in the given array with the following constraint. Constraint: Only those prime numbers should be displayed whose location is a composite number. Although you may have several prime numbers in the array, only those prime numbers should be displayed which are stored at non-prime locations. Remember that the first position in an array corresponds to the location/index 0....
1. Write a C++ program that implements the recursive function isMember as declared above. Pass an...
1. Write a C++ program that implements the recursive function isMember as declared above. Pass an array to the function by reference. Here are two sample runs of the program. The elements of the array are: 0 4 5 6 7 Enter the element you want to find: 6 Element is in the array Press any key to continue . . .