Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I have a C++ program which I could use help on, much appreciated, thank you! Wri

ID: 3841021 • Letter: I

Question

I have a C++ program which I could use help on, much appreciated, thank you!

Write a complete program to do the following:

The main program calls a function to read in a set of people’s three-digit ID numbers and their donations to a charity. The main program calls a function to sort the ID numbers into numerical order, being sure to carry along the corresponding donations.

The main program calls a function to print the sorted lists, giving both ID numbers and donations. The main program also calls a function to sort the donation amounts into ascending order, carrying along the corresponding ID numbers. Print the sorted lists, giving both ID numbers and donations.

Here are the details:

(a) The main program calls a function to read in the data. The data set consists of a parameter value which the main program calls n and n groups of data, each of which contains a person’s three-digit ID number and an integer (ie: 456 20000 or 123   30234).

(b) Then the main program sends the array of ID numbers, the array of donations, and the size n to a sorting function. This function sorts the ID numbers into numerical order. Be sure to maintain the matchup of ID numbers and donations. For example, 456 should always be associated with 20000, no matter where 456 moves in numerical order; similarly, 123 should stay with 30234.

When the sorting function finishes and returns control to the main program, it calls the printing function to print the two arrays.

(c) Next the main program sends the same three parameters to the sorting function, which sorts the donations into numerical order, being sure to maintain the linkup of ID numbers and donations.

When the sorting function finishes and returns control to the main program, it calls the printing function to print the two arrays with appropriate headings.

Your arrays should have room for up to 50 entries. To test the program, have a set of data with at least 15 to 20 values in each array. Make sure that your original order in not close to numerical order for either array and that the two numerical orders are not close to each other.

Explanation / Answer

Code:

// Program to demonstrate taking id, donation values as input and sorting them
#include <iostream>
using namespace std;

// function prototype
void readIdandDonation(int, long[], long[]);
void printValues(int, long[], long[]);
void sortValues(int, long[], long[]);

int main()
{
int n;
long id[100], don[100];

// reading number of values user want to read
cout << "Enter how many values you want to read? ";
cin >> n;
readIdandDonation(n, id, don);
cout << "Given values are" << endl;
printValues(n, id, don);
cout << "sorting by id" << endl;
sortValues(n, id, don);
printValues(n, id, don);
cout << "sorting by donation" << endl;
sortValues(n, don, id);
printValues(n, id, don);
  
return 0;
}

// function to read Id and Donation from the user
void readIdandDonation(int n, long id[], long don[])
{
for(int i = 0; i < n; i++){
cout << "Enter Id number: ";
cin >> id[i];
cout << "Enter Donation amount: ";
cin >> don[i];
}
}

// function to print Id and Donation values
void printValues(int n, long id[], long don[])
{
cout << "ID, Donation" << endl;
for(int i = 0; i < n; i++){
cout << id[i] << "," << don[i] << endl;
}
}

// functin to sort the given arrays
void sortValues(int size, long a[], long b[])
{
int temp1, temp2;
  
   for(int i = 0; i < size; i++)
for(int j = 0; j < size-1; j++)
       if(a[j] > a[j+1]){
           temp1 = a[j];
               a[j]= a[j+1];
               a[j+1] = temp1;
          
               temp2 = b[j];
               b[j] = b[j+1];
               b[j+1] = temp2;  
           }

}


Execution and output:
Enter how many values you want to read? 3
Enter Id number: 1
Enter Donation amount: 1000
Enter Id number: 2
Enter Donation amount: 90000
Enter Id number: 0
Enter Donation amount: 13103
Given values are
ID, Donation
1,1000
2,90000
0,13103
sorting by id
ID, Donation
0,13103
1,1000
2,90000
sorting by donation
ID, Donation
1,1000
0,13103
2,90000

Enter how many values you want to read? 2
Enter Id number: 456
Enter Donation amount: 20000
Enter Id number: 123
Enter Donation amount: 30234
Given values are
ID, Donation
456,20000
123,30234
sorting by id
ID, Donation
123,30234
456,20000
sorting by donation
ID, Donation
456,20000
123,30234