Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
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).




C++
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.









C++
#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.
Posted
Comments
[no name] 28-Jun-13 21:12pm    
If you read the rules for asking a question you will see this is not a question.
[no name] 28-Jun-13 21:28pm    
I have read the rules and this is indeed a question.
[no name] 28-Jun-13 21:37pm    
2.Be specific! Don't ask "I need to write a booking application". Specify exactly what it is you need help with.

3.Keep the subject brief but descriptive. eg "How do I change the dialog colour?"

4.Keep the question as concise as possible. If you have to include code, include the smallest snippet of code you can - do not dump your entire codebase

I have read the whole thing and have no idea what you are asking.
[no name] 28-Jun-13 21:57pm    
No.... it is not any kind of a question. You are just dumping your code here and asking someone to fix it for you.
The_Inventor 29-Jun-13 1:36am    
You have 2 competing, identically called functions, (find_index) in your 'question', and yet I haven't found a '?'.

If you want override a function that is inherited, use the 'virtual' command override.

virtual void Insert(); as an example in the header, then whatever it is you want to do with it is in your function version in your .cpp file.


1 solution

The following shows how to use arrays.

//	Shuffle 2 Piles
void CLayoutView::Shuffle2()
{
	CCardDeck tmpArray;  UINT i, j, k;
	tmpArray.SetSize(78,-1);
	while ((UINT)m_cardPile1.GetSize() && (UINT)m_cardPile2.GetSize())
	{
		for (j = 0; j < 78; j++)
		{
			srand(((UINT)time(NULL)* (UINT)time(NULL))/(rand() +1));
			i = ((UINT)rand() * (UINT)m_cardPile1.GetSize())/RAND_MAX;
				tmpArray.SetAt(j, m_cardPile1.GetAt(i));
				tmpArray.SetAt(j+1, m_cardPile2.GetAt(i));
				j++;
			m_cardPile1.RemoveAt(i,1);
			m_cardPile2.RemoveAt(i,1);
		}
	}
	
	m_pTarotCardDeck->RemoveAll();
	m_pTarotCardDeck->SetSize(0,-1);
	m_pTarotCardDeck->SetSize(78,-1);

	for (k = 0; k <78; k++)
	{
		m_pTarotCardDeck->SetAt(k, tmpArray.GetAt(k));
	}
}


You could also try counting delimiters with in the text file.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900