Abort trap :6 Error in C++ [closed] - c++

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 6 years ago.
Improve this question
I am new to programming so forgive me if question sounds trivial.Your any suggestion will be a great help in my learning.
I am writing up a Selection_Sorting program on the array of size 10. I am able to compile the program but after giving the input its reflecting this
message:
The sorted array as follows:
Abort trap: 6
My question is where to look in the program to troubleshoot the issue and what are the reasons this issue occurs ?
For reference I am attaching my code.
# include <iostream>
void fill_array(int sample_array[10], int size);
void sort(int sample_array[10], int size);
void swap(int &a1, int &a2);
int index_of_smallest(int array[10], int start_index, int size);
int main()
{
using namespace std;
int array[10];
fill_array(array, 10);
sort(array, 10);
cout << " The sorted array as follows : \n";
for (int i = 0; i < 10; i++)
{
cout << array[i] << " ";
}
return 0;
}
void fill_array(int sample_array[10], int size)
{
using namespace std;
for (int index = 0; index< 10; index++)
{
cin >> sample_array[index];
}
}
void swap(int &a1, int &a2)
{
int temp;
temp = a1;
a1 = a2;
a2 = temp;
}
int index_of_smallest(int array[10], int start_index, int size)
{
int min = array[start_index];
int min_index = start_index;
for (int i = start_index + 1; i< size - 1; i++)
{
if (array[i]< min)
{
min = array[i];
min_index = i;
}
}
return min_index;
}
void sort(int sample_array[10], int size)
{
int next_min_index;
int j;
for (j = 0; j < size; j++)
{
next_min_index = index_of_smallest(sample_array, j, 10);
}
swap(sample_array[j], sample_array[next_min_index]);
}

I decided to decipher the one function that would be most likely at fault. I've re-formatted it here to be readable:
void sort( int sample_array[10], int size )
{
int next_min_index;
int j;
for( j = 0; j < size; j++ )
{
next_min_index = index_of_smallest( sample_array, j, 10 );
}
swap( sample_array[j], sample_array[next_min_index] );
}
And now, hopefully you can see the problem.
The swap is not happening inside the loop. You moved the definition for j out of the loop scope (presumably to fix compile errors you didn't understand, which would have pointed you to the problem).
The swap is happening when j == 10. That is outside your array bounds and your program baulks. You should fix the error if you change the function to this:
void sort( int sample_array[10], int size )
{
for( int j = 0; j < size; j++ )
{
int next_min_index = index_of_smallest( sample_array, j, 10 );
swap( sample_array[j], sample_array[next_min_index] );
}
}
This might not be the only problem, but I'm not going to decipher the rest of your code. Hopefully this fix plus some strong encouragement to use human-readable code layout will help you along your way.

Related

Extract pair numbers from array

Good evening, folks.
I'm currently experiencing difficulties with extracting pair numbers from an array. I have the following code:
#include <iostream>
using namespace std;
int *paire(int *d, int length) {
int counter = 0;
int position = 0;
for (int i=0; i<length; i++) {
if (d[i] % 2 ==0)
counter++;
}
int *k = new int[counter];
for (int i=0; i<length; i++) {
if (d[i] % 2 ==0) {
k[position] = d[i];
position++;
}
}
return k;
}
int main() {
int b[8] = {1,2,3,4,5,6,7,8};
int *array1 = paire(b,8);
for (int i=0; i<5; i++) { // how can I point here to the counter in paire() ?
cout<<array1[i];
}
delete[] array1;
return 0;
}
So I think I've got it right with initializing the new array in function paire, but I'm having difficulties to iterate through the array.
P.S. I'm first year in university, so I would really be thankful if you can keep the same simplicity in the answers. Thanks in advance!
It appears that you need to return 2 separate values: the number of even numbers in the array b, and the address of the newly allocated memory that is storing exclusively those even numbers.
Since you can not return multiple variables, one solution that does minimal modification to your code would be as follows.
int *paire(int *d, int length, int& counter) {
counter = 0;
// rest of your function remains unchanged
// ...
}
int main() {
int b[8] = {1,2,3,4,5,6,7,8};
int evenNumbers;
int *array1 = paire(b,8, evenNumbers);
for (int i=0; i<evenNumbers; i++) {
cout<<array1[i];
}
delete [] array1;
return 0;
}
Alternatively, you can return the value in counter and send the reference to the int* variable as an argument to paire function. Or, you can declare paire to have return type void and use references to pass back both the values.
You can further simplify your function by allocating to that of the length and returning the counter by an output parameter.
#include <iostream>
using namespace std;
int *paire(int *d, int length, int &counter) {
counter = 0;
int *k = new int[length]; // allocate for the maximum memory
for (int i = 0; i < length; ++i) {
if (d[i] % 2 == 0) {
k[counter++] = d[i];
}
}
return k;
}
int main() {
int b[8] = {1,2,3,4,5,6,7,8};
int counter = 0;
int *array1 = paire(b,8, counter);
for (int i=0; i<counter; i++) { // how can I point here to the counter in paire() ?
cout<<array1[i] << " ";
}
delete [] array1;
return 0;
}
But please note that as others have already pointed out this method is quite error prone in the sense that it leaves the responsibility to the client to delete the internal memory used by paire function.

Vector of vectors initialization work won't but a matrix array does, why so? [duplicate]

This question already has answers here:
Accessing an array out of bounds gives no error, why?
(18 answers)
Closed 6 years ago.
So I'm practicing solving DP problems and found out this issue I couldn't comprehend why. If I initialize my vector of vectors with size n, operations won't work. Meanwhile if I just declare cost as int cost[n][n] it does. Please tell me why.
int sum (vector<int> freq, int i, int j){
int s=0;
for (int x=i; x<=j; x++) {
s+=freq[x];
}
return s;
}
int optimalSearchTree(vector<int> keys, vector<int> freq){
int n = keys.size();
vector<vector<int>> cost( n,vector<int>( n,0 )) ;
for (int i = 0; i < n; i++) {
cost[i][i] = keys[i];
}
for (int L=2; L<=n; L++) {
for (int i = 0; i<= n-L+1; i++) {
int j = i+L-1;
cost[i][j] = INT_MAX;
for (int r=i; r<=j; r++) {
int c = ((r > i)? cost[i][r-1]:0) +
((r < j)? cost[r+1][j]:0) +
sum(freq, i, j);
if (c < cost[i][j]) {
cost[i][j] = c;
}
}
}
}
return cost[0][n-1];
}
int main(){
vector<int> keys = {10,12,16,21};
vector<int> freq = {4,2,6,3};
cout<<optimalSearchTree(keys, freq)<<endl;
// int n = keys.size();
//vector<vector<int>> cost( n,vector<int>( n,0 )) ;
//cout<<cost.size()<<" "<<cost[0].size()<<endl;
}
I believe the solution follows the same as this question:
Accessing an array out of bounds gives no error, why?
In it, specifies that out of bound access from a int arr[n][n] won't return any error. Meanwhile accessing out of bound positions from a vector will.

wrong output with large test case in union find

I wrote a simple union find implementation using quick find method. Here is my code
#include <iostream>
using namespace std;
class QuickFind
{
int* id;
int size;
public:
QuickFind(int n)
{
id = new int[n];
size = n;
for(int i = 0; i < size; i++) id[i] = i;
}
bool is_connected(int p, int q)
{
return id[p] == id[q];
}
void do_union(int p, int q)
{
int tempP = p;
int tempQ = q;
for(int i = 0; i < size; i++)
{
if(id[i] == tempP) id[i] = tempQ;
}
}
};
int main()
{
QuickFind obj(10000);
obj.do_union(5,6);
obj.do_union(6,8);
cout<<obj.is_connected(5,6)<<endl;
for(int i = 0; i < 1000; i++)cout<<obj.is_connected(i,i+1)<<endl;
return 0;
}
I also wrote a for loop in main. This prints correct answers when I loop it say 50 or 100 times. But it is giving me all 0s when i loop it like 1000 or more times. I'm using codeblocks ide.
Also when i compiled the same code in codechef's online compiler i get correct output. Can anyone tell me about this anomaly?

Why this Selection sort code in Cpp, not giving required Output

#include<iostream>
using namespace std;
int min_arr(int arr[],int size);
void swap(int *,int *);
int main()
{
int arr[10]={31,2,55,3,77,12,89,98,43,34},loc;
int* arr1;
arr1 = &arr[0];
for(int i=0;i<10;i++)
{
for( int j=i;j<9;j++)
{
loc = min_arr(arr1,(10-i));
swap(&arr[loc],&arr[i]);
arr1++;
}
}
for(int i =0; i<10;i++)
cout<<arr[i]<<endl;
return 0;
}
int min_arr(int arr[],int size)
{
int k=0;
int temp=arr[0];
for(int i=1;i<size;i++)
{
if(arr[i]<temp)
{
temp=arr[i];
k=i;
}
}
return k;
}
void swap(int *a, int *b)
{
int temp;
temp=*a;
*a=*b;
*b=temp;
}
Why this Selection sort code in Cpp, not giving required Output? Kindly find the flaw! I have taken two functions to find min of the sub arrays as we procede. And as i find the min, i return its index and swap the first position of the sub array and minimum valued element!
After rearranging the code and adding some debug lines, it's pretty easy to find out what's wrong:
Firstly, the second loop (j loop) is completely pointless
Secondly loc variable is not 0-based but i-based (as you searched over arr1, which is incremented by the loop), so arr[loc] should be arr[loc+i]
Corrected, smartly indented (that's important to make tour code easily readable) code:
#include<iostream>
#define ARRAY_SIZE 10
using namespace std;
int min_arr(int arr[],int size);
void swap(int *,int *);
int main()
{
int arr[ARRAY_SIZE]={31,2,55,3,77,12,89,98,43,34},loc;
int* arr1;
arr1 = &arr[0];
for( int i = 0; i < ARRAY_SIZE; i++ )
{
//for( int j = i; j<ARRAY_SIZE-1; j++ )
{
loc = min_arr(arr1,(ARRAY_SIZE-i));
// for debug:
//std::cout << "min found at " << loc << std::endl;
swap(&arr[loc+i],&arr[i]);
// for debug:
//for( int i =0; i<ARRAY_SIZE; i++ )
// cout << arr[i] << " ";
//cout << std::endl;
arr1++;
}
}
for( int i =0; i<ARRAY_SIZE; i++ )
cout<<arr[i]<<endl;
return 0;
}
int min_arr( int arr[], int size )
{
int k=0;
int temp=arr[0];
for( int i=1; i<size; i++ )
{
if( arr[i] < temp )
{
temp=arr[i];
k=i;
}
}
return k;
}
void swap(int *a, int *b)
{
int temp;
temp=*a;
*a=*b;
*b=temp;
}
It will output:
2
3
12
31
34
43
55
77
89
98
In your version you just go with pointers beyond the area mapped for the array.
First of all, call of the function min_arr. It requires array, not pointer:
loc=min_arr(arr,(10-i));
Second, the function itself. You always start from the beginning of the array, that's why already sorted elements go resorted.
int min_arr(int arr[],int size)
{
int k=10-size;
int temp=arr[k];
for(int i=k+1;i<10;i++)
{
if(arr[i]<temp)
{
temp=arr[i];
k=i;
}
}
return k;
}
for(int i=0;i<10;i++)
{
for( int j=i;j<9;j++) //shouldn't this be j < 10 ?
......

Trying to delete duplicates in char array/cstring? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I don't understand why my code isn't working. The commented out code would remove the wrong characters and not remove any spaces, but the current delete_repeats function is giving me an error of: `line 49: expected initializer before numeric constant.
Can anyone help me?`
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
void fill_array(char a[], int size, int& number_used);
void delete_repeats(char a[], int& number_used);
void sentences_output(char a[], int& number_used);
int main()
{
char sentences[100];
int used=0;
fill_array(sentences, 100, used);
delete_repeats(sentences, used);
sentences_output(sentences, used);
return 0;
}
void fill_array(char a[], int size, int& number_used)
{
char c;
int index = 0;
cout<<"Enter your sentence or sentences and press enter. \n"
<< "The max number of characters is 100.\n";
cin.get(c);
while((c != '\n')&&(index < size))
{
a[index]=c;
cin.get(c);
index++;
}
number_used = index;
cout<<"Your original array size was "<<number_used<<endl;
return;
}
void delete_repeats(char a[], int& number_used)
48{
int counter = number_used;
for (int i = 0; i < number_used; i++)
{
for (int j = i+1; j < number_used; j++)
{
if (a[i] == a[j])
{
for (int k = j; k < number_used - 1; k++)
{
a[k]= a[k + 1];
}
// } wrong place; j-- and size-- only if a duplicate
j--;
number_used--;
} // moved brace here
cout << "size = " << number_used;
cout << endl;
}
}
return;
}
/*
void delete_repeats(char a[], int& number_used)
{
int location = 0;
int target = 0;
int change;
for(location = 0; location < number_used; location++)
{
for(target = 0; target<number_used; target++)
{
change = target;
if(a[location] == a[target])
{
a[target] = a[change+(target - location)];
}
}
}
return;
}
*/
void sentences_output(char a[], int& number_used)
{
cout<<"The new sentence is \n";
for(int i = 0; i < number_used; i++)
{
cout<<a[i]<<endl;
}
cout<<"The size of the new array is \n";
cout<<number_used<<endl;
return;
}
The commented out delete_repeats was the second one I came up with and the first delete_repeats was from an example I found, but neither is working.
Thanks everyone! Here is my corrected code:
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
void fill_array(char a[], int size, int& number_used);
void delete_repeats(char a[], int& number_used);
void sentences_output(char a[], int& number_used);
int main()
{
char sentences[100];
int used=0;
fill_array(sentences, 100, used);
delete_repeats(sentences, used);
sentences_output(sentences, used);
return 0;
}
void fill_array(char a[], int size, int& number_used)
{
char c;
int index = 0;
cout<<"Enter your sentence or sentences and press enter. \n"
<< "The max number of characters is 100.\n";
cin.get(c);
while((c != '\n')&&(index < size))
{
a[index]=c;
cin.get(c);
index++;
}
number_used = index;
cout<<"Your original array size was "<<number_used<<endl;
return;
}
void delete_repeats(char a[], int& number_used)
{
int counter = number_used;
for (int i = 0; i < number_used; i++)
{
for (int j = i+1; j < number_used; j++)
{
if (a[i] == a[j])
{
for (int k = j; k < number_used - 1; k++)
{
a[k]= a[k + 1];
}
j--;
number_used--;
}
}
}
return;
}
void sentences_output(char a[], int& number_used)
{
cout<<"The new sentence is \n";
for(int i = 0; i < number_used; i++)
{
cout<<a[i]<<" ";
}
cout<<"\n";
cout<<"The size of the new array is \n";
cout<<number_used<<endl;
return;
}
I removed the commented out delete_repeats function and edited the new one as well as changed the output function.
Did you include the line numbers in your actual code!? That would give the error you quote.