Values not going to external file

This is a discussion on Values not going to external file within the C++ Programming forums, part of the General Programming Boards category; I have compiled a program which is supposed to read one array from one file, read a second array from ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    30

    Values not going to external file

    I have compiled a program which is supposed to read one array from one file, read a second array from a second file, sort each array separately, then merge the sorted arrays into a third array, which should then be printed out to an external file. When I open the external file, there is nothing there. Can someone please help me figure out what I need to do to get this to work? Here is my code
    Code:
    //main
    //  openFiles
    //  populateInputArray
    //  mergeSortedArrays
    //  selSort
    //    findIndexOfMin
    //    exchange
    //  saveResultsToOutputFile
    
    
    /* The format of the input data files is multiple lines, each containing one or more double values:
    
    12345.44 90.1
    23456.1
    31245 66 567.11
    ......
    */
    
    #include <iostream>
    #include <string>
    #include <fstream>
    #include <iomanip>
    
    using namespace std;
    
    
    const int MAX_SIZE = 100; // max size of data file and arrays
    const bool DEBUG = true; // used during testing
    //const bool DEBUG = false; // used during production
    
    
    void openFiles (ifstream &inFile1, ifstream& inFile2, ofstream& outFile);
    void populateInputArray (ifstream& inFile, double array[], int& arraySize);
    void selSort (double array[], int arraySize);
    int findIndexOfMin(const double x[], int startIndex, int endIndex);
    void exchange (double& a1, double& a2);
    void mergeSortedArrays (double array1 [], int array1Size, double array2 [], int array2Size, double array3[], int& array3Size);
    void saveResultsToOutputFile (ofstream& outFile, double array [], int arraySize);
    
    
    int main()
    {
    	ifstream in1, in2;
    	ofstream out;
    	double array1[MAX_SIZE];
    	double array2[MAX_SIZE];
    	double array3[MAX_SIZE];
    	int array1Size, array2Size, array3Size=0;
    
    	openFiles (in1, in2, out);
    	populateInputArray (in1, array1, array1Size);
    	populateInputArray (in2, array2, array2Size);
    	selSort(array1, array1Size);
    	selSort(array2, array2Size);
    	saveResultsToOutputFile (out, array3, array3Size);
    
    	in1.close();
    	in2.close();
    
    	return 0;
    }
    
    void openFiles (ifstream& inFile1, ifstream& inFile2, ofstream& outFile)
    {
    	string fileName;
    
    	cout << "Enter name of the first input file" << endl;
    	cin >> fileName;
    
    	inFile1.open (fileName.c_str());
    	if (inFile1.fail())
    	{
    		cout << "Could not open file " << fileName << endl;
    		cout << "Exiting program." << endl;
    		exit (0);
    	}
    
    	cout << "Enter name of the second input file" << endl;
    	cin >> fileName;
    	
    
    	inFile2.open (fileName.c_str());
    	if (inFile2.fail())
    	{
      		cout << "Could not open file " << fileName << endl;
      		cout << "Exiting program." << endl;
      		exit (0);
    	}
    	
    
    	cout << "Enter name of the output file" << endl;
    	cin >> fileName;
    
    	outFile.open (fileName.c_str());
    	if (outFile.fail())
    	{
    		cout << "Could not open file " << fileName << endl;
    		cout << "Exiting program." << endl;
    		exit (0);
    	}
    } 
    void populateInputArray (ifstream& inFile, double array[], int& arraySize)
    {
    	for (int i = 0; i < MAX_SIZE; i++)
    	{
    		if (inFile.eof())
    		{
    			break;
    		}		
    		else 
    		{
                inFile >> array[i];
    		}
    	}
    	arraySize = sizeof(array)/sizeof(array[i]);//gets size of array
    
    }
    
    void mergeSortedArrays (double array1 [], int array1Size, double array2 [], int array2Size, double array3[], int& array3Size)
    {
    		int count = 0;
    		int a;
    		while ((count < array1Size) && (count < array2Size))
    		{
    			for (a = 0; a < MAX_SIZE; a++)
    			{
    				if (array1[a] < array2[a])
    				{
    					array3[a] = array1[a];
    				}
    				else
    				{
    					array3[a] = array2[a];
    				}
    				if (array1[a] == array2[a])
    				{
    					array3[a] = array3[a];
    				}
    				count++;
    			}
    		}
    
    	// at this point one of the arrays has been completely read
    	cout << "One of the arrays is empty" << endl;
    		if ( count < array1Size)
    		{
    			for (int b = a+1; b < array1Size; b++)
    			{
    				array3[b] = array1[b];
    				count++;
    			}
    		}
    		if ( count < array2Size)
    		{
    			for (int c = a+1; c < array2Size; c++)
    			{
    				array3[c] = array2[c];
    				count++;
    			}
    		}
    		array3Size = sizeof(array3)/sizeof(array3[count]);//gets size of array
    }
    
    void selSort(double array[], int arraySize)
    {
       // Local data ...
       int minSub;  // subscript of each smallest item
                    
    
       for (int i = 0; i < arraySize-1; i++)
       {
          // Find index of smallest element in unsorted section of 
          //    items.
          minSub = findIndexOfMin(array, i, arraySize-1);
    
          // Exchange items at position minSub and i
          exchange(array[minSub], array[i]);
    	  //cout << 'exchange' << '\n';
       }
    } 
    
    
    int findIndexOfMin
       (const double array[],       // IN: array of elements
        int startIndex,        // IN: subscript of first element
        int endIndex)          // IN: subscript of last element
    {
       // Local data ...
       int minIndex;            // index of the smallest element found
       int i;                   // index of the current element
    
       // Validate subarray bounds
       if ((startIndex < 0) || (startIndex > endIndex))
       {
          cerr << "Error in subarray bounds" << endl;
          return -1;                        // return error indicator
       }
    
       // Assume the first element of subarray is smallest and check 
       //   the rest.
       // minIndex will contain subscript of smallest examined so far.
       minIndex = startIndex;
       for (i = startIndex + 1; i <= endIndex; i++)
          if (array[i] < array[minIndex])
             minIndex = i;
    
       // All elements are examined and minIndex is
       //    the index of the smallest element.
       return minIndex;                    // return result
    } // end findIndexOfMin
    
    void exchange (double& smElem, double& value2)
    {
    	double valueHolder;//holds the value of the subscript of the smallest element
    
    	valueHolder = smElem;
    	
    	smElem = value2;
    	value2 = valueHolder;
    
    }
    void saveResultsToOutputFile (ofstream& outFile, double array[], int arraySize)
    {
    	for (int i = 0 ; i < arraySize; i++)
    	{
    		outFile << array[i] << '\n';
    	}
    
    	outFile.close();
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,555
    > arraySize = sizeof(array)/sizeof(array[i]);//gets size of array
    This only works when the declaration of the array is in scope.
    Here you just get the size of the pointer to the start of the array, since that is all that is passed as a parameter.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    array3Size is always zero.
    Code:
    int main()
    {
    	ifstream in1, in2;
    	ofstream out;
    	double array1[MAX_SIZE];
    	double array2[MAX_SIZE];
    	double array3[MAX_SIZE];
    	int array1Size, array2Size, array3Size=0;
    
    	openFiles (in1, in2, out);
    	populateInputArray (in1, array1, array1Size);
    	populateInputArray (in2, array2, array2Size);
    	selSort(array1, array1Size);
    	selSort(array2, array2Size);
    	saveResultsToOutputFile (out, array3, array3Size);
    
    	in1.close();
    	in2.close();
    
    	return 0;
    }
    So of course there'll be nothing in the file.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    void populateInputArray (ifstream& inFile, double array[], int& arraySize)
    {
    	for (int i = 0; i < MAX_SIZE; i++)
    	{
    		if (inFile.eof())
    		{
    			break;
    		}		
    		else 
    		{
                inFile >> array[i];
    		}
    	}
    	arraySize = sizeof(array)/sizeof(array[i]);//gets size of array
    
    }
    The code in red "doesn't work": arraySize will be zero on a 32-bit machine and 1 on a 64-bit machine (becaue sizeof(array) == sizeof(void *) == 4 or 8 (32/64-bit machine) and sizeof(array[i]) is 8. 4/8 => 0.

    You need to cound the number of elements you are getting (of course you can use "i").

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    30

    Unhappy

    Quote Originally Posted by dwks View Post
    array3Size is always zero.
    Code:
    int main()
    {
    	ifstream in1, in2;
    	ofstream out;
    	double array1[MAX_SIZE];
    	double array2[MAX_SIZE];
    	double array3[MAX_SIZE];
    	int array1Size, array2Size, array3Size=0;
    
    	openFiles (in1, in2, out);
    	populateInputArray (in1, array1, array1Size);
    	populateInputArray (in2, array2, array2Size);
    	selSort(array1, array1Size);
    	selSort(array2, array2Size);
    	saveResultsToOutputFile (out, array3, array3Size);
    
    	in1.close();
    	in2.close();
    
    	return 0;
    }
    So of course there'll be nothing in the file.
    I tried not initializing array3Size at all, but then when I run my program, I get an error "The variable 'array3Size' is being used without being defined." I couldn't figure out the problem there, so I initialized it to 0. I have initialized array3Size to MAX_SIZE, and I don't an error anymore, and there is something being printed to the file, but it's 100 rows of the number "-9.25596e+061". What am I doing wrong?
    Code:
    //main
    //  openFiles
    //  populateInputArray
    //  mergeSortedArrays
    //  selSort
    //    findIndexOfMin
    //    exchange
    //  saveResultsToOutputFile
    
    
    /* The format of the input data files is multiple lines, each containing one or more double values:
    
    12345.44 90.1
    23456.1
    31245 66 567.11
    ......
    */
    
    #include <iostream>
    #include <string>
    #include <fstream>
    #include <iomanip>
    
    using namespace std;
    
    
    const int MAX_SIZE = 100; // max size of data file and arrays
    const bool DEBUG = true; // used during testing
    //const bool DEBUG = false; // used during production
    
    
    void openFiles (ifstream &inFile1, ifstream& inFile2, ofstream& outFile);
    void populateInputArray (ifstream& inFile, double array[], int& arraySize);
    void selSort (double array[], int arraySize);
    int findIndexOfMin(const double x[], int startIndex, int endIndex);
    void exchange (double& a1, double& a2);
    void mergeSortedArrays (double array1 [], int array1Size, double array2 [], int array2Size, double array3[], int& array3Size);
    void saveResultsToOutputFile (ofstream& outFile, double array [], int arraySize);
    
    
    int main()
    {
    	ifstream in1, in2;
    	ofstream out;
    	double array1[MAX_SIZE];
    	double array2[MAX_SIZE];
    	double array3[MAX_SIZE];
    	int array1Size, array2Size, array3Size = MAX_SIZE;
    
    	openFiles (in1, in2, out);
    	populateInputArray (in1, array1, array1Size);
    	populateInputArray (in2, array2, array2Size);
    	selSort(array1, array1Size);
    	selSort(array2, array2Size);
    	saveResultsToOutputFile (out, array3, array3Size);
    
    	in1.close();
    	in2.close();
    
    	return 0;
    }
    
    void openFiles (ifstream& inFile1, ifstream& inFile2, ofstream& outFile)
    {
    	string fileName;
    
    	cout << "Enter name of the first input file" << endl;
    	cin >> fileName;
    
    	inFile1.open (fileName.c_str());
    	if (inFile1.fail())
    	{
    		cout << "Could not open file " << fileName << endl;
    		cout << "Exiting program." << endl;
    		exit (0);
    	}
    
    	cout << "Enter name of the second input file" << endl;
    	cin >> fileName;
    	
    
    	inFile2.open (fileName.c_str());
    	if (inFile2.fail())
    	{
      		cout << "Could not open file " << fileName << endl;
      		cout << "Exiting program." << endl;
      		exit (0);
    	}
    	
    
    	cout << "Enter name of the output file" << endl;
    	cin >> fileName;
    
    	outFile.open (fileName.c_str());
    	if (outFile.fail())
    	{
    		cout << "Could not open file " << fileName << endl;
    		cout << "Exiting program." << endl;
    		exit (0);
    	}
    } 
    void populateInputArray (ifstream& inFile, double array[], int& arraySize)
    {
    	for (int i = 0; i < MAX_SIZE; i++)
    	{
    		if (inFile.eof())
    		{
    			break;
    		}		
    		else 
    		{
                inFile >> array[i];
    		}
    	}
    	arraySize = i;//gets size of array
    
    }
    
    void mergeSortedArrays (double array1 [], int array1Size, double array2 [], int array2Size, double array3[], int& array3Size)
    {
    		int count = 0;
    		int a;
    		while ((count < array1Size) && (count < array2Size))
    		{
    			for (a = 0; a < MAX_SIZE; a++)
    			{
    				if (array1[a] < array2[a])
    				{
    					array3[a] = array1[a];
    				}
    				else
    				{
    					array3[a] = array2[a];
    				}
    				if (array1[a] == array2[a])
    				{
    					array3[a] = array3[a];
    				}
    				count++;
    			}
    		}
    
    	// at this point one of the arrays has been completely read
    	cout << "One of the arrays is empty" << endl;
    		if ( count < array1Size)
    		{
    			for (int b = a+1; b < array1Size; b++)
    			{
    				array3[b] = array1[b];
    				count++;
    			}
    		}
    		if ( count < array2Size)
    		{
    			for (int c = a+1; c < array2Size; c++)
    			{
    				array3[c] = array2[c];
    				count++;
    			}
    		}
    		array3Size = count;//gets size of array
    }
    
    void selSort(double array[], int arraySize)
    {
       // Local data ...
       int minSub;  // subscript of each smallest item
                    
    
       for (int i = 0; i < arraySize-1; i++)
       {
          // Find index of smallest element in unsorted section of 
          //    items.
          minSub = findIndexOfMin(array, i, arraySize-1);
    
          // Exchange items at position minSub and i
          exchange(array[minSub], array[i]);
    	  //cout << 'exchange' << '\n';
       }
    } 
    
    
    int findIndexOfMin
       (const double array[],       // IN: array of elements
        int startIndex,        // IN: subscript of first element
        int endIndex)          // IN: subscript of last element
    {
       // Local data ...
       int minIndex;            // index of the smallest element found
       int i;                   // index of the current element
    
       // Validate subarray bounds
       if ((startIndex < 0) || (startIndex > endIndex))
       {
          cerr << "Error in subarray bounds" << endl;
          return -1;                        // return error indicator
       }
    
       // Assume the first element of subarray is smallest and check 
       //   the rest.
       // minIndex will contain subscript of smallest examined so far.
       minIndex = startIndex;
       for (i = startIndex + 1; i <= endIndex; i++)
          if (array[i] < array[minIndex])
             minIndex = i;
    
       // All elements are examined and minIndex is
       //    the index of the smallest element.
       return minIndex;                    // return result
    } // end findIndexOfMin
    
    void exchange (double& smElem, double& value2)
    {
    	double valueHolder;//holds the value of the subscript of the smallest element
    
    	valueHolder = smElem;
    	
    	smElem = value2;
    	value2 = valueHolder;
    
    }
    void saveResultsToOutputFile (ofstream& outFile, double array[], int arraySize)
    {
    	for (int i = 0 ; i < arraySize; i++)
    	{
    		outFile << array[i] << '\n';
    	}
    
    	outFile.close();
    }

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Actually calling mergeSortedArrays() would be a start . . .
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User
    Join Date
    Mar 2006
    Posts
    30
    Quote Originally Posted by dwks View Post
    Actually calling mergeSortedArrays() would be a start . . .
    Thanks...I can't believe I missed that! Well I called mergeSortedArrays, and now the output has 3 rows of numbers from one of my data files, and then 97 rows of -9.25596E+61.

    One data file has the following values:
    -10.5
    -1.8
    3.5
    6.3
    7.2
    and the other one has the values:
    -1.8
    3.1
    6.3

    The values in the output file are as follows:
    -10.5
    -1.8
    3.5
    97 rows of -9.25596E+61.

    I figure that the reason so many rows are being printed out is somehow because of how I initialized the variable array3Size. I know I originally set array3Size = 100 but shouldn't that value change to the count?

    The program is supposed to weed out duplicates, but either way, my code is not working as I planned it.
    Code:
     
    //main
    //  openFiles
    //  populateInputArray
    //  mergeSortedArrays
    //  selSort
    //    findIndexOfMin
    //    exchange
    //  saveResultsToOutputFile
    
    
    /* The format of the input data files is multiple lines, each containing one or more double values:
    
    12345.44 90.1
    23456.1
    31245 66 567.11
    ......
    */
    
    #include <iostream>
    #include <string>
    #include <fstream>
    #include <iomanip>
    
    using namespace std;
    
    
    const int MAX_SIZE = 100; // max size of data file and arrays
    const bool DEBUG = true; // used during testing
    //const bool DEBUG = false; // used during production
    
    
    void openFiles (ifstream &inFile1, ifstream& inFile2, ofstream& outFile);
    void populateInputArray (ifstream& inFile, double array[], int& arraySize);
    void selSort (double array[], int arraySize);
    int findIndexOfMin(const double x[], int startIndex, int endIndex);
    void exchange (double& a1, double& a2);
    void mergeSortedArrays (double array1 [], int array1Size, double array2 [], int array2Size, double array3[], int& array3Size);
    void saveResultsToOutputFile (ofstream& outFile, double array [], int arraySize);
    
    
    int main()
    {
    	ifstream in1, in2;
    	ofstream out;
    	double array1[MAX_SIZE];
    	double array2[MAX_SIZE];
    	double array3[MAX_SIZE];
    	int array1Size, array2Size, array3Size;
    
    	openFiles (in1, in2, out);
    	populateInputArray (in1, array1, array1Size);
    	populateInputArray (in2, array2, array2Size);
    	mergeSortedArrays(array1, array1Size, array2, array2Size, array3, array3Size);
    	selSort(array1, array1Size);
    	selSort(array2, array2Size);
    	saveResultsToOutputFile (out, array3, array3Size);
    
    	in1.close();
    	in2.close();
    
    	return 0;
    }
    
    void openFiles (ifstream& inFile1, ifstream& inFile2, ofstream& outFile)
    {
    	string fileName;
    
    	cout << "Enter name of the first input file" << endl;
    	cin >> fileName;
    
    	inFile1.open (fileName.c_str());
    	if (inFile1.fail())
    	{
    		cout << "Could not open file " << fileName << endl;
    		cout << "Exiting program." << endl;
    		exit (0);
    	}
    
    	cout << "Enter name of the second input file" << endl;
    	cin >> fileName;
    	
    
    	inFile2.open (fileName.c_str());
    	if (inFile2.fail())
    	{
      		cout << "Could not open file " << fileName << endl;
      		cout << "Exiting program." << endl;
      		exit (0);
    	}
    	
    
    	cout << "Enter name of the output file" << endl;
    	cin >> fileName;
    
    	outFile.open (fileName.c_str());
    	if (outFile.fail())
    	{
    		cout << "Could not open file " << fileName << endl;
    		cout << "Exiting program." << endl;
    		exit (0);
    	}
    } 
    void populateInputArray (ifstream& inFile, double array[], int& arraySize)
    {
    	for (int i = 0; i < MAX_SIZE; i++)
    	{
    		if (inFile.eof())
    		{
    			break;
    		}		
    		else 
    		{
                inFile >> array[i];
    		}
    	}
    	arraySize = i;//gets size of array
    
    }
    
    void mergeSortedArrays (double array1 [], int array1Size, double array2 [], int array2Size, double array3[], int& array3Size)
    {
    		int count = 0;
    		int a;
    		while ((count < array1Size) && (count < array2Size))
    		{
    			for (a = 0; a < MAX_SIZE; a++)
    			{
    				if (array1[a] < array2[a])
    				{
    					array3[a] = array1[a];
    				}
    				else
    				{
    					array3[a] = array2[a];
    				}
    				if (array1[a] == array2[a])
    				{
    					array3[a] = array3[a];
    				}
    				count++;
    			}
    		}
    
    	// at this point one of the arrays has been completely read
    	cout << "One of the arrays is empty" << endl;
    		if ( count < array1Size)
    		{
    			for (int b = a+1; b < array1Size; b++)
    			{
    				array3[b] = array1[b];
    				count++;
    			}
    		}
    		if ( count < array2Size)
    		{
    			for (int c = a+1; c < array2Size; c++)
    			{
    				array3[c] = array2[c];
    				count++;
    			}
    		}
    		array3Size = count;//gets size of array
    }
    
    void selSort(double array[], int arraySize)
    {
       // Local data ...
       int minSub;  // subscript of each smallest item
                    
    
       for (int i = 0; i < arraySize-1; i++)
       {
          // Find index of smallest element in unsorted section of 
          //    items.
          minSub = findIndexOfMin(array, i, arraySize-1);
    
          // Exchange items at position minSub and i
          exchange(array[minSub], array[i]);
    	  //cout << 'exchange' << '\n';
       }
    } 
    
    
    int findIndexOfMin
       (const double array[],       // IN: array of elements
        int startIndex,        // IN: subscript of first element
        int endIndex)          // IN: subscript of last element
    {
       // Local data ...
       int minIndex;            // index of the smallest element found
       int i;                   // index of the current element
    
       // Validate subarray bounds
       if ((startIndex < 0) || (startIndex > endIndex))
       {
          cerr << "Error in subarray bounds" << endl;
          return -1;                        // return error indicator
       }
    
       // Assume the first element of subarray is smallest and check 
       //   the rest.
       // minIndex will contain subscript of smallest examined so far.
       minIndex = startIndex;
       for (i = startIndex + 1; i <= endIndex; i++)
          if (array[i] < array[minIndex])
             minIndex = i;
    
       // All elements are examined and minIndex is
       //    the index of the smallest element.
       return minIndex;                    // return result
    } // end findIndexOfMin
    
    void exchange (double& smElem, double& value2)
    {
    	double valueHolder;//holds the value of the subscript of the smallest element
    
    	valueHolder = smElem;
    	
    	smElem = value2;
    	value2 = valueHolder;
    
    }
    void saveResultsToOutputFile (ofstream& outFile, double array[], int arraySize)
    {
    	for (int i = 0 ; i < arraySize; i++)
    	{
    		outFile << array[i] << '\n';
    	}
    
    	outFile.close();
    }

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    		while ((count < array1Size) && (count < array2Size))
    		{
    			for (a = 0; a < MAX_SIZE; a++)
    			{
    				if (array1[a] < array2[a])
    				{
    					array3[a] = array1[a];
    				}
    				else
    				{
    					array3[a] = array2[a];
    				}
    				if (array1[a] == array2[a])
    				{
    					array3[a] = array3[a];
    				}
    				count++;
    			}
    		}
    What's the purpose of the red code? Doesn't the "else" part of the previous if do a good enough job of setting array3[a], you feel that it's necessary to MAKE sure the number that is in there is REALLY in there by writing it back again? :-)

    Also you have two loops, one that goes 0..MAX_SIZE, and one that ends when count is greater than one of the two arraysize values. How many times do you think you need to select the values out of the two arrays? Going through once shoule be sufficient.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Mar 2006
    Posts
    30
    Quote Originally Posted by matsp View Post
    Code:
    		while ((count < array1Size) && (count < array2Size))
    		{
    			for (a = 0; a < MAX_SIZE; a++)
    			{
    				if (array1[a] < array2[a])
    				{
    					array3[a] = array1[a];
    				}
    				else
    				{
    					array3[a] = array2[a];
    				}
    				if (array1[a] == array2[a])
    				{
    					array3[a] = array3[a];
    				}
    				count++;
    			}
    		}
    What's the purpose of the red code? Doesn't the "else" part of the previous if do a good enough job of setting array3[a], you feel that it's necessary to MAKE sure the number that is in there is REALLY in there by writing it back again? :-)

    Also you have two loops, one that goes 0..MAX_SIZE, and one that ends when count is greater than one of the two arraysize values. How many times do you think you need to select the values out of the two arrays? Going through once shoule be sufficient.

    --
    Mats

    I removed the for loop and now the final value is not printed 97 times, but it still appears that the files are not being merged correctly. I kept that last if in, but I realized that I typed it in wrong, so I changed it. If the same value is in both files, it should only be in the third array once. It looks like the files are not merging correctly, but I cannot figure out what is wrong. Here is the function that is supposed to merge the arrays into one.

    My input values are:
    12345.44 90.1
    23456.1
    31245 66 567.11
    -10.5
    -1.8
    3.5
    6.3
    7.2
    15.42
    -65.945

    and

    -1.8
    3.1
    6.3
    12345.44 90.1
    23456.1
    31245 66 567.11
    152.42
    6235.89
    942.1

    My output is:
    -1.80
    3.10
    6.30
    12345.44
    66.00
    567.11
    -10.50
    -1.80
    3.50
    6.30
    7.20
    15.42


    My output should have 14 lines, but it's missing some. Here is the code for my merge function
    Code:
    void mergeSortedArrays (double array1 [], int array1Size, double array2 [], int array2Size, double array3[], int& array3Size)
    {
    		int count = 0;
    		
    		while ((count < array1Size) && (count < array2Size))
    		{
    				if (array1[count] < array2[count])
    				{
    					array3[count] = array1[count];
    				}
    				else
    				{
    					array3[count] = array2[count];
    				}
    				if (array1[count] == array2[count])
    				{
    					array3[count] = array1[count];
    				}
    				count++;
    		}
    
    		// at this point one of the arrays has been completely read
    		cout << "One of the arrays is empty" << endl;
    
    		do
    		{
    			for (count; count < array1Size; count++)
    			{
    				array3[count] = array1[count];
    				
    			}
    		}while (count < array1Size);
    
    		do 
    		{
    			for (count; count < array2Size; count++)
    			{
    				array3[count] = array2[count];
    			
    			}
    		}while ( count < array2Size);
    		array3Size = count-1;//gets size of array
    }

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Code:
    array3Size = count-1;//gets size of array
    I think you need to subtract 1, like that, after both for loops, no?

    BTW, for loop sections can be empty. That is,
    Code:
    for (count; count < array2Size; count++)
    is the same as
    Code:
    for ( ; count < array2Size; count++)
    This code is still completely redundant.
    Code:
    				if (array1[count] == array2[count])
    				{
    					array3[count] = array1[count];
    				}
    Here's why. Imagine that array1[count] == array2[count]. This is false:
    Code:
    if (array1[count] < array2[count])
    So the code in the else is executed, namely
    Code:
    array3[count] = array2[count];
    Now, this is true:
    Code:
    if (array1[count] == array2[count])
    So this is executed.
    Code:
    array3[count] = array1[count];
    But since array1[count] == array2[count], you've already set array3[count] to that value. It's like you're assigning the same value again.

    Just leave out that second if entirely.

    Also, why the variable count? You could just use array3Size directly.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 04:02 AM
  3. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  4. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  5. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21