Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 days ago.
Improve this question
When I pass argument as 0, the function is taking it as -1. As a result, I'm getting a segmentation fault. Please help me out here...
Function definition of build():
void build(int ind, int low, int high, int a[], Node seg[]) {
if(low == high) {
seg[ind].data[0] = a[low];
return;
}
int mid = (low + high) / 1;
build(2*ind+1, low, mid, a, seg);
build(2*ind+2, mid+1, high, a, seg);
seg[ind].data = Merge(seg[2*ind+1].data, seg[2*ind+2].data);
}
Argument passed:
Node seg[4*n];
build(0, 0, n-1, arr, seg);
Results:
When I pass 0 as argument in function call build(), in function its taking ind value as -1;
As you can see when I debug it, its taking ind = -1
Argument passed to build function
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed last month.
Improve this question
I have a project going on, its a student registration system, i have done the most part except the part where i have to do quicksorting to sort my data, the plan is: my program got user to input their names, icno, matric number and their faculty, then if the user choose option '3', the list of data will be sorted alphabetically.. enter image description here, for example from the image above, the program will sort the data so that "Alex" will be first, i am doing my project in vs-studio and its for my data-structure and algorithm subject
void Student::quickSort(string &x,int left, int right)
{
int j = right;
int i = left;
int mid = x [(left + right) / 2];
while (i <= j) {
while (x[i] < mid)
i++;
while (x[j] > mid)
j--;
if (i <= j) {
swap(x[i], x[j]);
i++; j--;
}
};
if (left < j)
quickSort(x, left, j);
if (i < right)
quickSort(x, i , right);
displayStudent();
}
void Student::studentName(string x)
{
x = name[size];
quickSort(x, 0, x.size() - 1);
}
i tried using this code but it never works
From your description, wouldn't it be better to perform insertion sort so that data is always sorted.
Other things does not make sense. for instance :
void Student::studentName(string x)
{
x = name[size];
quickSort(x, 0, x.size() - 1);
}
Function takes string of x and the immediately its overridden with name[size]. You probably wanted to do name[size] = x; size ++;, assuming name is array of vector and its size is more than that of size.
Your function quickSort is actually attempting to sort the characters of the string, and, that is not what you want to do, right?.
Essentially you have 2D array of characters as your input, and the quick-sort you are using only operates on 1D array (here, array of characters).
sample code: http://www.java2s.com/Code/C/Data-Structure-Algorithm/AQuicksortforstrings.htm
other similar thread:
Using quicksort on a string array
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 months ago.
Improve this question
I implemented a binary search, and in theory should run in O(log N) time, and this holds up when counting the number of times it is looped through. However, when it is run, it seems to be extremely slow.
int binary_search(int i, vector<int> list) {
int min_ = 0;
int max_ = list.size();
while (max_ != min_+1) {
if (list[(max_+min_)/2] > i) {
max_ = (max_+min_)/2;
} else if (list[(max_+min_)/2] <= i) {
min_ = (max_+min_)/2;
}
}
return min_;
}
Can anyone explain why my algorithm is so slow?
For starters, you're making a copy of the vector<int> list that is passed in. Change it to be pass by reference:
Instead of this:
int binary_search(int i, vector<int> list) {
This:
int binary_search(int i, const vector<int>& list) {
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I am writing a C ++ program that needs to convert numbers from decimal to binary. Here is my code:
int* convertToBinary(int i, unsigned int n) {
int ans[10000];
if (n / 2 != 0) {
convertToBinary(i + 1, n / 2);
}
ans[i] = n / 2;
return ans;
}
void send_number(int num) {
for (int j = 0; j < 16; j++) {
printf("%d", convertToBinary(0, num)[j]);
}
}
In this case, the num variable takes only natural values from 0 to 65535.
The main function is send_number().
On execution I get the 'Segmentation fault (core dumped)' error. I can't figure out why this is happening.
PS: I am C++ beginner. I don't know English well and use google translator
There are 2 issues at play - scope and (related) dangling pointers.
When you define any variable inside a function - it is only valid inside that function.
convertToBinary returns a pointer that refers to invalid memory. So when you try to print it - you are using
convertToBinary(0, num)[j]
Think about what this does. You take an invalid pointer returned by the function and add an offset j to it.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Let's say I got this Array:
int myArray[] = {2,5,8,3,2,1,9};
is ther any way I could check if some of the contents can add up to 20? I managed to check if any two values add up to 20 but I just don't know how to handle it if is irrelevant how many values it needs.
Thank you for your help.
Most of the time you have a situation where you test if any combination of values satisfy a condition, you need to think of using recursion. You recurse through the elements and at each point branch off in two directions: one that considers that element, and one that doesn't. This can be short-circuited to stop looking if one of the branches does satisfy that condition.
Here's a potential solution to your problem
bool can_sum(const int* ptr, int size, int target, int total = 0)
{
// check success
if (total == target)
return true;
// check failure
if (total > target || size == 0)
return false;
return can_sum(ptr+1, size-1, target, total + *ptr) // check with *ptr
|| can_sum(ptr+1, size-1, target, total); // check without *ptr
}
int main()
{
int arr[] = {2,5,8,3,2,1,9};
bool result = can_sum(arr, 7, 20);
return 0;
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
hey there can anyone tell me how this function work ?!
for function and void function :
int countoccu(int array[],int value,int lower,int upper)
{
int counter=0;
if(lower==upper)
if (array[lower]==value)
return 1;
else
return 0;
else
counter = counter + countoccu(array, value, lower+1, upper);
if (array[lower]==value)
counter++;
return counter;
};
can anyone explain this for me
the output will be 3
void main()
{
int array[5]={3,7,3,3,11};
cout << countoccu(array,3,0,4) << endl;
}
It's a very stupid way to count number of value occurrences, for a given array, in given [upper, lower] range, using recurrence.
(If I understood it good.)
This looks like a homework, so I'll leave figuring it how it happens to you. My hint would be analyze code line by line, with a paper-sheet-pencil debugger.
int countoccu(int array[],int value,int lower,int upper){
int counter=0;
// Check if the end of the array is reached
if(lower==upper)
// Is the last element the "value" we are looking for?
if (array[lower]==value)
// Yes, so count it
return 1;
// No, don't count it
else return 0;
// Not the end of the array
else
// Move the position to the next item in the array and count it and all the following values that equals "value"
counter=counter+countoccu(array,value,lower+1,upper);
// Is the current item equal to the value being counted?
if (array[lower]==value)
// Yes, so count it
counter++;
return counter;
In your example you will get these calls:
countoccu(array,3,0,4) = 1+0+1+1+0 = 3
countoccu(array,3,1,4) = 0+1+1+0 = 2
countoccu(array,3,2,4) = 1+1+0 = 2
countoccu(array,3,3,4) = 1+0 = 1
countoccu(array,3,4,4) = 0 = 0
Though the function is written badly its principle of the work is simple. It checks whether the element with lower index is equal to the given value. If so it increases the count and adds the count for the array starting from the next index after index lower that is lower + 1 (calling itself at this time with lower + 1).
I would rewrite the function the following way
/* constexpr */ size_t count( const int a[], size_t n, int value )
{
return ( n == 0 ? 0 : ( ( a[0] == value ) + count( a + 1, n - 1, value ) ) );
}
I commented specifier constexpr because I think you do not know its meaning. So it may be omited.