Question: I have already written the code bu i cant figure out what the problem is? the code is given after the question is done. Any

I have already written the code bu i cant figure out what the problem is? the code is given after the question is done. Any help will be appreciated>

Purpose. In this lab you will apply the "4-cycle timing code" presented in the module. The purpose is to prepare you for making big oh determinations and confirming them with timing studies.

Requirements. Write a C++ console app, applying this module's timing test code to the reading of the dvc-schedule.txt (Links to an external site.)Links to an external site. file from lab 6 (DVC Schedule v.1). Do not read the whole file -- just read n lines. Parse as you did in previous labs that used this same input file, but do not check for duplicates and do not count, and no output. Read lines and parse only, timing how long it takes to do so for n lines. Start with n = 9000 or less for the first cycle, and confirm O(n) -- that is, the time it takes to read the file is directly proportional to the number of lines read from the file.

In each of the 4 timing cycles, do this: start the timer, then open the file. Run the EOF loop with duplicate checking and counting removed. After the EOF loop ends, close the file and stop the timer. You should not need any of your H files to support duplicate checking and counting, so do not use one.

Note that your computer may cache your input file, throwing off the timing for the first cycle. So run your program more than once to see it work right.

Example.

1.436 (expected O(n)) for n=8000 2.742 (expected 2.872) for n=16000 5.442 (expected 5.744) for n=32000 10.828 (expected 11.488) for n=64000 

Windows 7 Users NOTE: Old versions of Windows (like 7 and prior) do not have fine enough timing resolution to do this exercise properly. The resolution gives results like 0.0, 0.06, 0.12, 0.18, and so on to the nearest 1/16th of a second. So you may want to apply the following technique, like the one used to test fast algorithms (like O(1) and O(log n)) -- put the timed code in a extra inner loop, by adding the bolded lines of code:

 clock_t startTime = clock( );  for (int rep = 0; rep < 10; rep++)  { // open the file, read and parse n lines, close the file  } clock_t endTime = clock( );

If that's still too fast, use a larger number than 10 for the inner "rep" loop cycles.

HINT: The easiest way to read just "n" lines is to replace "while fin good" with "for (int i = 0; i < n; i++)" in the end-of-file-based parsing loop.

Part 2, Determining And Confirming Big Oh

Purpose. For you to determine big oh for a process and confirm it using the tools provided in this class.

Requirements. Redo part 1 above, but with v.1's parsing + duplicate checking, but still no counting of how many courses were offered by subject code -- and no output. You'll need one of your H files from past labs for the duplicate checking, so submit the needed H file(s) along with the CPP: MyDynamicArray.h and/or MyStaticArray.h.

Time how long it takes to read n lines from the input TXT file. To do so, read n lines from the input TXT file -- do not read all 80,000 of them. Put a counter in the main program loop to break after n lines have been read.

HINT #1. If you start with n = 9,000 for the first cycle, the last (4th) cycle of the n-loop will input 72,000 lines (after three doublings of 9,000). But if you start with an n much higher than that, you'll run out of input in the 4th cycle.

HINT #2. You can actually start with way less than n = 9,000 for the first cycle and still get good results, because the run-time is so slow. You decide...

Part 3, Perform A Timing Study On Nested For-Loop Sorting

Purpose. In this lab you will make your own determination for the big oh of nested for-loop sorting of an array, and confirm your determination.

Requirements. Write a C++ console app, applying the timing test code from the module, to the sorting of a dynamic array of doubles. You decide what you expect the big oh to be, and what n to use for the first cycle. In each cycle, create the array and fill it with random values (using srand and rand). Then start the timer, perform the sort, and stop the timer. Write code to verify that each number in the array is greater or equal to the number before it, starting with the 2nd number. Use assert, like this:

for (int i = 1; i < n; i++) assert (a[i - 1] <= a[i]); 

This way, there's not a lot of output to look through in order to verify that the sorting performed correctly.

Use your own MyStaticArray.h or MyDynamicArray.h or a regular C++ dynamic array, as you wish. If you use your own, submit its H file along with the timing test CPP (if you did not already do so for part 2).

The code I wrote

// Programmers Name :

// Programmers Id:

#define _CRT_SECURE_NO_WARNINGS

// C++ libraries code block

#include

#include

#include

using namespace std;

// C libraries code block

#include // for strtok and strcpy

#include // for assert

#include // for log and pow

#include // for srand and rand

#include // for clock( ), clock_t, time, and CLOCKS_PER_SEC

int main( )

{

cout << "Programmers Name: " << endl;

cout << "Programmers id:" << endl;

cout << "File: " << __FILE__ << endl;

// for parsing the inputfile

char* token;

char buf[1000];

const char* const tab = "\t";

int loopc = 0;

int n = 500 ;

//int count = 0;

string bigOh = "O(n)"; // YOUR PREDICTION: O(n), O(n log n), or O(n squared)

double elapsedSecondsNorm = 0;

double expectedSeconds = 0;

cout.setf(ios::fixed);

cout.precision(4);

// open the input file

ifstream fin;

fin.open("dvc-schedule.txt");

if (!fin.good( )) throw "I/O error";

// read the input file

for (int cycle = 0; cycle <= n; cycle++)

{

// start the timer, do something, and stop the timer

clock_t startTime = clock( );

// do something where n is the "size" of the problem

// Cout flush

//if (count++ == 1000) {cout << '.'; cout.flush( ); count == 0;}

if(++loopc % 500== 0)

{

cout << '.';

cout.flush();

}

// PARSING----------------------------------

string line;

getline(fin, line);

strcpy(buf, line.c_str( ));

if (buf[0] == 0) continue; // skip blank lines

// parse the line

const string term(token = strtok(buf, tab));

const string section(token = strtok(0, tab));

const string course((token = strtok(0, tab)) ? token : "");

const string instructor((token = strtok(0, tab)) ? token : "");

const string whenWhere((token = strtok(0, tab)) ? token : "");

if (course.find('-') == string::npos) continue; // invalid line: no dash in course name

const string subjectCode(course.begin( ), course.begin( ) + course.find('-'));

const string Dup = term + section;

clock_t endTime = clock( );

// compute timing results

double elapsedSeconds = (double)(endTime - startTime); // CLOCKS_PER_SEC; // problem here elapsed second = 0?

double factor = pow(2.0, double(cycle));

if (cycle == 1)

elapsedSecondsNorm = elapsedSeconds;

else if (bigOh == "O(n)")

expectedSeconds = factor * elapsedSecondsNorm;

else if (bigOh == "O(n log n)")

expectedSeconds = factor * log(double(n)) / log(n / factor) * elapsedSecondsNorm;

else if (bigOh == "O(n squared)")

expectedSeconds = factor * factor * elapsedSecondsNorm;

// reporting block

cout << elapsedSeconds;

if (cycle == 0) cout << " (expected " << bigOh << ')';

else cout << " (expected " << expectedSeconds << ')';

cout << " for n=" << n << endl;

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!