Question

This is my code and can you please tell me why it's not working? By the...

This is my code and can you please tell me why it's not working?

By the way, it will work if I reduce 10,000,000 to 1,000,000.

#include <iostream>

using namespace std;

void radixSort(int*a, int n)

{

int intBitSize = sizeof(int)<<3;

int radix = 256;

int mask = radix-1;

int maskBitLength = 8;

  

int *result = new int[n]();

int *buckets = new int[radix]();

int *startIndex = new int[radix]();

  

int flag = 0;

int key = 0;

bool hasNeg = false;

  

while (flag < intBitSize)

{

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

{

key = (a[i] & (mask << flag)) >> flag;

if(key < 0){

key += mask;

hasNeg = true;

}

++buckets[key];

}

  

startIndex[0] = 0;

  

for (int j = 1; j < radix; ++j)

{

startIndex[j] = startIndex[j - 1] + buckets[j - 1];

}

  

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

{

key = (a[i] & (mask << flag)) >> flag;

if(key < 0) key += mask;

result[startIndex[key] + --buckets[key]] = a[i];

}

  

memcpy(a,result,n*sizeof(int));

flag += maskBitLength;

}

  

if(hasNeg)

{

int indexOfNeg = 0;

  

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

{

if(a[i] < 0) {

indexOfNeg = i;

break;

}

}

  

memcpy(a,result+indexOfNeg,(n-indexOfNeg)*sizeof(int));

memcpy(a+(n-indexOfNeg),result,indexOfNeg*sizeof(int));

}

  

delete[] result;

delete[] buckets;

delete[] startIndex;

}

void sort(int *A, int n)

{

radixSort(A, n);

}

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 );

}

Homework Answers

Answer #1

For the error you are facing, first the solution :

Move the array definition

int B[10000000];

outside the main function.

Reason of segmentation fault :

When the array is defined in main, it takes up space on the stack. Unfortunately, the space allocated by the OS on the stack, is much less as compared to the space allocated to data-segment (where all global variables are allocated).

So, defining the array as global (instead of on stack in main() method), the issue is solved.

Know the answer?
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
Similar Questions
We see that this computes the product of two matrices. Add a new kernel code, called...
We see that this computes the product of two matrices. Add a new kernel code, called sum, to compute the sum of the two matrices. #include <stdio.h> #include <math.h> #include <sys/time.h> #define TILE_WIDTH 2 #define WIDTH 6 // Kernel function execute by the device (GPU) __global__ void product (float *d_a, float *d_b, float *d_c, const int n) {    int col = blockIdx.x * blockDim.x + threadIdx.x ;    int row = blockIdx.y * blockDim.y + threadIdx.y ;    float...
Using the C programming language implement Heapsort in the manner described in class. Here is some...
Using the C programming language implement Heapsort in the manner described in class. Here is some example code to use as a guideline. Remember, you need only implement the sort algorithm, both the comparison and main functions have been provided. /* * * after splitting this file into the five source files: * * srt.h, main.c, srtbubb.c, srtinsr.c, srtmerg.c * * compile using the command: * * gcc -std=c99 -DRAND -DPRNT -DTYPE=(float | double) -D(BUBB | HEAP | INSR |...
Write a MIPS assembly program that sorts an array using bubble sort translating the C code...
Write a MIPS assembly program that sorts an array using bubble sort translating the C code int main(void) { int array[] = {10, 2, 7, 5, 15, 30, 8, 6}; // input array int arraySize = sizeof(array)/sizeof(array[0]); bool swapped = true; int j = 0; int tmp; while (swapped) { swapped = false; //Note : "j" , "arraySize - j" are optimizations to the bubble sort algorithm j++; // j= sorted elements int i=0; /* "arraySize - j" is used...
It is about C++linked list code. my assignment is making 1 function, in below circumstance,(some functions...
It is about C++linked list code. my assignment is making 1 function, in below circumstance,(some functions are suggested for easier procedure of making function.) void search_node(struct linked_list* list, int find_node_ value) (The function to make) This function finds the node from the list that value is same with find_node_value and count the order of the node. This function should print message “The order of (node_value) is (order).” and error message “Function search_node : There is no such node to search.”....
What is wrong with my recursive Binary Search code? For some reason my 'middle' value is...
What is wrong with my recursive Binary Search code? For some reason my 'middle' value is always zero? Please fix and explain! #include <stdio.h> #include <stdlib.h> #include <stdbool.h> int BinarySearch(const int A[],const int L,const int R,const int key) {              if (R >= 1){           int middle = L + (R-1)/2;                              if (A[middle] == key)        return A[middle];               if (A[middle] > key){...
can someone edit my c++ code where it will output to a file. I am currently...
can someone edit my c++ code where it will output to a file. I am currently using xcode. #include <iostream> #include <cctype> #include <cstring> #include <fstream> using namespace std; bool inputNum(int [],int&,istream&); void multiply(int[],int,int[],int,int[],int&); void print(int[],int,int,int); int main() {ifstream input; int num1[35],num2[35],len1,len2,num3[60],len3=10,i; input.open("multiplyV2.txt"); //open file if(input.fail()) //is it ok? { cout<<"file did not open please check it\n"; system("pause"); return 1; }    while(inputNum(num1,len1,input)) {inputNum(num2,len2,input); multiply(num1,len1,num2,len2,num3,len3); print(num1,len1,len3,1); print(num2,len2,len3,2); for(i=0;i<len3;i++) cout<<"-"; cout<<endl; print(num3,len3,len3,1); //cout<<len1<<" "<<len2<<" "<<len3<<endl; cout<<endl;    } system("pause"); } void...
C++ Please and thank you. I will upvote Read the following problem, and answer questions. #include<iostream>...
C++ Please and thank you. I will upvote Read the following problem, and answer questions. #include<iostream> using namespace std; void shownumbers(int, int); int main() { int x, y; cout << "Enter first number : "; cin >> x; cout << "Enter second number : "; cin >> y; shownumbers(x, y); return 0; } void shownumbers(int a, int b) { bool flag; for (int i = a + 1; i <= b; i++) { flag = false; for (int j =...
in C++ Please and thanks Here is a list of 6 numbers. Use the selection sort...
in C++ Please and thanks Here is a list of 6 numbers. Use the selection sort algorithm to sort this list. Fill in this table with each iteration of the loop in the selection sort algorithm. Mark the place from which you are looking for the 'next smallest element'. In this display, the upper numbers are the indices, the lower numbers are in the corresponding positions. Use the several rows provided to show the sequence of steps. 0 1 2...
It is N queens problem please complete it use this code //*************************************************************** // D.S. Malik //...
It is N queens problem please complete it use this code //*************************************************************** // D.S. Malik // // This class specifies the functions to solve the n-queens // puzzle. //*************************************************************** class nQueensPuzzle { public: nQueensPuzzle(int queens = 8);     //constructor     //Postcondition: noOfSolutions = 0; noOfQueens = queens;     // queensInRow is a pointer to the array     // that store the n-tuple.     // If no value is specified for the parameter queens,     // the default value, which is 8, is assigned to it. bool...
This code it's not working, fix it for me please #include <iostream> using namespace std; class...
This code it's not working, fix it for me please #include <iostream> using namespace std; class People {    string name;    double height; public:    void setName(string name)    {        this->name = name;    }    void setHeight(double height)    {        this->height = height;    }    double getHeight() {        return height;    }    string getName()    {        return name;    } }; int main() {    const int size...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT