trying to write heapify algorithm - segmentation fault - c++

I'm ultimately trying to use heapsort to alphabetically sort words that have been read in. I've never done heaps before so I'm trying to follow along with my book. I'm using cin to store the words into a dynamically allocated array, as number of words is unknown ahead of time. From separate code I know it is being read in and the array is getting larger correctly. Then I'm trying to heapify this array, but I keep getting a segmentation fault as I'm new to programming, I can't determine how to trace this back to what I did wrong. This is my heapify code:
void Heap::make(){
//make a heap from wordArray
//r = last non-leaf
for(int r = size/2; r > 1; r--){
int c = 2 * r; //location of left child
while(r <= size){ //size is a data member of Heap
//if r has 2 children and right is larger, make c the right child
if((c < size) && (wordArray[c] < wordArray[c+1])){
c++;
}
//fix if parent failed heap-order condition
if(wordArray[r] < wordArray[c]){
swap(wordArray[r], wordArray[c]);
r = c; //check that it didn't get messed up at c
c = 2 * c;
}
else{
break; //heap-order condition holds so stop
}
}
}
}
from playing around with couts I can determine that the program works until the if(wordArray[r] < wordArray[c]) part. The elements of wordArray are stings, and the comparators work correctly from outside testing. Is this something to do with the array being dynamic or something? I'm confused as to what I'm doing wrong here.

You are reading out of bounds. When you check:
if((c < size)
where c is the last element, you read out of bounds here:
if((c < size) && (wordArray[c] < wordArray[c+1])){
^
The check should be:
if((c+1 < size)
And yet another problem is here:
while(r <= size)
where r is used in the code when it is clearly out of bounds if it r == size.
For arrays of size, n, their elements go from 0 to n-1, so accessing nth element is undefined behavior.

You can access the nth element of an array by it's it's n-1 line arr[n-1];//this is the nth element
Segmentation fault occur here in your code
(wordArray[c] < wordArray[c+1]) // try to modify this.
Lets assume the size = 6
Whwn for() loop will run for the first time, c = 2*6 means 6, and you are accessing the arr[6] and arr[6+1] both are not valid. Array index starts from zero instead of one.

Related

C++ SIGSEGV error

The code is giving SIGSEGV error. How can I remove it? The code is multiplying elements of an array and modulo 109+7 at each step of multiplication.
int main()
{
int n;
int A[10];
cin >> n;
for(int i = 0; i < n; i++)
cin >> A[i];
int ans = 1;
int m = 1000000007;
for(int i = 0; i < n; i++)
{
ans = (ans * A[i]) % m;
}
cout << ans;
}
In your code the array A is declared as of 10 elements. However, afterwards the amount of elements to handle is asked to user. Also, potentially the first number enterred by the user seems to be either negative or more than 10. In this case you need to test the enterred value.
Besides that, you can use std::vector instead of the array A to be of a dynamic size. Then after the user has given the amount of elements (with the first std::cin) you can set the size of the vector. The rest code then will remain nearly the same with some cosmetical changes.
Arrays in C++ do not magically resize themselves. As a result, if the user enters a value for n of 10 or more, both loops attempt to access elements of the array A past its end. For example, if n is 13, the loops will access 13 elements of a 10 element array named A.
That is undefined behaviour. If your host system is some unix variant (e.g. linux) and the operating system detects your program accessing memory it shouldn't, it will send a SIGSEGV signal to your program, which forceably causes the program to exit. However, that is only one possible symptom of many.
Given that this is a homework exercise, I'll just say you need to work out a way to dynamically allocate an array with n elements - AFTER reading n. Using a standard container (like std::vector<int>) is one way - but, depending on what your homework requires, may not be permitted. In that case, look up operators new and delete. (No, I will not be more specific - this is your homework, not mine)

Assigning a structure to another structure results in garbage

The two structures used in my code, one is nested
struct Class
{
std::string name;
int units;
char grade;
};
struct Student
{
std::string name;
int id;
int num;
double gpa;
Class classes[20];
};
I am trying to figure out a way to sort the structures within the all_students[100] array in order of their ID's in ascending order. My thought was, to start counting at position 1 and then compare that to the previous element. If it was smaller than the previous element then I would have a temporary array of type Student to equate it to, then it would be a simple matter of switching them places within the all_students array. However, when I print the results, one of the elements ends up being garbage numbers, and not in order. This is for an intermediate C++ class in University and we are not allowed to use pointers or vectors since he has not taught us this yet. Anything not clear feel free to ask me.
The function to sort the structures based on ID
void sort_id(Student all_students[100], const int SIZE)
{
Student temporary[1];
int counter = 1;
while (counter < SIZE + 1)
{
if (all_students[counter].id < all_students[counter - 1].id)
{
temporary[0] = all_students[counter];
all_students[counter] = all_students[counter - 1];
all_students[counter - 1] = temporary[0];
counter = 1;
}
counter++;
}
display(all_students, SIZE);
}
There are a few things wrong with your code:
You don't need to create an array of size 1 to use as a temporary variable.
Your counter will range from 1 to 100, you will go out of bounds: the indices of an array of size 100 range from 0 to 99.
The following solution uses insertion sort to sort the array of students, it provides a faster alternative to your sorting algorithm. Note that insertion sort is only good for sufficiently small or nearly sorted arrays.
void sort_id(Student* all_students, int size)
{
Student temporary;
int i = 1;
while(i < size) // Read my note below.
{
temporary = all_students[i];
int j = i - 1;
while(j >= 0 && temporary.id < all_students[j].id)
{
all_students[j+1] = all_students[j]
j--;
}
all_students[j+1] = temporary;
i++;
}
display(all_students, size);
}
Note: the outer while-loop can also be done with a for-loop like this:
for(int i = 1; i < size; i++)
{
// rest of the code ...
}
Usually, a for-loop is used when you know beforehand how many iterations will be done. In this case, we know the outer loop will iterate from 0 to size - 1. The inner loop is a while-loop because we don't know when it will stop.
Your array of Students ranges from 0, 99. Counter is allowed to go from 1 to 100.
I'm assuming SIZE is 100 (in which case, you probably should have the array count also be SIZE instead of hard-coding in 100, if that wasn't just an artifact of typing the example for us).
You can do the while loop either way, either
while(counter < SIZE)
and start counter on 0, or
while (counter < SIZE+1)
and start counter on 1, but if you do the latter, you need to subtract 1 from your array subscripts. I believe that's why the norm (based on my observations) is to start at 0.
EDIT: I wasn't the downvoter! Also, just another quick comment, there's really no reason to have your temporary be an array. Just have
Student temporary;
I overlooked the fact that I was allowing the loop to access one more element than the array actually held. That's why I was getting garbage because the loop was accessing data that didn't exist.
I fixed this by changing while (counter < SIZE + 1)
to: while (counter < SIZE )
Then to fix the second problem which was about sorting, I needed to make sure that the loop started again from the beginning after a switch, in case it needed to switch again with a lower element. So I wrote continue; after counter = 1

adding dynamic arrays and removing trailing 0's

I am trying to implement this function to add two dynamic a
rrays, however when I implement this into my main it completely crashes, I have no idea why...
I cannot understand why the program shuts down except the exit code on scite says exit code 255. But that is not helpful. Any idea what the problem may be?
For one:
for (int k=0; k<=max; k++)
This goes out of range. Instead allocate memory for [max+1] elements, since there shall be max+1 terms in the polynomial.
sum = new int[ max + 1 ];
Also, the j loop should start from max.
for (j=max; j>0 && sum[j]==0; --j);
You have a typo on this line:
for (j=max-1; j>0 && sum[j]==0; --j);
^here
The next statement int *tmp=sum; does not get executed.
Also the for loop should probably be
for (j=max-1; j>=0 && sum[j]==0; --j)
^ //don't forget the last member
A couple of nice things about C++ is all the standard containers (like std::vector) and standard algorithms available. For example you could use vectors and backwards iterators and std::find_if_not to find the last non-zero value.
Like
// Create a vector of a specific size, and initialize it
std::vector<int> sum(std::max(a->degree, b->degree), 0);
// Fill it up...
// Find the last non-zero value
auto last_non_zero = std::find_if_not(sum.rbegin(), sum.rend(),
[](const int& value){ return value == 0; });
if (last_non_zero == sum.rbegin())
{
// No zeroes found
}
else if (last_non_zero == sum.rend())
{
// All of it was zero
sum.clear();
}
else
{
std::vector<int> temp(last_non_zero, sum.rend())
std::reverse(temp); // Because the `temp` vector is reversed
sum = temp;
}
After this the vector sum should have been stripped of trailing zeroes.

C++ Dynamic bool array causes crash

Today I tried to program the Sieve of Eratosthenes and it works as far as it provides me with the prime numbers. But I have a problem with the dynamic array I don't understand.
First problem: As soon as I try to enter a "big" value for n (for example 120), the program crashes, it doesn't even allocate the memory.
Second problem: If I enter a value like 50 it is able to give out the correct prime numbers but crashes before it deletes the array.
Third problem: If I enter a very small value like 5 it is able to execute the entire program, it gives out the correct numbers and deletes the memory.
But I don't understand why it acts so differently. 120 boolean values can't crash my memory, at least I think so. And why isn't it able to delete an array of 50 values but is actually able to delete an array of 5 values?
Can anyone tell me what's the problem?
int n;
cin >> n;
n=n+1;
bool *feld = new bool[n];
for(int i=2;i<n;i++)
{
int j=i*i;
feld[j]=true;
for(;j<n;j+=i)
feld[j]=true;
}
for(int i=2;i<n;i++)
if(!feld[i])
cout << i << endl;
else;
delete[] feld;
feld = NULL;
Your problem is here:
int j=i*i;
feld[j]=true;
there is no check as to whether j < n so you are stomping over unallocated memory when j >= n.
This code is wrong
bool *feld = new bool[n];
for(int i=2;i<n;i++)
{
int j=i*i;
feld[j]=true;
...
}
Suppose n == 10 and i == 9, then j == 81 but you only have 10 elements in your bool array.
This is how it works when you write bugged programs, sometimes it seems to work, it might even give the right answer, other times it will crash. This is a very important lesson, and you're actually lucky to have learned it early.
Actually It's not just that feld[j]=true; is causing the error.
Also, you don't need that line at all before the loop.
because, it's the first case inside the loop.

It is safe to use i+1 in O(n) searching?

I'm asking myself if it is safe to use i+1 to check if the next number is the same as current... Example :
int search(int el, int* a, int n) {
if(a == NULL && n<0)
{
return NULL;
}
for(int i=0; i<n; i++)
{
if((a[i] == el )&& (a[i+1] != el)) // here I check next element
{
return i; // if I find it, return a position.
}
}
return -1; // else return -1
}
If we have an array with length 4, then a[i+1] will be passed an array length, but program will still work.
No, accessing elements out of bounds is Undefined Behavior.
Your program may "seem" to work, but you cannot expect anything out of it. It could work on your machine, and crash on another one. Or it could work on all machines now, but not tomorrow. You should check if the index is out-of-bounds before using the subscript operator.
You may consider changing your cycle so that it never accesses out-of-bounds elements:
for (int i = 0; i < n - 1; i++)
// ^^^^^
Then, you would have to take care of the last element in the array separately, as a special case.
That's not correct, when i reaches its last value (n-1) you check the value of a nonexistent element (the C standard allows you to have a pointer to one-after-last element, but not to dereference it).
You can fix your code like this:
if((a[i] == el ) && ((i == n-1) || (a[i+1] != el)))
If n is the last element in the array, then i + 1 is safe in this case. If n is the number of elements in the array, i + 1 may appear to work most of the time, but it is not safe.
In that case, you're accessing an element outside the bounds of the array, which may do anything from giving you incorrect results to crashing your program. Most of the time it will appear to work, though, making the problem very hard to debug when it happens.
It is not clear to me what you're asking. Even if array access is expensive, accessing both a[i] and a[i+1] remains of O(N) complexity. What you can't do is adding complexity that's based on i (for example an additional loop from 0 to i), or modifying i (for example decrementing it based on some condition).
The problem, as others have already pointed out, is that the last element is compared to a nonexistent datum: either you'll get an error and a possible crash, or the program will appear to be working - and actually work most of the time - until the time where that unknown and possibly random last-and-one item will trigger the comparison, and yield an unexpected result.
You should check that the array size is at least 1 (that ought to be a special case anyway, can't run a compare on a single element!) and then loop only up to n-1. Or you could save the previous value in a temporary variable; depending on the platform, this will be a (possibly much faster) register, or a (possibly much slower) stack location. In most cases I'd just state my intention of comparing with the next element, as you did, and let the compiler sort it out.
No.
int main(void)
{
char pumpkin[8];
int a[4];
int i, p;
a[0] = 3760;
a[1] = 100001;
a[2] = 595959;
a[3] = 1886221680;
pumpkin[0] = 'p';
pumpkin[1] = 'u';
pumpkin[2] = 'm';
pumpkin[3] = 'p';
for (i = 0; i < 4; i++) {
p = search(a[i], a, 4);
if (p >= 0)
printf("Found it at position %d: %d.\n", p, a[i]);
else
printf("Value not found.\n");
}
return 0;
}
In my machine:
$ gcc -std=c11 -o boundserror boundserror.c
$ ./boundserror
Found it at position 0: 3760.
Found it at position 1: 100001.
Found it at position 2: 595959.
Value not found.
What happened ? Compiler wrote value 1886221680 both in a[3] and where a[4] would be if it existed. About the program working on your machine: read about the works on my machine concept.
http://www.codinghorror.com/blog/2007/03/the-works-on-my-machine-certification-program.html