Example #1
0
        public static void PoemWriter()
        {
            // Declaring counters
            int pageCount = 0;
            int rowCount  = 0;
            int colCount  = 0;

            // Declare an int array so loop can tell at what page to start writing each poem
            int[] startPoem = new int[] { 9, 29, 399 };


            // Main block for writing poems to console
            try
            {
                for (int poem = 0; poem <= startPoem.Length;)
                {
                    while (pageCount < maxPage)
                    {
                        // If pageCount is greater than or equal to array element designating appropriate starting page, write character to console
                        // Also checks if current startPoem value is not nilPage (already read)
                        if ((pageCount >= startPoem[poem]) && (colCount < maxCol) && (rowCount < maxRow) && (startPoem[poem] != nilPage))
                        {
                            // Writes virtualMemory integer with (char) cast
                            var currentChar = Common_Code.virtualMemory[pageCount, rowCount, colCount];
                            if (currentChar != -99)
                            {
                                Console.Write("{0}", (char)currentChar);
                                ++colCount;
                            }

                            // If end of poem was reached, set current poem to nilPage to designate it as already written, so it does not get written again
                            // Then, increment poem by 1 to start writing the next poem in the process
                            else if (currentChar == -99)
                            {
                                startPoem[poem] = nilPage;
                                ++poem;

                                // Breaks out of while loop if necessary
                                if (poem >= startPoem.Length)
                                {
                                    break;
                                }
                            }
                        }

                        // Increments as needed
                        else if (colCount < maxCol)
                        {
                            Console.Write(' ');
                            ++colCount;
                        }

                        else if (rowCount < maxRow)
                        {
                            ++rowCount;
                            colCount = 0;
                        }

                        else if (rowCount == maxRow)
                        {
                            ++pageCount;
                            rowCount = 0;
                            colCount = 0;
                            Console.Write("Page {0}", pageCount);
                            Common_Code.DisplayFooter();
                        }
                    }

                    // Breaks out of for loop if necessary
                    if (poem >= startPoem.Length)
                    {
                        break;
                    }
                }
            }

            // Catch exceptions from above try block and report some probably useful stuff
            // Shouldn't be possible to reach in normal operation, but useful for debugging!
            catch (Exception e)
            {
                Console.WriteLine("{0}", e);
                Console.WriteLine("Page: {0}, Row: {1}, Col: {2}", pageCount, rowCount, colCount);
                Console.WriteLine("virtualMemory.Length: {0}", Common_Code.virtualMemory.Length);
            }

            // This is where we end up after the last poem in textFiles array is read
            // Run through pages with user prompt at each page, until page 500, where program gets sent back to Main(), prompting exit
            // Seems somewhat redundant but not sure how to write it better, maybe write a method that does this and use it in both places (here and above, in the main poem loop)
            try
            {
                while (pageCount < maxPage)
                {
                    if (colCount < maxCol)
                    {
                        Console.Write(' ');
                        ++colCount;
                    }

                    else if (rowCount < maxRow)
                    {
                        ++rowCount;
                        colCount = 0;
                    }

                    else if (rowCount == maxRow)
                    {
                        ++pageCount;
                        rowCount = 0;
                        colCount = 0;
                        Console.Write("Page {0}", pageCount);
                        Common_Code.DisplayFooter();
                    }
                }
            }

            // Catch exceptions from above try block and report some probably useful stuff
            // Shouldn't be possible to reach in normal operation
            catch (Exception e)
            {
                Console.WriteLine("{0}", e);
                Console.WriteLine("Page: {0}, Row: {1}, Col: {2}", pageCount, rowCount, colCount);
                Console.WriteLine("virtualMemory.Length: {0}", Common_Code.virtualMemory.Length);
            }
        }
Example #2
0
        /// <summary>
        /// Reads all three of the poems (TCOTLB.txt, RC.txt, GEAH.txt) in textDir (C:\devel\TFiles\)
        /// </summary>
        public static void PoemReader()
        {
            string poemOneFilePath   = string.Format(Common_Code.textDir + Common_Code.theChargeOfTheLightBrigade);
            string poemTwoFilePath   = string.Format(Common_Code.textDir + Common_Code.richardCory);
            string poemThreeFilePath = string.Format(Common_Code.textDir + Common_Code.greenEggsAndHam);

            // Reverse array if it hasn't already been reversed
            // Order before reverse: GEAH, RC, TCOTLB; After reverse: TCOTLB, RC, GEAH
            // Since TCOTLB comes at page 10, and GEAH comes at page 400, it makes more sense to me reversed
            if (textFiles[0] == (poemThreeFilePath))
            {
                Array.Reverse(textFiles);
            }

            // Test textFiles array elements to ensure they are the correct poems
            try
            {
                if ((textFiles[0] != poemOneFilePath) || (textFiles[1] != poemTwoFilePath) || (textFiles[2] != poemThreeFilePath))
                {
                    Console.WriteLine("{0}", missingFiles);
                    // Calls method to display "Press enter to continue..." prompt
                    Common_Code.DisplayFooter();
                    PoemReader();
                }
            }

            // Spit out an error if the files are missing, then restart PoemReader method
            catch (IndexOutOfRangeException e)
            {
                Console.WriteLine("{0}", e);
                Console.WriteLine("{0}", missingFiles);
                Common_Code.DisplayFooter();
                PoemReader();
            }

            // Creates 3D virtualMemory array and initializes all locations to -99
            Common_Code.VirtualMemoryInit();

            // Declaring counters
            int pageCount = 0;
            int rowCount  = 0;
            int colCount  = 0;

            // Declare an int array so loop can tell at what page to start reading each poem
            int[] startPoem = Common_Code.GetPoemStartPages();

            // Main block for reading poems into memory
            try
            {
                for (int poem = 0; poem <= startPoem.Length;)
                {
                    using (StreamReader readPoem = new StreamReader(textFiles[poem]))
                    {
                        while (pageCount < maxPage)
                        {
                            // If pageCount is greater than or equal to array element designating appropriate starting page, assign to virtual memory
                            // Also checks if current startPoem value is not nilPage (already read)
                            if ((pageCount >= startPoem[poem]) && (colCount < maxCol) && (rowCount < maxRow) && (startPoem[poem] != nilPage))
                            {
                                // Assigns to virtualMemory integer array then reads it with (char) cast so they are written as characters
                                var currentChar = readPoem.Read();
                                if (currentChar != -1)
                                {
                                    Common_Code.virtualMemory[pageCount, rowCount, colCount] = currentChar;
                                }

                                // If end of file is reached, set current poem to nilPage to designate it as already read, so it does not get read again
                                // Then, increment poem by 1 and break out of while loop so StreamReader can be set with next poem in textFiles array
                                if (currentChar == -1)
                                {
                                    startPoem[poem] = nilPage;
                                    ++poem;
                                    break;
                                }
                            }

                            // Increments to next element in the array
                            if ((colCount < maxCol) && (rowCount < maxRow))
                            {
                                ++colCount;
                            }

                            else if (rowCount < maxRow)
                            {
                                ++rowCount;
                                colCount = 0;
                            }

                            else if (rowCount == maxRow)
                            {
                                ++pageCount;
                                rowCount = 0;
                                colCount = 0;
                            }

                            // Increments pageCount if nothing else is possible to get to the next poem
                            else if (pageCount <= startPoem[poem])
                            {
                                ++pageCount;
                            }
                        }
                        // If poem was incremented so textFiles[poem] would be out of range, break out of for loop
                        if (poem >= textFiles.Length)
                        {
                            break;
                        }
                    }
                }
            }

            // Catch exceptions from above try block and report some probably useful stuff
            // Shouldn't be possible to reach in normal operation, but useful for debugging!
            catch (Exception e)
            {
                Console.WriteLine("{0}", e);
                Console.WriteLine("Page: {0}, Row: {1}, Col: {2}", pageCount, rowCount, colCount);
                Console.WriteLine("virtualMemory.Length: {0}", Common_Code.virtualMemory.Length);
            }
        }
Example #3
0
        /// <summary>
        /// Perform sorts (bucket, frequency, bubble) on a page of randomized integers in virtual memory and log their efficiency.
        /// </summary>
        public static void SortComparisons()
        {
            Common_Code.ShowHeader();

            Common_Code.VirtualMemoryInit();

            int pageToTwoDBucketSort = 200;
            int pageToFrequencySort  = 205;
            int pageToBubbleSort     = 210;

            // Use the default 0 and 1,500,000 as min and max to populate the pages with random numbers.
            // Not counting this towards the logCounter value since it's not part of the sorts being performed.
            PopulatePage(pageToTwoDBucketSort, minValue, maxValue);
            PopulatePage(pageToFrequencySort, minValue, maxValue);
            PopulatePage(pageToBubbleSort, minValue, maxValue);

            // Increment this value by 1 every time a variable (other than the logCount variable itself) changes.
            // Includes variable changes in iterators (e.g. for (int i = 0; i < 100; i++) should increase logCount by 1 for each iteration, for a total of 100).
            // Does not include variable declaration/initialization.
            int logCount = 0;

            // FindMinAndMax() Reassigns minValue and maxValue to whatever the lowest and highest numbers that were generated are (respectively).
            // Tracks how many iterations it takes (17 * 80 = 1360) to find the min and max values so it can be added to logCount.
            // Also, not every sort uses minValue and maxValue - only Frequency Sort and Bucket Sort, so I'm only increasing logCount by this for those sorts.
            int findMinAndMax = FindMinAndMax(pageToTwoDBucketSort);

            #region Sorts
            #region 2-Dimensional Bucket Sort
            // Set logCount to findMinAndMax since BucketSort uses them
            logCount += findMinAndMax;

            // Get the number of times to log based on operations performed during the bucket sort (i.e. total variable assignments it took to sort).
            logCount += TwoDBucketSortPage(pageToTwoDBucketSort);

            Week_10_Class.WriteEfficiencyLog(pageSize, logCount, Common_Code.arrayBucketSort);

            Console.WriteLine("After bucket sort on page {0}, is it sorted?: {1}", pageToTwoDBucketSort, IsPageSorted(pageToTwoDBucketSort));
            Common_Code.DisplayFooter();
            #endregion

            #region Frequency Sort
            // Reset logCount to findMinAndMax for frequency sort.
            logCount = findMinAndMax;

            logCount += FrequencySortPage(pageToFrequencySort);

            Week_10_Class.WriteEfficiencyLog(pageSize, logCount, Common_Code.frequencySort);

            Console.WriteLine("After frequency sort on page {0}, is it sorted?: {1}", pageToFrequencySort, IsPageSorted(pageToFrequencySort));
            Common_Code.DisplayFooter();
            #endregion

            #region Bubble Sort
            // Reset logCount to 0 for bubble sort.
            logCount = 0;

            // Get number of times to log based on bubble sort operations.
            logCount += LazyBubbleSortPage(pageToBubbleSort);

            Week_10_Class.WriteEfficiencyLog(pageSize, logCount, Common_Code.bubbleSort);

            Console.WriteLine("After bubble sort on page {0}, is it sorted?: {1}", pageToBubbleSort, IsPageSorted(pageToBubbleSort));
            Common_Code.DisplayFooter();
            #endregion
            #endregion

            // Writes locations in virtual memory to a log, excluding -99 (virtualNull), so the sorts can be manually checked for correctness.
            Common_Code.VirtualMemoryLog(12, false);
        }