Question

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 you want to determine; Enter a number between 1 to ten:   ";
    cin >> target;
    cout<< ksmall (array1, target, begining, ending);
      delete [] array1;
      array1=nullptr;
       return 0;
}

int ksmall(int* arr, int K, int first, int last)
{

int a=*(arr+first);
int i=first+1;
for(int j=first+1;j<=last; j++)

{
    if(arr[j]<=a)
    {
        swap(*(arr+i), *(arr+j));
         i++;
    }
}


swap(*(arr+i), *(arr+first));

int p= i;

if(K<(p-first+1))
{
    return ksmall(arr, K, first, p-1);
}
else if (K==(p-first+1))
{
    return *(arr+p);
}
else
{
    return ksmall(arr, K-(p-first+1), p+1, last);
}
}


void swap(int *m, int *n)
{
    int temp=*m;
    *m=*n;
    *n=temp;
}

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

#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 you want to determine; Enter a number between 1 to ten:   ";
    cin >> target;
    cout<< ksmall (array1, target, begining, ending);
      delete [] array1;
      array1=NULL;
       return 0;
}

int ksmall(int* arr, int K, int first, int last)
{

int a=*(arr+first);
int i=first;
for(int j=first+1;j<=last; j++)

{
    if(arr[j]<=a)
    {
        swap(*(arr+i), *(arr+j));
         i++;
    }
}


swap(*(arr+i), *(arr+first));

int p= i;

if(K<(p-first+1))
{
    return ksmall(arr, K, first, p-1);
}
else if (K==(p-first+1))
{
    return *(arr+p);
}
else
{
    return ksmall(arr, K-(p-first+1), p+1, last);
}
}


void swap(int *m, int *n)
{
    int temp=*m;
    *m=*n;
    *n=temp;
}

Add a comment
Know the answer?
Add Answer to:
Need help with my ksmall program. I get an error saying segmentation dump. Thanks #include<iostream> using...
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
  • Hello, I need help with my code. The code needs to display random number from 1...

    Hello, I need help with my code. The code needs to display random number from 1 to 50 every time the program runs but the program displays the same random numbers every time. Thanks #include #include using namespace std; void dynAlloc(int size, int *&arr); void displayArray(int *arr, int n); void insertionSort(int *arr, int n, int *temp); void linear_search(int *arr, int n, int key); void binary_search(int *arr, int n, int key); int main() {   const int n = 50; int *arr,...

  • Merge Sort: Time Complexity: O(n log(n)) #include "stdafx.h" #include <iostream> #include <time.h> #include <stdlib.h> using namespace...

    Merge Sort: Time Complexity: O(n log(n)) #include "stdafx.h" #include <iostream> #include <time.h> #include <stdlib.h> using namespace std; void combine(int *a, int low, int high, int mid) {        int i, j, k, c[100000];        i = low;        k = low;        j = mid + 1;        while (i <= mid && j <= high)        {               if (a[i] < a[j])               {                      c[k] = a[i];                      k++;                      i++;               }               else               {                     ...

  • my program wont run on my computer and im not understanding why. please help. #include<iostream> #include<iomanip>...

    my program wont run on my computer and im not understanding why. please help. #include<iostream> #include<iomanip> #include<string> #include "bookdata.h" #include "bookinfo.h" #include "invmenu.h" #include "reports.h" #include "cashier.h" using namespace std; const int ARRAYNUM = 20; BookData bookdata[ARRAYNUM]; int main () { bool userChoice = false; while(userChoice == false) { int userInput = 0; bool trueFalse = false; cout << "\n" << setw(45) << "Serendipity Booksellers\n" << setw(39) << "Main Menu\n\n" << "1.Cashier Module\n" << "2.Inventory Database Module\n" << "3.Report Module\n"...

  • Write a psuedocode for this program. #include <iostream> using namespace std; string message; string mappedKey; void...

    Write a psuedocode for this program. #include <iostream> using namespace std; string message; string mappedKey; void messageAndKey(){ string msg; cout << "Enter message: "; getline(cin, msg); cin.ignore(); //message to uppercase for(int i = 0; i < msg.length(); i++){ msg[i] = toupper(msg[i]); } string key; cout << "Enter key: "; getline(cin, key); cin.ignore(); //key to uppercase for(int i = 0; i < key.length(); i++){ key[i] = toupper(key[i]); } //mapping key to message string keyMap = ""; for (int i = 0,j...

  • 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;...

  • fully comments for my program, thank you will thumb up #include <iostream> #include <fstream> #include <string>...

    fully comments for my program, thank you will thumb up #include <iostream> #include <fstream> #include <string> #include <iomanip> using namespace std; struct book { int ISBN; string Author; string Title; string publisher; int Quantity; double price; }; void choice1(book books[], int& size, int MAX_SIZE) { ifstream inFile; inFile.open("inventory.txt"); if (inFile.fail()) cout <<"file could not open"<<endl; string str;    while(inFile && size < MAX_SIZE) { getline(inFile, str); books[size].ISBN = atoi(str.c_str()); getline(inFile, books[size].Title);    getline(inFile, books[size].Author); getline(inFile, books[size].publisher);          getline(inFile,...

  • 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 <<...

  • I need help with understanding dummy nodes in doubly-linked lists. Here is the code that I...

    I need help with understanding dummy nodes in doubly-linked lists. Here is the code that I have right now. ************city.h**************** #ifndef city_h #define city_h #include <string> using namespace std; class City{ public: City () { name = "N/A"; population = 0; } City (string nm, unsigned int pop){ name = nm; population = pop; } void setName (string name) { this -> name = name; } void setPopulation (unsigned int population){ this -> population = population; } string getName() const...

  • #include <iostream> using namespace std; bool binarySearch(int arr[], int start, int end, int target){ //your code...

    #include <iostream> using namespace std; bool binarySearch(int arr[], int start, int end, int target){ //your code here } void fill(int arr[], int count){ for(int i = 0; i < count; i++){ cout << "Enter number: "; cin >> arr[i]; } } void display(int arr[], int count){ for(int i = 0; i < count; i++){ cout << arr[i] << endl; } } int main() { cout << "How many items: "; int count; cin >> count; int * arr = new...

  • My following program has an array which holds 1000 random integers between 1-1000. Now I need...

    My following program has an array which holds 1000 random integers between 1-1000. Now I need help to create an array that holds 10,000 random integer between 1-1000 in my following program. The main goal of this program is time analysis by using bubble sort and binary search algorithms. Please do the following task; 1. Replace the 1000 random integers with 10,000 random integers After change please answer the following question 2. what will be happen, if an array holds...

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