The world’s Largest Sharp Brain Virtual Experts Marketplace Just a click Away
Levels Tought:
Elementary,Middle School,High School,College,University,PHD
| Teaching Since: | Apr 2017 |
| Last Sign in: | 103 Weeks Ago, 3 Days Ago |
| Questions Answered: | 4870 |
| Tutorials Posted: | 4863 |
MBA IT, Mater in Science and Technology
Devry
Jul-1996 - Jul-2000
Professor
Devry University
Mar-2010 - Oct-2016
Complete the given project to help in analyzing complexity of algorithms.
Â
#include <ctime>
#include <iostream>
using namespace std;
int n = 60, arr[1000], key, samples=40, sampleResults[100]; float scale=.5;
int execTime = 0; // complexity
int getSearchKey(){ cout << "Enter keyn"; cin >> key; return (key); }
int seqSearch(){ int i = 0; while (i<n && key != arr[i]) i++; if (key == arr[i]) return(i); return(-1);}
int bSearchIteration(int first, int last) { return -1; }//to complete by students
int bSearchRecursion(int first, int last)//provided to student
{int mid = (first + last) / 2;
if (first > last) return -1; else
{if (arr[mid] > key) { execTime++; bSearchRecursion( mid + 1, last); } else
if (arr[mid] < key) { execTime++; bSearchRecursion(first, mid - 1); } else
if (arr[mid] == key) return mid;
}
}
void plot(){for (int i = 0; i < n; i++)//repeat for n lines
{for (int j = 0; j < arr[i] * scale; j++) cout << ' ';//skip the given spaces for that line
cout << '*' << endl;//draw * and go to the next line
}
}
void initArray(){ for (int i = 0; i < n; i++) arr[i] = 0; }
void generArr(){for (int x = 0; x < n; x++) arr[x] = rand() % n; cout << "data has been generated successfullyn";}
void printArr(){for (int i = 0; i < n; i++) cout <<i<<")t"<< arr[i] << endl; }
void sortArr(){ for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) if (arr[i]>arr[j]){ int t = arr[i]; arr[i] = arr[j]; arr[j] = t;} }
void plotSeqSearchTimes(){
for (int i = 0; i < samples; i++) { generArr(); sampleResults[i] = seqSearch(); }//analysis for seqSearch
initArray(); for (int i = 0; i < samples; i++) arr[i] = sampleResults[i]; plot();
}
void plotBinSearchTimesIterative(){} //for students to complete
void plotBinSearchTimesRecursive(){} //for students to complete
void menu()
{while (1){int choice;
cout << "nSTARTING MENUn";
srand(time(0));
cout << "nn 0-exitn 1-seqSearchn 2-bSearchIterationn 3-bSearchRecursionn 4-plot()n 5-generArr()n 6-printArr()n";
cout << "n 7-plotSeqSearchTimes()n 8-plotBinSearchTimesIterative()n 9-plotBinSearchTimesRecursive()n10-getSearchKey()n";cin >> choice;
switch (choice)
{case 0: exit(0);
case 1: key = getSearchKey(); cout << seqSearch() << endl; break;
case 2: key = getSearchKey(); execTime = 0; cout << bSearchIteration(0, n); break;//for students to complete
case 3: key = getSearchKey(); execTime = 0; cout << bSearchRecursion(0, n); break;
case 4: plot(); break;
case 5: generArr(); break;
case 6: printArr(); break;
case 7: cout << "Enter samples numbert"; cin >> samples; plotSeqSearchTimes(); break;
case 8: cout << "Enter samples numbert"; cin >> samples; plotBinSearchTimesIterative(); break;//for students to complete
case 9: cout << "Enter samples numbert"; cin >> samples; plotBinSearchTimesRecursive(); break;//for students to complete
case 10: key = getSearchKey(); break;
case 11: sortArr(); break; //SortArr() must run before any BinSearch() //IMPORTANT NOTE
}
}
}
void main()
{srand(time(0)); menu();
system("pause");
}