Menu

[Solved]Lab 1 Attached Files Timerh 613 B Intsearchsorttemplateh 402 B Lab 1templatecpp 2393 Kb Do Q37233092

Lab .1

Attached Files:

  • File Timer.h(613 B)
  • FileIntSearchSort_template.h (402 B)
  • File lab1_template.cpp (2.393 KB)

Download lab 1_template.cpp and save it

Download Timer.h and move the file to the same folder as the CPPfile.

#include “Timer.h”start() //will start a stopwatch timerstop() //will stop the stopwatch timerexecutionTime() //returns a double value for duration (in milliseconds)

Create txt file contains all numbers from 1 – 100,000 in randomorder and name it numbers100k.txt and move

the file to the same folder as the CPP file.

Download IntSearchSort_template.h and save it as IntSearchSort.hin the same folder as the CPP file.

Modify your IntSearchSort.h to include the function definitionsfor linear search, binary search, bubble sort, selection sort, andswap.

Modify the CPP file:

  • Header comments
  • Hello message
  • Open data file (and validate it is open)
  • Load arrays
  • Close data file
  • Bubble sort
  • Selection sort
  • Prompt for (valid) guess
  • Linear search
  • Binary search
  • Goodbye message

Compile and run the completed program. Note the number ofmilliseconds for:

  • Loading arrays
  • Bubble sorting
  • Selection sorting
  • Linear search
  • Binary search

File lab1_template.cpp (2.393 KB)

#include “IntSearchSort.h”
#include
#include
#include
using namespace std;

//GLOBAL CONSTANTS
const int SIZE = 100000;
const string DATAFILE = “numbers100k.txt”;

//MAIN PROGRAM
int main()
{
   //declare local variables
   int arrUnsorted[SIZE];
   int arrBubble[SIZE];
   int arrSelection[SIZE];
   int value, guess, position, index = 0;
   ifstream data;
  
   //hello
   //PUT HELLO MESSAGE HERE
  
   //open file
   cout << “nOpening file…”;
   //OPEN DATA FILE AND VALIDATE IT IS OPEN
  
   //load arrays
   cout << “nLoading arrays…”;
   start(); //start clock
   while (index < SIZE)
   {
       //read number from file
       data >> value;
      
       //add number to each array
       //ASSIGN value TO ALL THREE ARRAYSAT index
      
       index++;
   } //end while
   stop(); //stop clock
   cout << ” complete in ” << executionTime()<< ” milliseconds”;
  
   //close file
   //CLOSE THE FILE
  
   //bubble sort arrBubble
   cout << “nBubble sorting array…”;
   start(); //start clock
   //SORT arrBubble USING BUBBLE SORT FUNCTION
   stop(); //stop clock
   cout << ” complete in ” << executionTime()<< ” milliseconds”;
  
   //selection sort arrSelection
   cout << “nSelection sorting array…”;
   start(); //start clock
   //SORT arrSelection USING SELECTION SORTFUNCTION
   stop(); //stop clock
   cout << ” complete in ” << executionTime()<< ” milliseconds”;
  
   //prompt user for guess
   //PROMPT USER FOR GUESS BETWEEN 1 – 100000(VALIDATE!)
  
   //linear search for guess (unsorted array)
   cout << “nLinear searching…”;
   start(); //start the clock
   //USE LINEAR SEARCH FUNCTION TO FIND position OF guessIN arrUnsorted
   stop(); //stop the clock
   cout << ” found at position ” << position<< ” in ” << executionTime() << “milliseconds”;
  
   //binary search for guess (sorted array)
   cout << “nBinary searching…”;
   start(); //start the clock
   //USE BINARY SEARCH FUNCTION TO FIND position OF guessIN arrBubble OR arrSelection
   stop(); //stop the clock
   cout << ” found at position ” << position<< ” in ” << executionTime() << “milliseconds”;
  
   //goodbye
   //PUT GOODBYE MESSAGE HERE
  
   return 0;
} //end main()

File Timer.h (613 B)//Timer.h//Implements functions for tracking execution time#ifndef TIMER_H#define TIMER_H//LIBRARIES#include using namespace std;//for tracking execution timechrono::steady_clock::time_point startTime, endTime;chrono::duration diff;void start(){ startTime = chrono::steady_clock::now();} //end start()void stop(){ endTime = chrono::steady_clock::now();} //end stop()double executionTime(){ diff = endTime – startTime; return 1000 * diff.count();} //end executionTime()#endif //TIMER_H

FileIntSearchSort_template.h (402 B)

//IntSearchSort.h//Implements functions for searching and sorting arrays of integers//PUT YOUR NAME HERE//PUT DATE HERE#ifndef INTSEARCHSORT_H#define INTSEARCHSORT_H//PROTOTYPESint linearSearch(int[], int, int);int binarySearch(int[], int, int);void bubbleSort(int[], int);void selectionSort(int[], int);void swap(int&, int&);//DEFINITONS#endif //INTSEARCHSORT_H

Expert Answer


Answer to Lab .1 Attached Files: Timer.h (613 B) IntSearchSort_template.h (402 B) lab 1_template.cpp (2.393 KB) Download lab 1_tem… . . .

OR


Leave a Reply

Your email address will not be published. Required fields are marked *