Insert Sorting Random Numbers
CSC404, Data Structures
Programming Assignment 4
Due February 8, 2001
You are to write a program that will
-
Input a string that will be an output file name F, a string that will be
an input file P, and an input an integer N
-
Read N numbers from the file P into an array
-
Sort the array using the Insert Sort
-
Write those N sorted numbers to the output file F, fifteen numbers on each
line.
Each of those four instructions are to be done using functions.
-
Test your program to create a file with a variety of values for N, both
small and large values.
-
Use your program to create a file "Assignment4.txt" containing 100 random
integers.
-
E-mail your text file along with your source code to albert@crawfordenterprise.com
-
When I reply to your e-mail it must get to you
-
The "from" must be your name.
-
The subject must be "CSC404 Assignment 4, Insert Sorting Random Numbers"
Text file input and output:
-
Open a file for input
#include <fstream.h>
fstream FileVar;
FileVar.open(filename, ios::out);
-
Open a file for output
#include <fstream.h>
fstream FileVar;
FileVar.open(filename, ios::in);
-
Close a file
FileVar.close();
-
Read from a file
FileVar >> dataValue;
-
Write to a file
FileVar << value1 << value2;
FileVar << value1 << value2 << endl;
Insert Sort Code
void InsertSort( ItemType SortArray[], int Size )
{ ItemType Temp;
int Pass, K;
for (Pass = 1; Pass < Size; Pass++ )
{ Temp = SortArray[Pass};
// save the item to be inserted
K := Pass;
// start comparing with those items already inserted
while ((K > 0) and (Temp
< SortArray[K-1])) // assending order
{ // proper location
not found, move items down and continue
SortArray[K]
= SortArray[K-1]; // move item down in array
K--;
}
SortArray[K] := Temp;
// place current item to be inserted into its proper place
} // end current Pass
} // end InsertSort