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

Write a program that reads in a set of positive integers and outputs the set in

ID: 3621408 • Letter: W

Question

Write a program that reads in a set of positive integers and outputs the set in a descending order based on the following specifications:

(1)You may assume that the data set has at most 100 numbers and -999 marks the
end of the input data; you may enter the set through the keyboard or read from an input file. Note that the actual number of input values might be less than 100.

(2) The program should use a function to sort the set. The input to the function should be the set of numbers and its actual size; because the number of values entered may be less than 100.

(3) You should print a copy of the original set and its sorted version in a two-column tabular form, with proper headings, to an output file.





Explanation / Answer

please rate - thanks

#include <iostream>
#include <fstream>
using namespace std;
void sort(int[],int);
void print(int[],int[],int);
int main()
{int i,a[100],b[100];
ifstream in;
in.open("input.txt");       
if(in.fail())           
   { cout<<"input file did not open please check it ";
   system("pause");
   return 1;
   }
i=0;
in>>a[i];
while(a[i]!=-999&&i<100)
   {b[i]=a[i];
   i++;
   in>>a[i];
   }
sort(a,i);
print(a,b,i);
in.close();
return 0;
}
void sort(int a[],int len)
{int i,j,t;
for(i=0;i<len-1;i++)
    for(j=i;j<len;j++)
      if(a[i]<a[j])
         {t=a[i];
         a[i]=a[j];
         a[j]=t;
         }
return;
}
void print(int a[],int b[],int len)
{int i;
ofstream out;
out.open("output.txt");
out<<"unsorted sorted ";
for(i=0;i<len;i++)
   out<<b[i]<<" "<<a[i]<<endl;
out.close();
return;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote