Question

Can someone help me with this please: #include <iostream> #include <cstdlib> #include <string> #include <cstring> using namespace std; #define ARSIZ 150000 int debug=0; void du...

Can someone help me with this please:

#include <iostream>
#include <cstdlib>
#include <string>
#include <cstring>

using namespace std;

#define ARSIZ 150000

int debug=0;

void dump(int ar[], int len)
{
for(int i=0; i<len; i++)
{
cout<<" DUMP: data = : "<< ar[i] << endl;
}
}

void sort(int *ar, int length)
{
if(length == 1 || length == 0) return;

cout << "BREAKING DOWN: " << endl;

//for(int i=0; i<length; i++)

// cout << " DUMP: DATA = : " << ar[i] << endl;

int left_length = length/2, right_length = length - (length/2);
int left_list[left_length], right_list[right_length];
for(int i=0; i<right_length; i++)

right_list[i] = ar[i + left_length];

for(int i=0; i<left_length; i++)

left_list[i] = ar[i];
cout << "left = " << endl;


for(int i=0; i<left_length; i++)

cout << " DUMP: data = : " << right_list[i] << endl;

sort(left_list, left_length);
sort(right_list, right_length);

int left_index = 0, right_index = 0;

while(left_index + right_index < length)
{
if(left_list[left_index] < right_list[right_index])
{
if(left_index <left_length)
{
ar[left_index + right_index] = left_list[left_index];
left_index++;
}
else
{
ar[left_index + right_index] = right_list[right_index];
right_index++;
}
}
else
{
if(right_index < right_length)
{
ar[left_index + right_index] = right_list[right_index];
right_index++;
}
else
{
ar[left_index + right_index] = left_list[left_index];
left_index++;
}
}
}
cout << "MERGED: list :" << endl;

for (int i=0; i<length; i++)

cout << " DUMP: data = : " << ar[i] << endl;

}
int main(int argc, char **argv)
{
int ar[ARSIZ];

int i, v, len;

if ((argc == 2) && (strstr(argv[1],"debug"))) debug = 1;

for (i=0; i<ARSIZ; i++) {
cin >> v;

if (v < 0) break;

ar[i] = v;
}

len = i;

cout << "main: before sort:\n";
for (i=0; i<len; i++) {
cout << "main: ar[" << i << "] = " << ar[i] << endl;
}

sort(ar, len);

cout << "\nmain: after sort:\n";
for (i=0; i<len; i++) {
cout << "main: ar[" << i << "] = " << ar[i] << endl;
}
}
And this is what it should print out:

[sigala@cs lab8]$ cat nums.7
3
0
4
2
5
1
6
-1
[sigala@cs lab8-msort]$ lab8 < nums.7
main: before sort:
main: ar[0] = 3
main: ar[1] = 0
main: ar[2] = 4
main: ar[3] = 2
main: ar[4] = 5
main: ar[5] = 1
main: ar[6] = 6

main: after sort:
main: ar[0] = 0
main: ar[1] = 1
main: ar[2] = 2
main: ar[3] = 3
main: ar[4] = 4
main: ar[5] = 5
main: ar[6] = 6

[sigala@cs lab8]$ lab8 debug < nums.7
main: before sort:
main: ar[0] = 3
main: ar[1] = 0
main: ar[2] = 4
main: ar[3] = 2
main: ar[4] = 5
main: ar[5] = 1
main: ar[6] = 6
BREAKING DOWN:
DUMP: data = : 3
DUMP: data = : 0
DUMP: data = : 4
DUMP: data = : 2
DUMP: data = : 5
DUMP: data = : 1
DUMP: data = : 6
left =
DUMP: data = : 3
DUMP: data = : 0
DUMP: data = : 4right =
DUMP: data = : 2
DUMP: data = : 5
DUMP: data = : 1
DUMP: data = : 6
BREAKING DOWN:
DUMP: data = : 3
DUMP: data = : 0
DUMP: data = : 4
left =
DUMP: data = : 3
right =
DUMP: data = : 0
DUMP: data = : 4
BREAKING DOWN:
DUMP: data = : 0
DUMP: data = : 4
left =
DUMP: data = : 0
right =
DUMP: data = : 4
MERGED: list:
DUMP: data = : 0
DUMP: data = : 4
MERGED: list:
DUMP: data = : 0
DUMP: data = : 3
DUMP: data = : 4
BREAKING DOWN:
DUMP: data = : 2
DUMP: data = : 5
DUMP: data = : 1
DUMP: data = : 6
left =
DUMP: data = : 2
DUMP: data = : 5
right =
DUMP: data = : 1
DUMP: data = : 6
BREAKING DOWN:
DUMP: data = : 2
DUMP: data = : 5
left =
DUMP: data = : 2
right =
DUMP: data = : 5
MERGED: list:
DUMP: data = : 2
DUMP: data = : 5

BREAKING DOWN:
DUMP: data = : 1
DUMP: data = : 6
left =
DUMP: data = : 1
right =
DUMP: data = : 6
MERGED: list:
DUMP: data = : 1
DUMP: data = : 6
MERGED: list:
DUMP: data = : 1
DUMP: data = : 2
DUMP: data = : 5
DUMP: data = : 6
MERGED: list:
DUMP: data = : 0
DUMP: data = : 1
DUMP: data = : 2
DUMP: data = : 3
DUMP: data = : 4
DUMP: data = : 5
DUMP: data = : 6

main: after sort:
main: ar[0] = 0
main: ar[1] = 1
main: ar[2] = 2
main: ar[3] = 3
main: ar[4] = 4
main: ar[5] = 5
main: ar[6] = 6

But for some reason is not in order, help please.

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

i st MERGED: DUMP: data: 0 DUMP: data: 4 I i st MERGED: DUMP: data: 0 DUMP: data: 3 DUMP: data: 4 BREAKING DOWN: left- DUMP:

#include <iostream>
#include <cstdlib>
#include <string>
#include <cstring>

using namespace std;

#define ARSIZ 150000

int debug = 0;

void dump(int ar[], int len)
{
    for (int i = 0; i < len; i++) {
        cout << " DUMP: data = : " << ar[i] << endl;
    }
}

void sort(int* ar, int length)
{
    if (length == 1 || length == 0)
        return;

    cout << "BREAKING DOWN: " << endl;

    //for(int i=0; i<length; i++)

    // cout << " DUMP: DATA = : " << ar[i] << endl;

    int left_length = length / 2, right_length = length - (length / 2);
        
    int left_list[left_length], right_list[right_length];

    for (int i = 0; i < left_length; i++)
        left_list[i] = ar[i];
    for (int i = 0; i < right_length; i++)
        right_list[i] = ar[i + left_length];
                
    cout << "left = " << endl;
        dump(left_list, left_length);
    cout << "right = " << endl;
        dump(right_list, right_length);

    sort(left_list, left_length);
    sort(right_list, right_length);

    int left_index = 0, right_index = 0;
        int out_index = 0;

    while ((left_index < left_length) && (right_index < right_length)) {
        if (left_list[left_index] < right_list[right_index]) {
            ar[out_index++] = left_list[left_index++];
        } else if(left_list[left_index] > right_list[right_index]) {
            ar[out_index++] = right_list[right_index++];
        } else {
            ar[out_index++] = left_list[left_index++];
            ar[out_index++] = right_list[right_index++];
        }
    }
        
        // Its possible that one of the list is still remaining
        while(left_index < left_length) {
                ar[out_index++] = left_list[left_index++];
        }
        while(right_index < right_length) {
                ar[out_index++] = right_list[right_index++];
        }
        
    cout << "MERGED: list :" << endl;

    for (int i = 0; i < length; i++)
        cout << " DUMP: data = : " << ar[i] << endl;
}

int main(int argc, char** argv)
{
    int ar[ARSIZ];

    int i, v, len;

    if ((argc == 2) && (strstr(argv[1], "debug")))
        debug = 1;

    for (i = 0; i < ARSIZ; i++) {
        cin >> v;

        if (v < 0)
            break;

        ar[i] = v;
    }

    len = i;

    cout << "main: before sort:\n";
    for (i = 0; i < len; i++) {
        cout << "main: ar[" << i << "] = " << ar[i] << endl;
    }

    sort(ar, len);

    cout << "\nmain: after sort:\n";
    for (i = 0; i < len; i++) {
        cout << "main: ar[" << i << "] = " << ar[i] << endl;
    }
}

please upvote. Thanks!

Add a comment
Know the answer?
Add Answer to:
Can someone help me with this please: #include <iostream> #include <cstdlib> #include <string> #include <cstring> using namespace std; #define ARSIZ 150000 int debug=0; void du...
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 with this code: #include <iostream> #include <cstdlib> #include <string> using namespace std; void...

    I need help with this code: #include <iostream> #include <cstdlib> #include <string> using namespace std; void dump(int ar[], int size) { cout << "\nDUMP [ "; for (int i=0; i<=size; i++) { cout << ar[i] << " "; } cout << "]\n\n"; } void quicksort(int ar[], int start, int end) { cout << "TOP OF SORT ===========================" << endl; dump(ar, start, end); int pivot = ar[end]; int left = start; int right = end - 1; int tmp; cout <<...

  • #include <iostream> #include <cstdlib> using namespace std; int **dynArray(int row, int cols) { int **myPtr; int...

    #include <iostream> #include <cstdlib> using namespace std; int **dynArray(int row, int cols) { int **myPtr; int lab[4]; myPtr = new int *[row]; for(int i = 0; i < row; i++) myPtr[i] = new int[lab[i]]; for(int i = 0; i<row ; i++) if(myPtr[i] == 0) cout<<"empty"; return myPtr; } void getinput(int ID,int &Station,int &labnumb) { cout<<" Enter your ID number: "<<endl; cin>>ID; cout<<" Enter your station number: "<<endl; cin>>Station; cout<<" Enter your lab number: "<<endl; cin>>labnumb; return; } void logout(int ID,int...

  • Consider the following C++ program: #include <iostream> #include <cstdlib> using namespace std; int main int n...

    Consider the following C++ program: #include <iostream> #include <cstdlib> using namespace std; int main int n =0; int i = 0; cout << "Please enter a strictly positive number:"; cin >> n if (n <= 0) exit(EXIT_FAILURE) while (n > 1) n-n/2; i << endl; cout"Output:" return 0; Answer the following questions: What is the output of the program for each of the following values of n: -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9? What does...

  • can someone help me fix my jeopardy game #include<iostream> #include<stdlib.h> using namespace std; int rollDie() {...

    can someone help me fix my jeopardy game #include<iostream> #include<stdlib.h> using namespace std; int rollDie() { return (rand() % 6+1); } void askYoNs(){ cout<<"Do you want to roll a dice (Y/N)?:"<<endl; } void printScores(int turnTotal,int humanTotal,int compTotal){ int player; int human; if(player==human){ cout<<"Your turn total is "<<turnTotal<<endl; } else{ cout<<"computer turn total is "<<turnTotal<<endl; } cout<<"computer: "<<compTotal<<endl; cout<<"human: "<<humanTotal<<endl; cout<<endl; } int human; int changePlayer(int player){ if(player==human) return 1; return human; } int process(int& turnTotal,int roll,int curr_player,int& humanTotal,int& computerTotal){ if(roll==2...

  • #include <iostream> #include <string> #include <cstring> using namespace std; class Node{       private:       ...

    #include <iostream> #include <string> #include <cstring> using namespace std; class Node{       private:        int data;        Node* nextNodePtr;           public:        Node(){}               void setData(int d){            data = d;        }               int getData(){            return data;        }               void setNextNodePtr(Node* nodePtr){                nextNodePtr = nodePtr;        }                ...

  • #include<iostream> #include<string> #include<iomanip> using namespace std; /* ********* Class Car ************* ********************************* */ class Car {...

    #include<iostream> #include<string> #include<iomanip> using namespace std; /* ********* Class Car ************* ********************************* */ class Car { private: string reportingMark; int carNumber; string kind; bool loaded; string choice; string destination; public: Car() { reportingMark = ""; carNumber = 0; kind = "Others"; loaded = 0; destination = "NONE"; } ~Car() { } void setUpCar(string &reportingMark, int &carNumber, string &kind, bool &loaded, string &destination); }; void input(string &reportingMark, int &carNumber, string &kind, bool &loaded,string choice, string &destination); void output(string &reportingMark, int &carNumber,...

  • #include <iostream> #include <string> using std::string; using std::cout; using std::endl; void testAnswer(string testname, int answer, int...

    #include <iostream> #include <string> using std::string; using std::cout; using std::endl; void testAnswer(string testname, int answer, int expected) { if (answer == expected) cout << "PASSED: " << testname << " expected and returned " << answer << "\n"; else cout << "FAILED: " << testname << " returned " << answer << " but expected " << expected << "\n"; } // Implement printArray here void printArray(int array[],int b) { for(int i = 0; i<b;i++) { std::cout<< array[i] << "...

  • Please add a detailed comment for this program. #include<iostream> #include<string> #include<fstream> #include<sstream> #include<cctype> using namespace std;...

    Please add a detailed comment for this program. #include<iostream> #include<string> #include<fstream> #include<sstream> #include<cctype> using namespace std; int is_palindrome(string word){ int len = word.size(); for(int i=0; i<len/2; i++){ if(toupper(word[i])!=toupper(word[len-i-1])) return 0; } return 1; } int have_vowels3(string word){ int cnt = 0; for(int i=0; i<word.size(); i++){ if(tolower(word[i])=='a' || tolower(word[i])=='e' || tolower(word[i])=='i' || tolower(word[i]) =='o' || tolower(word[i]) == 'u') cnt++; } if(cnt>=3) return 1; else return 0; } int have_consecutives(string word){ for(int i=0; i<word.size()-1; i++){ if(tolower(word[i])=='o' && tolower(word[i+1]=='o')) return 1; } return...

  • Explain the output of the following C++ program. #include <iostream> using namespace std; void Magic(int i=1,...

    Explain the output of the following C++ program. #include <iostream> using namespace std; void Magic(int i=1, int j=2,int k=3, double product =1.0) { i+=2; j*=2; k/=2; product=i*j*k; } void Magic(int& i, int& j, double& product) { i+=2; j=j*2+2; product=i*j; } void Magic(int* i,int* j) { double product; *i+=2; *j=*j*2+2; product=*i * *j; } int main() { double product; int i=0,j=0,k=0; product=i*j*k;    Magic(); cout<<"i, j, k and product in main () after 1st round:"<<endl<<i<<endl<<j<<endl<<k<<endl<<product<<endl;    Magic(2,4); cout<<"i, j, k and...

  • I need to update this code: #include <iostream> #include <string> #include <cctype> using namespace std; int...

    I need to update this code: #include <iostream> #include <string> #include <cctype> using namespace std; int main() { string s; cout<< "Enter a string" <<endl; getline (cin,s); cout<< s <<endl; int vowels=0,consonants=0,digits=0,specialChar=0; for (int i=0; i<s.length(); i++) { char ch=s[i]; if (isalpha(s[i])!= 0){ s[i]= toupper(s[i]);    if (ch == 'a'|| ch == 'e'|| ch == 'i'|| ch == 'o' || ch == 'u') vowels++; else consonants++; } else if (isdigit(s[i])!= 0) digits++; else specialChar++; } cout<<"Vowels="<<vowels<<endl; cout<<"Consonants="<<consonants<<endl; cout<<"Digits="<<digits<<endl; cout<<"Special Characters="<<specialChar<<endl;...

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