Create an input and output file 25. Write a complete program to do the following
ID: 3549226 • Letter: C
Question
Create an input and output file
25. 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. (See Exercise
45.) 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 (e.g., 456 20000 or 123 30234).
The main program calls these arrays idnumbers and donations. A
separate printing function prints the original set of data in the form
of a neat table. When the arrays print, there should be an overall
heading, plus headings for the columns of ID numbers and donations.
(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
#include<iostream>
using namespace std;
void print(int id[],int donation[],int n)
{
cout << " ID Donation ";
for(int i=0;i<n;i++)
cout << " " << id[i] << " " << donation[i];
cout << " ___________________________ ";
}
int input(int id[],int donation[])
{
int num=0;
cout << " Enter the total number of numbers ";
cin >> num;
for(int i=0;i<num;i++)
{
cout << " Enter the ID number " << i+1 << " --> ";
cin >> id[i];
cout << " Enter the corresponding Donation --> ";
cin >> donation[i];
}
cout << " ___________________________ The data you entered are --> ";
print(id,donation,num);
return num;
}
void SortByID(int id[],int donation[],int n)
{
int i=0,j=0,k=0,tempid=0,temp_don=0;
for(i=1;i<=n-1;i++)
{
for(j=0;j<i;j++)
{
if(id[j]>id[i])
{
tempid=id[j];
id[j]=id[i];
temp_don=donation[j];
donation[j]=donation[i];
for(k=i;k>j;k--)
{
id[k]=id[k-1];
donation[k]=donation[k-1];
}
id[k+1]=tempid;
donation[k+1]=temp_don;
}
}
}
cout << " ___________________________ After Sorting by ID, The Data are----> ";
print(id,donation,n);
}
void SortByDonation(int id[],int donation[],int n)
{
int i=0,j=0,k=0,tempid=0,temp_don=0;
for(i=1;i<=n-1;i++)
{
for(j=0;j<i;j++)
{
if(donation[j]>donation[i])
{
tempid=id[j];
id[j]=id[i];
temp_don=donation[j];
donation[j]=donation[i];
for(k=i;k>j;k--)
{
id[k]=id[k-1];
donation[k]=donation[k-1];
}
id[k+1]=tempid;
donation[k+1]=temp_don;
}
}
}
cout << " ___________________________ After Sorting by Donation, The Data are----> ";
print(id,donation,n);
}
int main()
{
int id[50]={0};
int donation[50]={0};
int n=0;
n=input(id,donation);
SortByID(id,donation,n);
SortByDonation(id,donation,n);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.