Question

So. I'm sick and I need help figuring out whatever is going on with this. #include...

So. I'm sick and I need help figuring out whatever is going on with this.

#include

using namespace std;

//prototypes
int getExchangesInBubbleSort(int[], int);
int getExchangesInSelectionSort(int[], int);

int main()
{
   char choice;
  

   cout << "1. Search Benchmarks\n";
   cout << "2. Sorting Benchmarks\n";

   do
   {
       int input;
       cout << "Please enter the program you would like to run. \n";
       cin >> input;
      
       if (input == 1)
       {
           const int ARRAY_SIZE = 20;
           int a = 0;
           int b = 0;
           int array[ARRAY_SIZE] = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20 };

           int data = 15;   //searching for 15

           //linear search
           for (int i = 0; i < 20; i++)
           {
               if (array[i] == data)
               {
                   a++;
                   break;
               }
               a++;
           }
           //binary
           int low = 0, high = 20 - 1, mid;

           while (low <= high)
               mid = (low + high + 1) / 2;
           if (array[mid] == data)
           {
               b++;
               break;
           }
           else if (array[mid] < data)
           {
               low = mid + 1;
           }
           else
           {
               high = mid - 1;
               b += 2;
           }
           cout << "Using Linear search: " << a << endl;
           cout << "Using Binary search: " << b << endl;
       }
       if (input == 2)
       {
           int exchangesInBubbleSort;
           int exchangesInSelectionSort;

           int array1[20] = { 3,14,15,16,2,7,18,6,5,8,20,4,19,2,12,9,11,10,1,17 };
           int array2[20] = { 3,14,15,16,2,7,18,6,5,8,20,4,19,2,12,9,11,10,1,17 };
          

           exchangesInBubbleSort = getExchangesInBubbleSort(array1, 20);

           cout << "The number of exchanges in bubble sort: " << exchangesInBubbleSort << endl;

           exchangesInSelectionSort = getExchangesInSelectionSort(array2, 20);

           cout << "The number of exchanges in selection sort: " << exchangesInSelectionSort << endl;
       }
       cout << "Would you like to run this program again?(Y/N)\n";
       cin >> choice;
   } while ((choice == 'y') || (choice == 'Y'));

   system("pause");
   return 0;
}

int getExchangesInBubbleSort(int intergers[], int n)
{
   int eCount = 0;
   bool isSwap;
   int current;

   do
   {
       //set the boolean value to false
       isSwap = false;

       for (int i = 0; i < (n - i); i++)
       {
           current = intergers[i];
           intergers[i] = intergers[i + 1];
           intergers[i + 1] = current;

           //set boolean value to true
           isSwap = true;
           {
               eCount++;
           }
       }
   } while (isSwap);

   return eCount;
}

int getExchangesInSelectionSort(int intergers[], int n)
{
   int eCount = 0;
   int smallestinterger;
   int smallestindex;

   for (int i = 0; i < (n - 1); i++)
   {
       smallestinterger = intergers[i];
       smallestindex = i;
       for (int j = (i + 1); j < n; j++)
       {
           if (intergers[j] < smallestinterger)
           {
               smallestinterger = intergers[j];
               smallestindex = j;

               eCount++;
           }
       }
       intergers[smallestindex] = intergers[i];
       intergers[i] = smallestinterger;
   }

   return eCount;
}

When it runs it allows the user to enter their menu choice, but after that it gives no output.

These are the errors it gives:

The thread 0x2288 has exited with code -1073741510 (0xc000013a).
The thread 0x4f24 has exited with code -1073741510 (0xc000013a).
The thread 0x334c has exited with code -1073741510 (0xc000013a).
The thread 0x26f0 has exited with code -1073741510 (0xc000013a).
The thread 0xcd0 has exited with code -1073741510 (0xc000013a).
The program '[15716] Lab6.exe' has exited with code -1073741510 (0xc000013a).

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code - I have made changes in the code , I have commented the changes that I did . Please have a look.

#include<iostream>

using namespace std;

//prototypes
int getExchangesInBubbleSort(int[], int);
int getExchangesInSelectionSort(int[], int);

int main()
{
char choice;
  

cout << "1. Search Benchmarks\n";
cout << "2. Sorting Benchmarks\n";

do
{
int input;
cout << "Please enter the program you would like to run. \n";
cin >> input;
  
if (input == 1)
{
const int ARRAY_SIZE = 20;
int a = 0;
int b = 0;
int array[ARRAY_SIZE] = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20 };

int data = 15; //searching for 15

//linear search
for (int i = 0; i < 20; i++)
{
if (array[i] == data)
{
a++;
break;
}
a++;
}
//binary
int low = 0, high = 20 - 1, mid;

/*here after while loop parantheses were missing , I have added those here*/
while (low <= high){
mid = (low + high + 1) / 2;
if (array[mid] == data)
{
b++;
break;
}
else if (array[mid] < data)
{
low = mid + 1;
}
else
{
high = mid - 1;
b += 2;
}
}
cout << "Using Linear search: " << a << endl;
cout << "Using Binary search: " << b << endl;
}
if (input == 2)
{
int exchangesInBubbleSort;
int exchangesInSelectionSort;

int array1[20] = { 3,14,15,16,2,7,18,6,5,8,20,4,19,2,12,9,11,10,1,17 };
int array2[20] = { 3,14,15,16,2,7,18,6,5,8,20,4,19,2,12,9,11,10,1,17 };
  

exchangesInBubbleSort = getExchangesInBubbleSort(array1, 20);

cout << "The number of exchanges in bubble sort: " << exchangesInBubbleSort << endl;

exchangesInSelectionSort = getExchangesInSelectionSort(array2, 20);

cout << "The number of exchanges in selection sort: " << exchangesInSelectionSort << endl;
}
cout << "Would you like to run this program again?(Y/N)\n";
cin >> choice;
} while ((choice == 'y') || (choice == 'Y'));

system("pause");
return 0;
}

int getExchangesInBubbleSort(int intergers[], int n)
{
int eCount = 0,j=0,i=0;
bool isSwap;
int current;

/* I have changed the functionality for bubble sort because the previous one was giving issue*/
for (i = 0; i < n; ++i)
   {
       for (j = 0; j < n-i-1; ++j)
       {
           // Comparing consecutive data and switching values if value at j > j+1.
           if (intergers[j] > intergers[j+1])
           {
               intergers[j] = intergers[j]+intergers[j+1];
               intergers[j+1] = intergers[j]-intergers[j + 1];
               intergers[j] = intergers[j]-intergers[j + 1];
               eCount++;
           }
       }
       // Value at n-i-1 will be maximum of all the values below this index.
   }  
return eCount;
}

int getExchangesInSelectionSort(int intergers[], int n)
{
int eCount = 0;
int smallestinterger;
int smallestindex;

for (int i = 0; i < (n - 1); i++)
{
smallestinterger = intergers[i];
smallestindex = i;
for (int j = (i + 1); j < n; j++)
{
if (intergers[j] < smallestinterger)
{
smallestinterger = intergers[j];
smallestindex = j;

eCount++;
}
}
intergers[smallestindex] = intergers[i];
intergers[i] = smallestinterger;
}

return eCount;
}

Screenshots -

Pls give thumbs up or hit like.

Add a comment
Know the answer?
Add Answer to:
So. I'm sick and I need help figuring out whatever is going on with this. #include...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need help fixing my code: In C++ *************** 1) I want to sum the digits...

    I need help fixing my code: In C++ *************** 1) I want to sum the digits of an n*n matrix 2) find the average I have completed the rest ****Do not use C++ standard library. You must use pointers and pointer arithmetic to represent the matrix and to navigate through it. MY CODE: (I have indicated at which point I need help) #include <iostream> using namespace std; void swap(int *xp, int *yp) { int temp = *xp; *xp = *yp;...

  • Can you fix this program and run an output for me. I'm using C++ #include using...

    Can you fix this program and run an output for me. I'm using C++ #include using namespace std; //function to calculate number of unique digit in a number and retun it int countUniqueDigit(int input) {    int uniqueDigitCount = 0;    int storeDigit = 0;    int digit = 0;    while (input > 0) {        digit = 1 << (input % 10);        if (!(storeDigit & digit)) {            storeDigit |= digit;       ...

  • Introduction: One of the most important uses of pointers is for dynamic allocation of memory. In...

    Introduction: One of the most important uses of pointers is for dynamic allocation of memory. In C++ there are commands that let the user request a chunk of memory from the operating system, and use this memory to store data. There are also commands to return memory back to the O/S when the program is finished using the data. In this lab, we will explore some of the things that can go wrong when using dynamic memory and discuss how...

  • Need help with a C++ program. I have been getting the error "this function or variable...

    Need help with a C++ program. I have been getting the error "this function or variable may be unsafe" as well as one that says I must "return a value" any help we be greatly appreciated. I have been working on this project for about 2 hours. #include <iostream> #include <string> using namespace std; int average(int a[]) {    // average function , declaring variable    int i;    char str[40];    float avg = 0;    // iterating in...

  • I need help fixing this code and adding a loop please. Here is the original problem: #include <iostream> using namespace std; //function to print seating chart void printSeatingChart(int **cha...

    I need help fixing this code and adding a loop please. Here is the original problem: #include <iostream> using namespace std; //function to print seating chart void printSeatingChart(int **chart) {    int i, j;    cout << "\nROW\t";    for (i = 1; i <= 10; i++)    {        cout << i << "\t";    }    cout << "\n-----------------------------------------------------------------------------------\n";    for (i = 8; i >= 0; i--)    {        cout << "\n" << i + 1 << "\t";        for (j = 0; j < 10; j++)       ...

  • #include <iostream> #include <vector> #include <fstream> #include <time.h> #include <chrono> #include <sstream> #include <algorithm> class Clock...

    #include <iostream> #include <vector> #include <fstream> #include <time.h> #include <chrono> #include <sstream> #include <algorithm> class Clock { private: std::chrono::high_resolution_clock::time_point start; public: void Reset() { start = std::chrono::high_resolution_clock::now(); } double CurrentTime() { auto end = std::chrono::high_resolution_clock::now(); double elapsed_us = std::chrono::duration std::micro>(end - start).count(); return elapsed_us; } }; class books{ private: std::string type; int ISBN; public: void setIsbn(int x) { ISBN = x; } void setType(std::string y) { type = y; } int putIsbn() { return ISBN; } std::string putType() { return...

  • #include #include #include #include #include #include // NOLINT (build/c++11) #include class Clock { private: std::chrono::high_resolution_clock::time_point start;...

    #include #include #include #include #include #include // NOLINT (build/c++11) #include class Clock { private: std::chrono::high_resolution_clock::time_point start; public: void Reset() { start = std::chrono::high_resolution_clock::now(); } double CurrentTime() { auto end = std::chrono::high_resolution_clock::now(); double elapsed_us = std::chrono::duration std::micro>(end - start).count(); return elapsed_us; } }; class books{ private: std::string type; int ISBN; public: void setIsbn(int x) { ISBN = x; } void setType(std::string y) { type = y; } int putIsbn() { return ISBN; } std::string putType() { return type; } }; std::ostream...

  • I need a detailed pseudocode for this code in C ++. Thank you #include <iostream> #include...

    I need a detailed pseudocode for this code in C ++. Thank you #include <iostream> #include <string> #include <iomanip> using namespace std; struct Drink {    string name;    double cost;    int noOfDrinks; }; void displayMenu(Drink drinks[], int n); int main() {    const int size = 5;       Drink drinks[size] = { {"Cola", 0.65, 2},    {"Root Beer", 0.70, 1},    {"Grape Soda", 0.75, 5},    {"Lemon-Lime", 0.85, 20},    {"Water", 0.90, 20} };    cout <<...

  • Need help with my ksmall program. I get an error saying segmentation dump. Thanks #include<iostream> using...

    Need help with my ksmall program. I get an error saying segmentation dump. Thanks #include<iostream> using namespace std; int ksmall(int*, int, int , int); void swap(int*, int*); int main() {     int SIZE = 10;     int target;     int begining=0;     int ending=SIZE-1;     int *array1= new int[SIZE];     cout << "Enter 10 integers: " << endl;     for (int i=0; i<SIZE; i++)     {        cin>>array1[i];     }     cout << " What is the Kth smallest number...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT