Question

In: Computer Science

C++ Given 2 int arrays that are related, sort them correspondingly. EXAMPLE: int arr1[] = {84,...

C++

Given 2 int arrays that are related, sort them correspondingly.

EXAMPLE:

int arr1[] = {84, 4, 30, 66, 15};

int arr2[] = {7, 5, 2, 9, 10};

SORTED ANSWER:

4 - 5

15 - 10

30 - 2

66 - 9

84 - 7

IN C++

Solutions

Expert Solution

Below is your code: -

#include<iostream>
using namespace std;

int main() {
   //declaring arrays
   int arr1[] = {84, 4, 30, 66, 15};
   int arr2[] = {7, 5, 2, 9, 10};
   //getting size of first array
   int n = sizeof(arr1)/sizeof(arr1[0]);
   //code to sort the arrays
   //traverse first array
   for(int i=0; i<n; i++)
   {      
       //compare it with subsequent member of first array
       for(int j=i+1; j<n; j++)
       {
           //If array element is unsorted
           //sort first array and swap elemnt of second array also
           if(arr1[i] > arr1[j])
           {
               int temp1 =arr1[i];
               int temp2 =arr2[i];
               arr1[i]=arr1[j];
               arr2[i]=arr2[j];
               arr1[j]=temp1;
               arr2[j]=temp2;
           }
       }
   }
  
   cout<<"SORTED ANSWER: "<<endl;
   //printing the array
   for(int i = 0; i < n; i ++) {
       cout<<arr1[i]<<" - "<<arr2[i]<<endl;
   }
  
   return 0;
}

Output

SORTED ANSWER:
4 - 5
15 - 10
30 - 2
66 - 9
84 - 7


Related Solutions

Given the root C++ code: void sort() {    const int N = 10;    int...
Given the root C++ code: void sort() {    const int N = 10;    int x[N];    for(int i = 0; i < N; i++)    {        x[i] = 1 + gRandom-> Rndm() * 10;        cout<<x[i]<<" "; }    cout<<endl;    int t;       for(int i = 0; i < N; i++)    {    for(int j = i+1; j < N; j++)    {        if(x[j] < x[i])        {   ...
C Write a function int sort(int** arr, int n) that takes as input an array of...
C Write a function int sort(int** arr, int n) that takes as input an array of int pointers, multiplies the ints the pointers point to by -1, and then sorts the array such that the values pointed to by the pointers are in decreasing order. For example, if the array is size 10 the pointer at index 0 will point to the largest value after multiplying by -1 and the pointer at index 9 will point to the smallest value...
Write a very general sort method that can sort any type of data arrays/lists. For example,...
Write a very general sort method that can sort any type of data arrays/lists. For example, can sort a list of integers in ascending order, a list of integers in descending order, a list of doubles, a list of student objects (with names and scores) in ascending order of names, or in descending order of scores, … You can use any pre-defined sort function or can code your own. Use your favorite language for implementation. If your language doesn’t support...
Create a C++ file with given instructions below- Analyze random numbers Create two arrays, one int...
Create a C++ file with given instructions below- Analyze random numbers Create two arrays, one int one double. They should have 10 elements each Ask the user how many random numbers to generate. Then generate that many random numbers between 1 and 10. Use a for loop. Keep track of how many of each number in the range was generated (1 to 10) in the int array. Be sure to initialize the array to all 0's to start. Then, send...
Code: C++ Write a very general sort method that can sort any type of data arrays/lists....
Code: C++ Write a very general sort method that can sort any type of data arrays/lists. For example, can sort a list of integers in ascending order, a list of integers in descending order, a list of doubles, a list of student objects (with names and scores) in ascending order of names, or in descending order of scores, … You can use any pre-defined sort function or can code your own. Use your favorite language for implementation. If your language...
Write a function in C that uses the Merge Sort sorting algorithm with arrays. The function...
Write a function in C that uses the Merge Sort sorting algorithm with arrays. The function must not be void and must output type int* i.e. it must take the form: int* merge_sort(int a[], int n) where a[ ] is the input matrix and n is the size of the matrix. You may use an auxiliary functions such as "merge." The returned array should be sorted using merge_sort and should not modify the array that was input (a[ ] ).
In C++ Write a function that accepts two int arrays of the same size. The first...
In C++ Write a function that accepts two int arrays of the same size. The first array will contain numbers and the second array will be filled out inside the function. THE FUNCTION SHOULD FIND ALL NUMBERS IN THE ARRAY THAT ARE GREATER THAN OR EQUAL TO THE AVERAGE. You need to design the function. so the output code should be: (show contents of 1st array) The numbers that are greater than the average of the first array are: (the...
In c++ Array expander Write a function that accepts an int array and the arrays size...
In c++ Array expander Write a function that accepts an int array and the arrays size as arguments. The function should create a new array that is twice the size of the argument array. The function should create a new array that is twice the size of the argument array. The function should copy the contents of the argument array to the new array and initialize the unused elements of the second array with 0. The function should return a...
Counting SortShow the B and C arrays after Counting Sort finishes on the array A [19,...
Counting SortShow the B and C arrays after Counting Sort finishes on the array A [19, 6, 10, 7, 16, 17, 13, 14, 12, 9] if the input range is 0-19.
from partition import partition def quicksort(a: list, l: int, u: int) -> None: '''Sort the given...
from partition import partition def quicksort(a: list, l: int, u: int) -> None: '''Sort the given list a in non-descending order. Precondition: 0 <= l and u < len(a)''' if l < u: mid = (l + u) // 2 three = [a[l], a[mid], a[u]] three.sort() if three[1] == a[l]: pivot_loc = l elif three[1] == a[u]: pivot_loc = u else: pivot_loc = mid a[u], a[pivot_loc] = a[pivot_loc], a[u] pivot = a[u] i = partition(a, l, u - 1, pivot)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT