Question

//Can you write algorithm and comment for the fllowing C++ code. The form of algorithm and comment show in the picture.

7 using namespace std; ALgorithe: that checks what range a given NPG folls into . 1. Take the mpg value passed to the function. 11 12 13 14 15 16 Comments (10 points) 2 Check f tt is greater than se If yes, then print Nice Job . If not, then check f t ts greater than 25 Alorithm If yes, then print Not great, but oay 4. If not, then print So bad, so very, very bad (10 points) Input parometers: iles per gollon (Float type 18Output:dfferent string bosed on three cotegortes of G:50, 25-49, and Less than 25 Returns: nothing 21 23 void checkPG(Float mpg) 50) check f the Input volue ts greater than 5e 25 26 27 28 29 30 31 32 cout ee Nice job e endl; 7 output message lse if(ng25) 7/ff not, check f is greater than 25 cout< ot great, but okay. < endl; /7 output message else / for atLl other volues 35 cout ce So bad, so very, very bad endl; /7 output nessage 37 38 39

void cocktailSort(int pData[], int Count)

{

int iTemp;

int ant = 0;

int ans = 0;

int left = 1;

int right = Count - 1;

int k=0;

if(Count==5&&pData[0]==1&&pData[4]==0)

k = 9;

int t;

do

{

int i;

for (i = right; i >= left; i--)

{

ant++;

if (pData[i]<pData[i - 1])

{

iTemp = pData[i];

pData[i] = pData[i - 1];

pData[i - 1] = iTemp;

t = i;

ans++;

}

}

left = t + 1;

//反向的部分  

for (i = left; i<right + 1; i++)

{

ant++;

if (pData[i]<pData[i - 1])

{

iTemp = pData[i];

pData[i] = pData[i - 1];

pData[i - 1] = iTemp;

t = i;

ans++;

}

}

right = t - 1;

} while (left <= right);

cout<<"Cocktail Sort:"<<endl;

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

cout << pData[i] << " ";

cout << endl;

if(k!=0)

cout << "Number of comparisons: " << 9<< endl;

else if(Count==5&&pData[4]!=0&&pData[4]!=4)

cout << "Number of comparisons: " << 9<< endl;

else if(Count==10)

cout << "Number of comparisons: " << 39<< endl;

else if(Count==27)

cout << "Number of comparisons: " << 260<< endl;

else if(Count==5&&pData[4]==4&&pData[0]==1)

cout << "Number of comparisons: " << 4<< endl;

else

cout << "Number of comparisons: " << ant<< endl;

cout << "Number of swaps: " << ans << endl;

}

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

Algorithm is as below:

procedure cocktailShakerSort( A : unsorted array, Count : size of array ) defined as:

iTemp;

ant = 0; // variable to store number of comparisions

ans = 0; // variable to store number of swaps

left = 1; // variable to store left counter of array

right = Count - 1; // variable to store the size of array

k=0;

if Count==5 and pData[0]==1 and pData[4]==0 then

k = 9 // if input array is like {1,0,0,0,0}, the number of comparisions is 9

do

for each i in right to left do: //iterate from right to left ahd shift the smallest element of array at the start

ant++

if pData[i]<pData[i - 1] then

iTemp = pData[i];

pData[i] = pData[i - 1];

pData[i - 1] = iTemp;

t = i;

ans++;  

end if

end for

left = t + 1;

for each i in left to right+1 do: //iterate from left to right ahd shift the largest element of array at the end

ant++

if pData[i]<pData[i - 1] then

iTemp = pData[i];

pData[i] = pData[i - 1];

pData[i - 1] = iTemp;

t = i;

ans++;

end if

end for

right = t - 1;

while left<=right // the while loop will reamin true untill counter reaches the mid of array i.e. (right + left) /2

//Array is sorted now

print Cocktail sort

for each i in 0 to count-1 do:

print pData[i]

end for

if k!=0 then

print Number of comparision 9

else if Count==5 and pData[4]!=0 and pData[4]!=4 then

print Number of comparision 9

else if Count==10 then

print Number of comparision 39

else if Count==27 then

print Number of comparision 260

else if Count==5 and pData[4]==4 and pData[0]==1 then

print Number of comparision 4

else

print Number of comparision ant

end if

print Number of swaps ans

end procedure

Comments on the code:

void cocktailSort(int pData[], int Count)

{

int iTemp;

int ant = 0; // variable to store number of comparisions

int ans = 0; // variable to store number of swaps

int left = 1; // variable to store left counter of array

int right = Count - 1; // variable to store the size of array

int k=0;

if(Count==5&&pData[0]==1&&pData[4]==0)

k = 9; // if input array is like {1,0,0,0,0}, the number of comparisions is 9

int t;

do

{

int i;

//iterate from right to left ahd shift the smallest element of array at the start

for (i = right; i >= left; i--)

{

ant++; // for each comparisions increase the count of comparision

if (pData[i]<pData[i - 1])

{

//swapping the values, take pData[i] in temporary variable itemp, then assign pData[i-1] to pData[i] and then itemp to pData[i-1]

iTemp = pData[i];

pData[i] = pData[i - 1];

pData[i - 1] = iTemp;

t = i;

ans++; // after every swap increase the count of swap variable

}

}

left = t + 1; //change the counter of left

//iterate from left to right ahd shift the largest element of array at the end

for (i = left; i<right + 1; i++)

{

ant++; // for each comparisions increase the count of comparision

if (pData[i]<pData[i - 1])

{

//swapping the values, take pData[i] in temporary variable itemp, then assign pData[i-1] to pData[i] and then itemp to pData[i-1]

iTemp = pData[i];

pData[i] = pData[i - 1];

pData[i - 1] = iTemp;

t = i;

ans++; // after every swap increase the count of swap variable

}

}

right = t - 1;

} while (left <= right); // the while loop will reamin true untill counter reaches the mid of array i.e. (right + left) /2

//Array is sorted now

cout<<"Cocktail Sort:"<<endl;

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

cout << pData[i] << " ";

cout << endl;

//Below conditions to check how many comparision took place to sort and print the number

if(k!=0)

cout << "Number of comparisons: " << 9<< endl;

else if(Count==5&&pData[4]!=0&&pData[4]!=4)

cout << "Number of comparisons: " << 9<< endl;

else if(Count==10)

cout << "Number of comparisons: " << 39<< endl;

else if(Count==27)

cout << "Number of comparisons: " << 260<< endl;

else if(Count==5&&pData[4]==4&&pData[0]==1)

cout << "Number of comparisons: " << 4<< endl;

else

cout << "Number of comparisons: " << ant<< endl;

//To print total number of swaps

cout << "Number of swaps: " << ans << endl;

}

Add a comment
Know the answer?
Add Answer to:
//Can you write algorithm and comment for the fllowing C++ code. The form of algorithm and...
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 have 2 issues with the C++ code below. The biggest concern is that the wrong...

    I have 2 issues with the C++ code below. The biggest concern is that the wrong file name input does not give out the "file cannot be opened!!" message that it is coded to do. What am I missing for this to happen correctly? The second is that the range outputs are right except the last one is bigger than the rest so it will not output 175-200, it outputs 175-199. What can be done about it? #include <iostream> #include...

  • This is a fill in the code type: // FILL IN THE CODE - Write a...

    This is a fill in the code type: // FILL IN THE CODE - Write a program to multiply 2 numbers, print out the results and print out the original numbers in ascending order. #include <iostream> using namespace std; int main() {   int num1;       // holds 1st number   int num2;       // holds 2nd number   int result;       // holds result of multiplication   int *num1Ptr = nullptr; // int pointer which will be set to point to the 1st number   int *num2Ptr...

  • How can I write this code (posted below) using vectors instead of arrays? This is the...

    How can I write this code (posted below) using vectors instead of arrays? This is the task I have and the code below is for Task 1.3: Generate twenty random permutations of the number 0, 1, 2, ..., 9 using of the algorithms you designed for Task 1.3. Store these permutations into a vector and print them to the screen with the additional information of unchanged positions and number of calls torand(). Calculate the total numbers of unchanged positions. Compare...

  • Example (4) Trace the following program and find the output >> SOURCE CODE #include <iostream.h> int...

    Example (4) Trace the following program and find the output >> SOURCE CODE #include <iostream.h> int main0 // define two integers int x-3; int y = 4; //print out a message telling which is bigger if (x >y) i cout << "x is bigger than y" << endl: else cout << "x is smaller than y" << endl; return 0; Example (5) Write a C++ program that takes from the user a number in SR (Saudi Riyal) then the program...

  • c++, we have to write functions for the code given below and other instructions for it...

    c++, we have to write functions for the code given below and other instructions for it to compile. I am having issues understanding how to confront the problem and how to write functions and read the program so it can eventually be solved so it can be compiled 7/ * INSTRUCTIONS: Write two functions in the space // * indicated below. // * // * #1 => Find index of maximum value: Write a function that will // * find...

  • The Code is C++ // tic tac toe game #include <iostream> using namespace std; const int SIZE = 9; int check(char *); void displayBoard(char *); void initBoard(char *); int main() {    char board[...

    The Code is C++ // tic tac toe game #include <iostream> using namespace std; const int SIZE = 9; int check(char *); void displayBoard(char *); void initBoard(char *); int main() {    char board[SIZE];    int player, choice, win, count;    char mark;    count = 0; // number of boxes marked till now    initBoard(board);       // start the game    player = 1; // default player    mark = 'X'; // default mark    do {        displayBoard(board);        cout << "Player " << player << "(" << mark...

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

  • can you please split this program into .h and .cpp file #include <iostream> #include<string> #include<fstream> #define...

    can you please split this program into .h and .cpp file #include <iostream> #include<string> #include<fstream> #define SIZE 100 using namespace std; //declare struct struct word_block {    std::string word;    int count; }; int getIndex(word_block arr[], int n, string s); int main(int argc, char **argv) {    string filename="input.txt";    //declare array of struct word_block    word_block arr[SIZE];    int count = 0;    if (argc < 2)    {        cout << "Usage: " << argv[0] << "...

  • -can you change the program that I attached to make 3 file songmain.cpp , song.cpp ,...

    -can you change the program that I attached to make 3 file songmain.cpp , song.cpp , and song.h -I attached my program and the example out put. -Must use Cstring not string -Use strcpy - use strcpy when you use Cstring: instead of this->name=name .... use strcpy ( this->name, name) - the readdata, printalltasks, printtasksindaterange, complitetasks, addtasks must be in the Taskmain.cpp - I also attached some requirements below as a picture #include <iostream> #include <iomanip> #include <cstring> #include <fstream>...

  • Required in C++ I'm asked to: Print a histogram in which the total number of times...

    Required in C++ I'm asked to: Print a histogram in which the total number of times the dice rolls equals each possible value is displayed by printing a character like * that number of times. Below is my current code. I am not allowed to use arrays or anything too advanced as I just started the class. I would really appreciate the help as I've been stuck on it for a while now. I can only get it to print...

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