A[0] A[1] A[2] A[3] A[4] Step 1: Of the unsorted elements in array A, find the i
ID: 3622224 • Letter: A
Question
A[0] A[1] A[2] A[3] A[4]
Step 1: Of the unsorted elements in array A, find the index of the smallest element.In this example, the value 2 is stored in A[4]
Step 2: Swap the element currently in position 0 with the element in position
42 16 4 23 18
A[0] A[1] A[2] A[3] A[4]
Step 3: Of the unsorted elements in array A, find the index of the smallest element.In this example, the value 4 is stored in A[2]
Step 4: Swap the element currently in position 1 with the element in position 2
2 4 16 23 18
A[0] A[1] A[2] A[3] A[4]
Step 5: Of the unsorted elements in array A, find the index of the smallest element.In this example, the value 16 is stored in A[2]
Step 6: Swap the element currently in position 2 with the element in position 2
2 4 16 23 18
A[0] A[1] A[2] A[3] A[4]
Step 7: Of the unsorted elements in array A, find the index of the smallest element.In this example, the value 18 is stored in A[4]
Step 8: Swap the element currently in position 3 with the element in position 4
2 4 16 18 23
A[0] A[1] A[2] A[3] A[4]
Step 7: Of the unsorted elements in array A, find the index of the smallest element.In this example, the value 23 is stored in A[4]
Step 8: Swap the element currently in position 4 with the element in position 4
2 4 16 18 23
A[0] A[1] A[2] A[3] A[4]
Note that there are two basic operations that are repeated until all the elements are sorted:
- Locating the “smallest” of the unsorted elements
- Swapping the position of smallest unsorted element with that of the first unsorted element
Each of these operations can be implemented as a separate function.
Descending Sort Algorithm – what is required in the programInstead of locating the smallest unsorted element and moving it to the lead position, locate the largestunsorted element and move it to the lead
position using a swap function. The code that finds the “largest”unsorted element is easily implemented as a function.
input files
P12_empty.txt
P12_error1.txt
817A6354
P12_error2.txt
B17A6354
P12_in8.txt
81726354
P12_in15.txt
987151432110654131211
P12_in22.txt
15141312111022212019181716987654321
README.txt
################################################################To receive the full 30 points for the extra credit program, yourprogram must correctly handle the input files:
P12_empty.txtP12_in8.txtP12_in15.txtP12_in22.txt
Output of your program should match that of the sample solutionincluding all messages.#################################################################
#################################################################For the +5 extra points, the program should be able to handlethe input files:
P12_error1.txtP12_error2.txt
With outputs as shown by the sample solution.#################################################################
important!!!!!
don't use system function.
Explanation / Answer
please rate - thanks
#include <iostream>
#include <cmath>
#include<fstream>
#include <iomanip>
using namespace std;
double mean(int [],int,int&);
double standardd(double);
double calcvariance(int[],int, double);
double calc(int, double);
void sort(int[],int);
int main()
{int x[20],i=0,j,kt=0,max=20,sum=0;
double average,sd,variance;
char filename[30];
ifstream input;
cout<<"what is the name of the file you are using? ";
cin>>filename;
input.open(filename);
while(input.fail())
{ cout<<"file did not open please check it ";
input.clear();
cout<<"what is the name of the file you are using? ";
cin>>filename;
input.open(filename);
}
kt=0;
input>>x[kt];
while(kt<max-2&&input)
{ kt++;
input>>x[kt];
}
cout<<kt<<" numbers were read ";
cout<<"The numbers are: ";
for(j=0;j<kt;j++)
cout<<x[j]<<" ";
cout<<endl;
sort(x,kt);
cout<<"After sorting the numbers are: ";
for(j=0;j<kt;j++)
cout<<x[j]<<" ";
cout<<endl;
average=mean(x,kt,sum);
cout<<"The sum is "<<sum<<" ";
cout<<"The average is "<<average<<" ";
variance=calcvariance(x,kt,average);
cout<<"The variance is "<<setprecision(2)<<fixed<<variance<<" ";
cout<<"The standard deviation is "<<setprecision(2)<<fixed<<standardd(variance)<<" ";
system("pause");
return 0;
}
void sort(int x[],int n)
{int i,j,t;
for(i=0;i<n-1;i++)
for(j=i;j<n;j++)
if(x[i]<x[j])
{t=x[i];
x[i]=x[j];
x[j]=t;
}
}
double mean(int x[],int n,int &sum)
{int i;
for(i=0;i<n;i++)
sum+=x[i];
if(n==0)
return 0;
else
return (double)sum/n;
}
double calc(int x,double avg)
{double temp;
temp=x-avg;
return temp*temp;
}
double calcvariance(int x[],int n, double average)
{double mean, sum=0;
int i;
for(i=0;i<n;i++)
sum+=calc(x[i],average);
if(n<=1)
mean=0;
else
mean=sum/(n-1);
return mean;
}
double standardd(double variance)
{return sqrt(variance);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.