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

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...
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)...
This is C++ programming Given an int variable k, an int array incompletes that has been...
This is C++ programming Given an int variable k, an int array incompletes that has been declared and initialized, an int variable nIncompletes that contains the number of elements in the array, an int variable studentID that has been initialized, and an int variable numberOfIncompletes, Write code that counts the number of times the value of studentID appears in incompletes and assigns this value to numberOfIncompletes. You may use only k, incompletes, nIncompletes, studentID, and numberOfIncompletes. I tried this code...
Write a C++ app to read both files, store them into parallel vectors, sort the list...
Write a C++ app to read both files, store them into parallel vectors, sort the list of people in alphabetical order, display the new sorted list of names with their corresponding descriptions. Use the Bubble Sort strategy to rearrange the vector(s). File 1: Marilyn Monroe Abraham Lincoln Nelson Mandela John F. Kennedy Martin Luther King Queen Elizabeth II Winston Churchill Donald Trump Bill Gates Muhammad Ali Mahatma Gandhi Margaret Thatcher Mother Teresa Christopher Columbus Charles Darwin Elvis Presley Albert Einstein...
Given a list of numbers, could you build a Turing machine to sort them into non-decreasing...
Given a list of numbers, could you build a Turing machine to sort them into non-decreasing order? Non-decreasing order is essentially the same thing as increasing order, except that it recognizes that some of the numbers might be repeated. Explain why this is or why this is not possible. Do not try to give an algorithm or even a description of how the machine would work--just an explanation as to why it is or is not possible.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT