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.

What I want to do is to remove that insert sort, and use my own functions. I like to call it insert in order, I want to use three functions for that. I call them (find_index), (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.

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++;
}
}
```

Keep in mind that this is just an idea. But if done correctly I am sure it works. Why not use other methods? Because I am still learning c++ and don't no nothing about pointers or templates yet. On the other hand, you'd have to mention the array size, if you don't want to use poitners and that kind of stuff. I don't want to use classes either, yet. So, 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. Now, here is my problem, I am sure this must work, but no matter what I do I cannot make this to work with my program(There are too many errors to list). There is no doubt that I am making some mistakes. Any ideas how I can make this thing work?

Here is my source code that works 100%. 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 << "\n\nCannot open the input file: " << inputfile <<
"\n\n** 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<<"\n\nThe input data has been read from: " << inputfile << "\nand 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<<"\nThe 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<<"\nThe 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<<"\nThe Standard Deviation is:"<<deviation<<endl;
}
//End of code.
```

*Edited
by modaresi*: Forgot to add something.