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...
C++ Write a program that takes two integer arrays of different sizes from the user (this...
C++ Write a program that takes two integer arrays of different sizes from the user (this means that the user can tell the program the size of each array), and then computes the intersection and union of those arrays. Note: the arrays can be of different lengths and the intersection/union should have unique elements in sorted order. Use pointer notation of arrays for this question. Eg: *(arr+1) [10]
Write a program that takes two integer arrays of different sizes from the user (this means...
Write a program that takes two integer arrays of different sizes from the user (this means that the user can tell the program the size of each array), and then computes the intersection and union of those arrays. Note: the arrays can be of different lengths and the intersection/union should have unique elements in sorted order. Use pointer notation of arrays for this question. Eg: *(arr+1) [10] C++
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...
Write a Java program that asks the user to enter an array of integers in the...
Write a Java program that asks the user to enter an array of integers in the main method. The program should prompt the user for the number of elements in the array and then the elements of the array. The program should then call a method named isSorted that accepts an array of and returns true if the list is in sorted (increasing) order and false otherwise. For example, if arrays named arr1 and arr2 store [10, 20, 30, 41,...
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...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT