Question

In: Computer Science

Please make my Code working and pass the test but do NOT change anything in main...

Please make my Code working and pass the test but do NOT change anything in main function, thank you.

#include <iostream>

using namespace std;

void sort(int *A, int n){

  

for(int passes = 0;passes < 2;passes++)

{

// shift can have only two values either 0 or 16, used for shifting purpose

int shift = passes * 16;

int N = 1<<(16 + 1);

  

// Temporary array for storing frequency of upper or lower 16 bits

int temp[N];

  

// initialze all value to zero at first

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

temp[i] = 0;

// storing the frequecy of either lower or upper half the every number

for(int i = 0;i < n;i++){

temp[(A[i] >> shift)&0xFFFF]++;

}

// making cummulative sum of the frequecies

for(int i = 1;i < N;i++)

temp[i] += temp[i-1];

// A temporary output array for storing ns

int output[n];

  

// storing the number in their requied position

for(int i = n - 1;i >= 0;i--)

{

output[ temp[(A[i] >> shift)&(0xFFFF)] - 1] = A[i];

temp[ (A[i] >> shift)&(0xFFFF) ]--;

}

  

// copy the values of temporary output array to original array

for(int i = 0;i < n;i++){

A[i] = output[i];

}

}

}

void sort( int *A, int n);

int main()

{

int i, offset, j;

int B[10000000];

time_t t;

srand( (unsigned) time( &t ));

offset = rand()%10000000;

for( i = 0; i< 10000000; i++ )

{

B[i] = ((91*i)%10000000) + offset;

}

printf("Prepared array of 10 million integers; calling sort\n");

sort( B, 10000000);

printf("finished sort, now check result\n");

for( i=0, j=0; i < 10000000; i++ )

if( B[i] != i+ offset ) j++;

if( j == 0 )

printf("Passed Test\n");

else

printf("Failed Test. %d numbers wrong.\n", j );

}

Solutions

Expert Solution

Solution- This code works successfully in Gcc 6.3, C++14 environment. Maybe, there is issue in the compiler itself.

Screenshot of code and IO -

In the following screenshot it is clear that the code gives the output that shows test cases passed.

Code

#include <iostream>

using namespace std;

void sort(int *A, int n){

  

for(int passes = 0;passes < 2;passes++)

{

// shift can have only two values either 0 or 16, used for shifting purpose

int shift = passes * 16;

int N = 1<<(16 + 1);

  

// Temporary array for storing frequency of upper or lower 16 bits

int temp[N];

  

// initialze all value to zero at first

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

temp[i] = 0;

// storing the frequecy of either lower or upper half the every number

for(int i = 0;i < n;i++){

temp[(A[i] >> shift)&0xFFFF]++;

}

// making cummulative sum of the frequecies

for(int i = 1;i < N;i++)

temp[i] += temp[i-1];

// A temporary output array for storing ns

int output[n];

  

// storing the number in their requied position

for(int i = n - 1;i >= 0;i--)

{

output[ temp[(A[i] >> shift)&(0xFFFF)] - 1] = A[i];

temp[ (A[i] >> shift)&(0xFFFF) ]--;

}

  

// copy the values of temporary output array to original array

for(int i = 0;i < n;i++){

A[i] = output[i];

}

}

}

void sort( int *A, int n);

int main()

{

int i, offset, j;

int B[10000000];

time_t t;

srand( (unsigned) time( &t ));

offset = rand()%10000000;

for( i = 0; i< 10000000; i++ )

{

B[i] = ((91*i)%10000000) + offset;

}

printf("Prepared array of 10 million integers; calling sort\n");

sort( B, 10000000);

printf("finished sort, now check result\n");

for( i=0, j=0; i < 10000000; i++ )

if( B[i] != i+ offset ) j++;

if( j == 0 )

printf("Passed Test\n");

else

printf("Failed Test. %d numbers wrong.\n", j );

}

Related Solutions

C++: Do not change anything in the supplied code below that will be the Ch16_Ex5_MainProgram.cpp except...
C++: Do not change anything in the supplied code below that will be the Ch16_Ex5_MainProgram.cpp except to add documentation. PLEASE DO NOT CHANGE THE CODE JUST ADD DOCUMENTATION. Thank you. The last few have changed the code. Appreciate you all. Please use the file names listed below since your file will have the following components: Ch16_Ex5_MainProgram.cpp - given file //22 34 56 4 19 2 89 90 0 14 32 88 125 56 11 43 55 -999 #include <iostream> #include...
Please finish this code and make it work. This is my homework and my professor wont...
Please finish this code and make it work. This is my homework and my professor wont allow me to change the code in main, it's a set of huge numbers need to sort by radixsort with bit operation. #include <iostream> using namespace std; void radixLSD_help(int *items, int length, int bit) {    // – Count number of items for each bucket.    // – Figure out where each bucket should be stored (positions // of the first and last element...
Implement the following function by replacing the pass keyword with your code. Do not change the...
Implement the following function by replacing the pass keyword with your code. Do not change the spelling of the function name or the parameter list. def order_pie(): Important notes: Ask ONLY these questions, in EXACTLY this order. Do not ask more questions than are necessary to arrive at the right answer. You can expect the input values to be 'yes' or 'no'. For this assignment you do not have to worry about other spellings or capitalization. The return values must...
Working with Python. I am trying to make my code go through each subject in my...
Working with Python. I am trying to make my code go through each subject in my sample size and request something about it. For example, I have my code request from the user to input a sample size N. If I said sample size was 5 for example, I want the code to ask the user the following: "Enter age of subject 1" "Enter age of subject 2" "Enter age of subject 3" "Enter age of subject 4" "Enter age...
Create all necessary code to make this main function work. It is not allowed to change...
Create all necessary code to make this main function work. It is not allowed to change the main function. int main() {        int ListDataSample1[] = { 1, 1, 1 };        int ListDataSample2[] = { 2, 2, 2 };        List<int> List1 = List<int>(ListDataSample2, 3);        List<int> List2 = List<int>(ListDataSample2, 3);               cout << "List1 :" << List1 << endl;        cout << "List2 :" << List2 << endl << endl;        List1 += List2;               cout...
can someone finish and check my code on main. cpp? Its not working for me even...
can someone finish and check my code on main. cpp? Its not working for me even though im sure my code make sense is it possible to output each function to show they work. this is supposed to be a vector class library made from allocated memory i have included templated functions in the class file to help create the rest of the functions. Thank you so much note: i did not include main.cpp because it  was empty- im hoping someone...
My current code that is not working correctly #include<stdio.h> #include<math.h> int main() { //variable declaration int...
My current code that is not working correctly #include<stdio.h> #include<math.h> int main() { //variable declaration int a,b,c,D,n; double x1,x2; double realPart, imagPart; do { // this set of code blocks prompts a message to the user and then read the integer entered and stores it as an integer printf("Enter a value for a:\n"); scanf("%d",&a); printf("Enter a value for b:\n"); scanf("%d",&b); printf("Enter a value for c:\n"); scanf("%d",&c); printf("You entered the Equation \n"); printf("%dx^2%+dx%+d=0\n",a,b,c); D = b*b - 4*a*c;    if (D<0)...
Please add to this Python, Guess My Number Program. Add code to the program to make...
Please add to this Python, Guess My Number Program. Add code to the program to make it ask the user for his/her name and then greet that user by their name. Please add code comments throughout the rest of the program If possible or where you add in the code to make it ask the name and greet them before the program begins. import random def menu(): print("\n\n1. You guess the number\n2. You type a number and see if the...
Enter your answers in the empty code chunks. Don't change anything in the chunk below, and...
Enter your answers in the empty code chunks. Don't change anything in the chunk below, and make sure you run it before attempting any of the problems: ```{r message=FALSE, warning=TRUE} library(tidyverse) library(ggpubr) set.seed(2018) # Belgium win 3rd place in the World Cup ``` # Basics Calculate $\frac{(2+2)\times (3^2 + 5)}{(6/4)}$: ```{r basics1} # your code here ``` Create a vector called "x" with the following values: 10, 15, 18, 20. ```{r basics2} # your code here ``` Calculate the mean...
I'm really confused Junit test case Here is my code. How can I do Junit test...
I'm really confused Junit test case Here is my code. How can I do Junit test with this code? package pieces; import java.util.ArrayList; import board.Board; public class Knight extends Piece {    public Knight(int positionX, int positionY, boolean isWhite) {        super("N", positionX, positionY, isWhite);    }    @Override    public String getPossibleMoves() {        ArrayList<String> possibleMoves = new ArrayList<>();               // check if squares where knight can go are available for it       ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT