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

Right now, this program asks the user to enter the input infile and outfile file

ID: 3538378 • Letter: R

Question

Right now, this program asks the user to enter the input infile and outfile file names. Then it gives an error if it can't open the file. If it can open infile, it reads the first value and consider that as the array size. After that it continues with other numbers and stores them. Then it sorts them using insert sort, and get the mean, median, and standard deviation. Finally it prints the sorted numbers along with calculated values in the output file.
For this project, you need to remove that insert sort, and use three functions instead. It is called insert in order. Call them (insert_in_order), (insert_at), and (find_index). So, Insert in order works like this:
Insert in order means that you will add a function called find_index that will locate the place in the array that a new value should be inserted. It will then call insert_at, which will move the appropriate, existing elements of the array so that the new element may be inserted in its proper index. So insert_at will be called by find_index, which will be called by insert_in_order. Insert_in_order reads in each new value and calls find_index with it, setting off a chain as described above that ends with each element inserted at the right index so that the array is assembled in order.
I also need you to modify this program and make it print in a nice tabular format like this:

The captured numbers from infile are: 10 66 88

***************************************************************************

------------- ------------- ----------------------------------------------

Median Mean    Standard Deviation

------------- ------------- ----------------------------------------------



66 54.6667 32.8363


Here is an example of what I mean, (the syntax might not be 100% correct though).




While (infile) insertinorder(&infile);

void insertinorder (&infile) {

double nextval;

infile>>nextval;

findindex (nextval);

}

void findindex (nextval) {

int i = 0;

if (lenth ==0) list[0]=maxval; lenth++;

else

while (nextval<list(i)) i++

insertat (nextval, i);

}

void insertat(nextval, i){

int j=0;

if (lenth<max){

for (j=lenth; j>i; j--) list[j]= list (j-1);

list[i]=nextval;

lenth++;

}

}

Do not use classes, pointers or templates.
You can simply read while (infile). Once you get to the end of file it will stop reading. Actually, that would be doing insert in order while (infile). Insert in order then calls find index which calls insert at. Insert at increments the length of the array every time a value is inserted. There is no need for telling the program how large the array is, then, as you can see.
Although not absolutely required, I do like the program to give an error and cease, if it reaches array 200th, so the infile should not contain more than 200 numbers. Again that is not as important as using three functions to sort the numbers.
Below you see my source code that works 100%. However, Right now it is still using insert sort algorithm, and expects the first number infile to be the array size. so, 88 66 10, should be 3 88 66 10, which is what I want changed.









#include <cmath>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>


using namespace std;

const int Max_Size = 200;

void print (ofstream& outdata, double list[], int lenth);
void insertionSort (ofstream& outdata, double list[], int lenth);
void median (ofstream& outdata, double list[], int lenth);
void mean (ofstream& outdata, double list[], int lenth);
void standard_deviation(ofstream& outdata, double list[], int lenth);
void readdata(ifstream& indata, double list[], int& lenth, bool& lenthok);



int main()
{
double dataarray[Max_Size];
int datalenth;
bool lenthisok;

ifstream indata;
ofstream outdata;

string inputfile;
string outputfile;

cout<<"Please enter the input file name:"<<endl;
cin>>inputfile;

indata.open(inputfile.c_str());
if(!indata)
{
cout << " Cannot open the input file: " << inputfile <<
" ** Please make sure the input file path and name are correct and try again." << endl;

return 1;
}

cout<<"Please enter the output file name: "<<endl;
cin>>outputfile;

outdata.open(outputfile);


{

readdata(indata, dataarray, datalenth, lenthisok);

if (lenthisok)
insertionSort(outdata, dataarray, datalenth);


else
cout<<"Lenth of the secret code must be <="<<Max_Size<<endl;

print (outdata, dataarray, datalenth);
median(outdata, dataarray, datalenth);
mean(outdata, dataarray, datalenth);
standard_deviation(outdata, dataarray, datalenth);

cout<<" The input data has been read from: " << inputfile << " and the output data was written to: " << outputfile <<endl;


}



indata.close();
outdata.close();

return 0;
}




void readdata(ifstream& indata, double list[], int& lenth, bool& lenthok)
{
int i;
lenthok = true;

indata>>lenth;
if (lenth>Max_Size)
{
lenthok = false;
return;
}

for (i=0; i<lenth; i++)
indata>>list[i];
}



void insertionSort(ofstream& outdata, double list[], int lenth)
{
int firstoutoforder;
int location;
double temp;


for (firstoutoforder=1; firstoutoforder < lenth; firstoutoforder++)

if (list[firstoutoforder] < list[firstoutoforder-1])
{
temp = list[firstoutoforder];
location = firstoutoforder;

do
{
list[location] = list[location-1];
location--;
}
while (location > 0 && list[location-1] > temp);

list[location] = temp;

}
}



void print (ofstream& outdata, double list[], int lenth)
{
int i;
outdata<<"Using insertion sort algorithm, the elements are:"<<endl;
for (i=0; i<lenth; i++)
outdata<<list[i]<<" ";
}



void median (ofstream& outdata, double list[], int lenth)
{
int middle;
double median;

middle = lenth / 2;

if (lenth % 2==0)
{
median = (list[middle-1] + list[middle]) / 2;
}
else
median = (list[middle]);

outdata<<" The median for the elements entered is:"<<median<<endl;

}



void mean (ofstream& outdata, double list[], int lenth)
{
double sum = 0;
double average = 0;
int i;

for (i=0; i<lenth; i++)
sum = sum+list[i];
average = sum/lenth;

outdata<<" The Mean is:"<<average<<endl;
}




void standard_deviation(ofstream& outdata, double list[], int lenth)
{

double sum = 0;
double average = 0;
double sq_diff_sum = 0;
double variance = 0;
double diff = 0;
int i;
double deviation = 0;

for (i=0; i<lenth; i++)
sum = sum+list[i];
average = sum/lenth;

for (i=0; i<lenth; i++)
{

diff = list[i] - average;
sq_diff_sum += diff * diff;
}

variance = sq_diff_sum/lenth;
deviation = sqrt(variance);
outdata<<" The Standard Deviation is:"<<deviation<<endl;



}

//End of code.

Explanation / Answer

https://www.dropbox.com/s/if95dm4w4j93bx6/2darray.cpp

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