i know what causes the segfault, but why? - c++

Ok, so I have a simple c++ program that's supposed to run a couple sorting algorithms on an array composed of ints and track the time each one takes.. pretty basic, however I've run into a problem.
When the program first starts, it asks how many items you would like in the array. My assignment involves setting the array at specific lengths from 100 items all the way to 750000. It'll handle many values, including up to around 600000. When I try 750000 however it immediately segfaults. A couple couts here and there led me to discover that the error happens when the fourth array (all of the same length) is initialized. The weird thing is that it only happens on my OS; at my school it works no problem. (i'm on the latest ubuntu while my school uses redhat. not sure if that's useful)
I'll include the complete code just for reference but the segfault occurs at line 27:
int array1[num], array2[num], array3[num], array4[num]; // initialize arrays
I know this because I initialized each array on separate lines and put couts in between. array1, 2, and 3 were initialized, then it segfaults. Again this ONLY happens when the arrays are longer than about 600000 or so. Anything less works fine.
Full code:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
void insertionSort(int array[], int size);
void bubbleSort(int array[], int size);
void mergeSort(int array[], int first, int last, int size);
void quickSort(int array[], int size);
int main()
{
cout << endl << endl << "\t\t**** Extra Credit Assignment- Sorting ****" << endl << endl << endl;
cout << "Enter the number of items to sort: ";
int num;
cin >> num;
while(cin.fail()) // while cin does not recieve an integer
{
cin.clear();
cin.ignore(1000, '\n');
cout << "Invalid entry. Try again: "; // try again
cin >> num;
}
int array1[num], array2[num], array3[num], array4[num]; // initialize arrays
int randNum, sizeOfArray = sizeof(array1)/sizeof(array1[0]); // random number, size of the arrays
srand(time(NULL)); // random seed (used with rand())
for(int i = 0; i < sizeOfArray; i++) // traverse through the array
{
randNum = rand() % 2147483647+1; // establish random number (from 1 to 2147483647)
array1[i] = array2[i] = array3[i] = array3[i] = randNum; // set randNum to all arrays at current index
}
time_t beginTime, endTime;
double elapsedTime;
cout << endl << "Elapsed time:" << endl << "\tInsertion Sort-\t";
time(&beginTime);
insertionSort(array1, sizeOfArray);
time(&endTime);
elapsedTime = difftime(endTime, beginTime);
cout << elapsedTime << " seconds" << endl << "\tBubble Sort-\t";
time(&beginTime);
bubbleSort(array2, sizeOfArray);
time(&endTime);
elapsedTime = difftime(endTime, beginTime);
cout << elapsedTime << " seconds" << endl << "\tMerge Sort-\t";
time(&beginTime);
mergeSort(array3, 0, sizeOfArray-1, sizeOfArray);
time(&endTime);
elapsedTime = difftime(endTime, beginTime);
cout << elapsedTime << " seconds"<< endl;
/* ********************* TESTING *************************
// *******************************************************
cout << "Insertion->Unsorted:\t";
for(int i = 0; i < sizeOfArray; i++)
{
cout << array1[i] << " ";
}
cout << endl;
insertionSort(array1, sizeOfArray);
cout << "Insertion->Sorted:\t";
for(int i = 0; i < sizeOfArray; i++)
{
cout << array1[i] << " ";
}
cout << endl;
cout << "Bubble->Unsorted:\t";
for(int i = 0; i < sizeOfArray; i++)
{
cout << array2[i] << " ";
}
cout << endl;
bubbleSort(array2, sizeOfArray);
cout << "Bubble->Sorted:\t\t";
for(int i = 0; i < sizeOfArray; i++)
{
cout << array2[i] << " ";
}
cout << endl;
cout << "Merge->Unsorted:\t";
for(int i = 0; i < sizeOfArray; i++)
{
cout << array3[i] << " ";
}
cout << endl;
mergeSort(array3, 0, sizeOfArray-1, sizeOfArray);
cout << "Merge->Sorted:\t\t";
for(int i = 0; i < sizeOfArray; i++)
{
cout << array3[i] << " ";
}
cout << endl; */
return 0;
}
void insertionSort(int array[], int size)
{
for(int i = 1; i < size; i++)
{
int item = array[i], index = i;
while(index > 0 && array[index-1] > item)
{
array[index] = array[index-1];
index--;
}
array[index] = item;
}
}
void bubbleSort(int array[], int size)
{
bool sorted = false;
for(int i = 1; i < size && !sorted; i++)
{
sorted = true;
for(int i2 = 0; i2 < size-i; i2++)
{
int nextI = i2+1;
if(array[i2] > array[nextI])
{
swap(array[i2], array[nextI]);
sorted = false;
}
}
}
}
void merge(int array[], int first, int mid, int last, int size)
{
int tempArray[size];
int first1 = first, first2 = mid+1;
int last1 = mid, last2 = last;
int index = first1;
while(first1 <= last1 && first2 <= last2)
{
if(array[first1] < array[first2])
{
tempArray[index] = array[first1];
first1++;
}
else
{
tempArray[index] = array[first2];
first2++;
}
index++;
}
while(first1 <= last1)
{
tempArray[index] = array[first1];
first1++;
index++;
}
while(first2 <= last2)
{
tempArray[index] = array[first2];
first2++;
index++;
}
for(index = first; index <= last; index++)
{
array[index] = tempArray[index];
}
}
void mergeSort(int array[], int first, int last, int size)
{
if(first < last)
{
int mid = (first+last)/2;
mergeSort(array, first, mid, size);
mergeSort(array, mid+1, last, size);
merge(array, first, mid, last, size);
}
}
Any help is greatly appreciated. It might be a memory limitation on my system? I really don't know lol just a thought.

You can't allocate such big arrays on the stack which has a fixed limited size, try:
int* array1 = new int[num];

As others have noted, the SEGV you're getting is due to overflowing the stack. The reason it happens on your ubuntu machine and not your school's redhat machine is likely due to differences in the default stack size.
You may be able to change your default stack size with ulimit -s which, with no additional arguments, will print your current stack size in kilobytes. For example, on my machine, that prints 8192 or 8 megabytes. I can raise it to 16MB with ulimit -s 16384
An array of 750000 ints will require about 3MB of stack space (4 bytes per int), so 4 such arrays (like you have) will require 12MB...

You've allocated very large arrays on the stack and you're overflowing the stack. If you new them or make them static, they'll be allocated on the heap and you won't fail.

It's because the stack is not an infinite resource. When you do int x[big_honkin_number], it tries to allocate enough space on the stack for that array.
You can usually compile/link your code to give you more stack but a better solution is to use dynamic memory allocation (i.e., new).

Related

How would you go about resolving this output value?

I finished this code homework assignment tonight. I thought I was done, but I just realized that my "Average" value is coming out wrong with certain values. For example: When my professor entered the values 22, 66, 45.1, and 88 he got an "Average" of 55.27. However, when I enter those values in my program, I get an "Average" of 55.25. I have no idea what I am doing wrong. I was pretty confident in my program until I noticed that flaw. My program is due at midnight, so I am clueless on how to fix it. Any tips will be greatly appreciated!
Code Prompt: "Write a program that dynamically allocates an array large enough to hold a user-defined number of test scores. Once all the scores are entered, the array should be passed to a function that sorts them in ascending order. Another function should be called that calculates the average score. The program should display the sorted list of scores and averages with appropriate headings. Use pointer notation rather than array notation whenever possible."
Professor Notes: The book only states, "Input Validation: Do not accept negative numbers for test scores." We also need to have input validation for the number of scores. If it is negative, including 0, the program halts, we should consider this situation for 'counter' not to be negative while we have a loop to enter numbers. So negative numbers should be rejected for the number of scores and the values of scores.
Here is my code:
#include <iostream>
#include <iomanip>
using namespace std;
void showArray(double* array, int size);
double averageArray(double* array, int size);
void orderArray(double* array, int size);
int main()
{
double* scores = nullptr;
int counter;
double numberOfScores;
cout << "\nHow many test scores will you enter? ";
cin >> numberOfScores;
if (numberOfScores < 0) {
cout << "The number cannot be negative.\n"
<< "Enter another number: ";
cin >> numberOfScores;
}
if (numberOfScores == 0) {
cout << "You must enter a number greater than zero.\n"
<< "Enter another number: ";
cin >> numberOfScores;
}
scores = new double[numberOfScores];
for (counter = 0; counter < numberOfScores; counter++) {
cout << "Enter test score " << (counter + 1) << ": ";
cin >> *(scores + counter);
if (*(scores + counter) < 0) {
cout << "Negative scores are not allowed. " << endl
<< "Enter another score for this test : ";
cin >> *(scores + counter);
}
}
orderArray(scores, counter);
cout << "\nThe test scores in ascending order, and their average, are: " << endl
<< endl;
cout << " Score" << endl;
cout << " -----" << endl
<< endl;
showArray(scores, counter);
cout << "\nAverage Score: "
<< " " << averageArray(scores, counter) << endl
<< endl;
cout << "Press any key to continue...";
delete[] scores;
scores = nullptr;
system("pause>0");
}
void orderArray(double* array, int size)
{
int counterx;
int minIndex;
int minValue;
for (counterx = 0; counterx < (size - 1); counterx++) {
minIndex = counterx;
minValue = *(array + counterx);
for (int index = counterx + 1; index < size; index++) {
if (*(array + index) < minValue) {
minValue = *(array + index);
minIndex = index;
}
}
*(array + minIndex) = *(array + counterx);
*(array + counterx) = minValue;
}
}
double averageArray(double* array, int size)
{
int x;
double total{};
for (x = 0; x < size; x++) {
total += *(array + x);
}
double average = total / size;
return average;
}
void showArray(double* array, int size)
{
for (int i = 0; i < size; i++) {
cout << " " << *(array + i) << endl;
}
}
I try to start my answers with a brief code review:
#include <iostream>
#include <iomanip>
using namespace std; // Bad practice; avoid
void showArray(double* array, int size);
double averageArray(double* array, int size);
void orderArray(double* array, int size);
int main()
{
double* scores = nullptr;
int counter;
double numberOfScores;
cout << "\nHow many test scores will you enter? ";
cin >> numberOfScores;
// This is not input validation, I can enter two consecutive bad values,
// and the second one will be accepted.
if (numberOfScores < 0) {
// Weird formatting, this blank line
cout << "The number cannot be negative.\n"
<< "Enter another number: ";
cin >> numberOfScores;
}
// The homework, as presented, doesn't say you have to treat 0 differently.
if (numberOfScores == 0) {
cout << "You must enter a number greater than zero.\n"
<< "Enter another number: ";
cin >> numberOfScores;
}
scores = new double[numberOfScores];
// Declare your loop counter in the loop
for (counter = 0; counter < numberOfScores; counter++) {
cout << "Enter test score " << (counter + 1) << ": ";
cin >> *(scores + counter);
if (*(scores + counter) < 0) {
cout << "Negative scores are not allowed. " << endl
<< "Enter another score for this test : ";
cin >> *(scores + counter);
}
}
orderArray(scores, counter); // Why not use numberOfScores?
cout << "\nThe test scores in ascending order, and their average, are: " << endl
<< endl;
cout << " Score" << endl;
cout << " -----" << endl
<< endl;
showArray(scores, counter); // Same as above.
cout << "\nAverage Score: "
<< " " << averageArray(scores, counter) << endl
<< endl;
cout << "Press any key to continue...";
delete[] scores;
scores = nullptr;
system("pause>0"); // Meh, I suppose if you're on VS
}
void orderArray(double* array, int size)
{
int counterx;
int minIndex;
int minValue; // Unnecessary, and also the culprit
// This looks like selection sort
for (counterx = 0; counterx < (size - 1); counterx++) {
minIndex = counterx;
minValue = *(array + counterx);
for (int index = counterx + 1; index < size; index++) {
if (*(array + index) < minValue) {
minValue = *(array + index);
minIndex = index;
}
}
*(array + minIndex) = *(array + counterx);
*(array + counterx) = minValue;
}
}
double averageArray(double* array, int size)
{
int x;
double total{};
for (x = 0; x < size; x++) {
total += *(array + x);
}
double average = total / size;
return average;
}
void showArray(double* array, int size)
{
for (int i = 0; i < size; i++) {
cout << " " << *(array + i) << endl;
}
}
When you are sorting your array, you keep track of the minValue as an int and not a double. That's why your average of the sample input is incorrect. 45.1 is truncated to 45 for your calculations. You don't need to keep track of the minValue at all. Knowing where the minimum is, and where it needs to go is sufficient.
But as I pointed out, there are some other serious problems with your code, namely, your [lack of] input validation. Currently, if I enter two consecutive bad numbers, the second one will be accepted no matter what. You need a loop that will not exit until a good value is entered. It appears that you are allowed to assume that it's always a number at least, and not frisbee or any other non-numeric value.
Below is an example of what your program could look like if your professor decides to teach you C++. It requires that you compile to the C++17 standard. I don't know what compiler you're using, but it appears to be Visual Studio Community. I'm not very familiar with that IDE, but I imagine it's easy enough to set in the project settings.
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <string>
#include <vector>
// Assumes a number is always entered
double positive_value_prompt(const std::string& prompt) {
double num;
std::cout << prompt;
do {
std::cin >> num;
if (num <= 0) {
std::cerr << "Value must be positive.\n";
}
} while (num <= 0);
return num;
}
int main() {
// Declare variables when you need them.
double numberOfScores =
positive_value_prompt("How many test scores will you enter? ");
std::vector<double> scores;
for (int counter = 0; counter < numberOfScores; counter++) {
scores.push_back(positive_value_prompt("Enter test score: "));
}
std::sort(scores.begin(), scores.end());
for (const auto& i : scores) {
std::cout << i << ' ';
}
std::cout << '\n';
std::cout << "\nAverage Score: "
<< std::reduce(
scores.begin(), scores.end(), 0.0,
[size = scores.size()](auto mean, const auto& val) mutable {
return mean += val / size;
})
<< '\n';
}
And here's an example of selection sort where you don't have to worry about the minimum value. It requires that you compile to C++20. You can see the code running here.
#include <iostream>
#include <random>
#include <vector>
void selection_sort(std::vector<int>& vec) {
for (int i = 0; i < std::ssize(vec); ++i) {
int minIdx = i;
for (int j = i + 1; j < std::ssize(vec); ++j) {
if (vec[j] < vec[minIdx]) {
minIdx = j;
}
}
int tmp = vec[i];
vec[i] = vec[minIdx];
vec[minIdx] = tmp;
}
}
void print(const std::vector<int>& v) {
for (const auto& i : v) {
std::cout << i << ' ';
}
std::cout << '\n';
}
int main() {
std::mt19937 prng(std::random_device{}());
std::uniform_int_distribution<int> dist(1, 1000);
std::vector<int> v;
for (int i = 0; i < 10; ++i) {
v.push_back(dist(prng));
}
print(v);
selection_sort(v);
print(v);
}
I opted not to give your code the 'light touch' treatment because than I would have done your homework for you, and that's just not something I do. However, the logic shown should still be able to guide you toward a working solution.

quick sort c++ code error segmentation fault 11?

Code:
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
void partition(int *A, int start, int end){ //end is the position of the last number
cout<<"start is "<<start<<" end is "<<end<<endl;
if(end - start+1 > 1){
int i = start + 1, j = start + 1, pivot = A[start];
while(j <= end){
if(A[j] <= pivot){
swap(A[j], A[i]);
i++;
}
j++;
}
swap(A[start], A[i-1]);
partition(A, start, i-1);
partition(A, i, end);
}
}
void QuickSort(int *A, int n){
partition(A, 0, n-1);
}
int main() {
int n, method;
string sortMethod[ ] = {
"Insertion sort", "Selection sort", "Bubble sort", "Merge sort", "Quick sort"
};
int m = sizeof(sortMethod)/sizeof(string);
srand(time(0));
while (true){
cout << "Number of test marks: ";
cin >> n;
if ( n==0 ) break;
int *A = new int[n];
for (int i=0; i < n; i++) cin>>A[i];
//A[i] = rand()%101;
for (int i=0; i < n; i++)cout << A[i] << " ";
cout<<endl<<endl;
cout << "Sorting method: \n";
for (int i=0; i < m; i++) cout << i+1 << ": " << sortMethod[i] << endl;
// cin >> method;
method = 5;
cout<<endl;
int t = time(0);
QuickSort(A,n);
if (method > m) continue;
cout << sortMethod[method-1] << endl;
if (n <= 100) {
for (int i=0; i < n; i++) {
if (i> 0 && i%10==0) cout << endl;
cout << A[i] << " ";
}
cout << endl;
}
else {
cout << "Sorting completed in " << time(0) - t << " sec.\n";
}
cout << endl;
}
cout << "Program ends here."<<endl;
return 0;
}
When some of my input numbers have the same value, I get a "Segmentation fault: 11".
For example, an input of "4 7 7 3 1" would produce infinite lines of the following output: "start is 1 end is 3".
May I ask how should I improve my code? I know that Segfault 11 happens when you try to access an invalid memory, and i think that's likely what I did here, although i cant seem to identify the error. Thanks!
So this is what's happening here. Imagine that in a certain partition the first element (the pivot) is also the largest.
This means that in every iteration of the while loop the value of A[j]<=pivot will be true and i will be increased.
Since both i and j are increased by 1 in every iteration there value is the same, so at the end of the loop they are both equal to end+1.
Then you are invoking partition(A, start, i-1) which is really the same as partition(A, start, end) - meaning you have an infinite recursion which continues until you reach... well, stack overflow and the program crush with segfault.
The minimal fix will be to make sure that the sub partitions are strictly smaller than the original one by excluding the pivot from them. So something like this:
partition(A, start, i-2); // i-1 is the pivot, so we don't need to include it
partition(A, i, end);

Passing an array as a Reference into a function not working

For my lab in school I was asked to generate random numbers and characters and pass it into a function template and then sort them after showing my work as unsorted first. I'm using visual studios as a requirement for my school, but my main issue is that it's compiling with no errors but when I run my program it's not passing my array to be sorted. I've been spending a lot of time trying to understand why it's not working any help would be very appreciated.
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <algorithm>
using namespace std;
template <typename T>
void arrayIn(T arr[], int size, char word) {
if (word == 'd') {
sort(arr, arr + size, greater<>());
}
else {
sort(arr, arr + size);
}
return;
}
template <typename O>
void arr_out(O arr[], int size) {
int j;
for (j = 0; j < size; j++) {
cout << arr[j] << endl;
return;
}
delete[] arr;
}
int main(void) {
srand(time_t(NULL));
int size,i,j;
char word;
int *arr1;
char *arr2;
cout << "Enter in the size of the array: ";
cin >> size;
cout << "How would you like to sort in ascending or descending order?: ";
cin >> word;
arr1 = new int[size];
arr2 = new char[size];
if (arr1 == 0) {
cout << "memory allocation error";
system("pause");
exit(1);
}
cout << "The first array will sort intagers." << endl;
cout << "not sorted" << endl;
for (i = 0; i < size; i++) {
arr1[i] = rand() % 100 + 1;
cout << arr1[i] << endl;
}
arrayIn(arr1, size, word);
cout << "sorted" << endl;
arr_out(arr1,size);
if (arr2 == 0) {
cout << "memory allocation error";
system("pause`enter code here`");
exit(1);
}
cout << "the secound array will sort characters." << endl;
cout << "not sorted" << endl;
for (j = 0; j < size; j++) {
arr2[j] = rand() % (126 + 1 - 33) + 33;
cout << arr2[j] << endl;
}
arrayIn(arr2, size, word);
cout << "sorted" << endl;
arr_out(arr2, size);
system("pause");
return 0;
}
You have a very simple mistake in your arr_out function, which makes it only print the first element. Correct as follows (remove the line I commented out):
template <typename O>
void arr_out(O arr[], int size) {
int j;
for (j = 0; j < size; j++) {
cout << arr[j] << endl;
// return; // This will return after printing the first element!
}
delete[] arr;
}

Why do I get a program crash for large values but not small values for my program?

Why do I get a program crash for large values but not small values for my program? If I input 1-3 the program does what it is supposed to but when I enter a number greater than that the program crashes and/or does not complete? Is it something to do with a pointer error or the way I've referenced something? I'm unsure so any help is appreciated. Thanks!
Code:
#include <iostream>
using namespace std;
void getData (int size, int *Arr){
cout << "\n\nEnter integer data one line at a time\n" << endl ;
for (int i=0; i < size; i++){
cin >> Arr[i];
}
}
void findMinAndMax(int array[], int size, int *min, int *max) {
int smallest = array[0];
int largest = array[0];
*min = smallest;
*max = largest;
for (int i = 1; i < size; i++)
{
if (array[i] > *max){
*max = array[i];
cout << "Max Value (loop): " << *max << endl;
}
if (array[i] < *min){
*min = array[i];
cout << "Min Value (loop): " << *max << endl;
}
}
// testing code
cout << "Min Value: " << *min << endl;
cout << "Max Value: " << *max << endl;
}
int *makeFrequency (int data[], int dSize, int *minDataValue, int *maxDataValue) {
cout << "Min Value Pre: " << *minDataValue << endl;// testing code
cout << "Max Value Pre: " << *maxDataValue << endl;// testing code
findMinAndMax(data, dSize, minDataValue, maxDataValue);
cout << "Min Value Post: " << *minDataValue << endl; // testing code
cout << "Max Value Post: " << *maxDataValue << endl;// testing code
int fSize = *minDataValue + *maxDataValue;
cout << "fSize: " << fSize << endl; // testing code
int *frequency;
frequency = new int [fSize];
// if frequency is 0, end
if (frequency == 0)
{
return 0;
}
// set all elements to 0 in array frequency
for (int i = 0; i <= fSize; i++) {
frequency[i] = 0;
}
for (int i = 0; i <= dSize; i++) {
int j = data[i] - (*minDataValue) + 1;
frequency[j] = frequency[j] + 1;
}
return frequency;
}
void makeHistogram (int *freq, int min, int max ){
cout << "Frequency Value HISTOGRAM: " << *freq << endl;
cout << "\n\n\n ----------- Histogram ----------------\n" << endl;
int size = min + max;
cout << "Size Value HISTOGRAM: " << size << endl;
for (int i = 0; i < size; i++){
if (freq[i] > 0) {
cout << "\n" << min + i - 1 << ": ";
for (int j = 0; j < freq[i]; j++) {
cout << '*';
}
}
}
cout << endl << endl;
}
int main() {
int dSize;
int *ArrayOfInts;
cout << "How many data values? ";
cin >> dSize;
ArrayOfInts = new int [dSize];
getData(dSize, ArrayOfInts);
int *frequency, min, max;
frequency = makeFrequency(ArrayOfInts, dSize, &min, &max);
if (frequency == 0) return -1;
cout << "Min Value MAIN: " << min << endl; // testing code
cout << "Max Value MAIN: " << max << endl; // testing code
cout << "Frequency Value MAIN: " << *frequency << endl;
makeHistogram(frequency, min, max);
delete [] frequency;
return 0;
}
One place where you have undefined behaviour which can cause crashes:
here you allocate fSize elements:
frequency = new int [fSize];
later you iterate it until fSize:
for (int i = 0; i <= fSize; i++) {
you should change to i < fSize, because there is no fSize element in your array. And the same problem with i <= dSize later on. Should be i < dSize.
btw. I dont see why only large values should cause crashes in your code, maybe this is just UB.
You're setting fSize incorrectly. It should be the difference between the maximum and minimum values, not the sum of them. Otherwise, if you have negative numbers in your list, the frequency array will be too small. And if absolute value of any of the negative numbers is larger than the highest number, fSize will be negative, which is not valid for the size of an array.
Then you need to add 1 to include both endpoints. So it should be:
int fSize = *maxDataValue - *minDataValue + 1;
Then, as the other answer pointed out, you need to fix your for loops. When the size of an array is N, the array indexes from from 0 to N-1. So it should be:
for (int i = 0; i < fSize; i++) {
using < as the loop test, not <=. If you try to write outside an array, you invoke undefined behavior, so anything can happen -- if you're lucky you get a crash, but that's not guaranteed.
You have a similar problem when you assign to frequency:
for (int i = 0; i <= dSize; i++) {
int j = data[i] - (*minDataValue) + 1;
frequency[j] = frequency[j] + 1;
}
There's no need to add 1 when subtracting *minDataValue, and doing so will cause you to go outside the array when data[i] is the maximum.

Insertion sort implementation in C++ results in multiple errors

This is a console application on CodeBlocks 13.12.
I am getting a variety of errors when I run this Insertion Sort.
Sometimes it prints outrageously large values that weren't in the original array. Or sometimes it runs and sorts the array perfectly fine.
Can anybody please point out what could possibly be wrong? Sorry I'm a noob.
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
void insertionSort(int arr[], int size);
int main()
{
int size;
srand(time(NULL));
cout << "Specify the size of your array: ";
cin >> size;
int theArray[size]; // creates an array of a size the user chooses
cout << endl << "Your current array: {";
for (int i = 0; i < size; i++) //prints out the original array
{
theArray[i] = rand() % 10000;
cout << theArray[i];
if (i != size - 1) // to beautify output
{
cout << ", ";
}
if (i % 10 == 0 && i != 0)
{
cout << endl;
}
}
cout << "}" << endl << endl;
insertionSort(theArray, size);
}
void insertionSort(int arr[], int size)
{
int begin = clock(); // are for timing the sort
for (int i = 0; i < size; i++) //does the sorting
{
int j = i + 1;
int temp = arr[j];
while (arr[i] > arr[j])
{
arr[j] = arr[i];
arr[i] = temp;
j--;
i--;
}
}
int end = clock(); // are for timing the sort
cout << endl << "Your sorted array is: {";
for (int i = 0; i < size; i++) // prints out sorted array
{
cout << arr[i];
if (i != size - 1)
{
cout << ", ";
}
if (i % 10 == 0 && i != 0)
{
cout << endl;
}
}
cout << "}" << endl << endl << "Your sort took: " << end - begin << " milliseconds" << endl << endl;
}
Additionally to #marom's answer, in your while loop, you don't put limitations neither on i or j, hence you try to access arr[-1], arr[-2] and so on. Also, you go back to the beginning of the sorted array, since you decrement i. Have a look at this code, compiled with g++ 4.8.1 gives no errors. Also, try to use std::swap defined in header <utility> since c++11 or in header <algorithm> until c++11.
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <utility>
using namespace std;
void insertionSort(int arr[], int size);
int main()
{
int size;
srand(time(NULL));
cout << "Specify the size of your array: ";
cin >> size;
int theArray[size]; // creates an array of a size the user chooses
cout << endl << "Your current array: {";
for (int i = 0; i < size; i++) //prints out the original array
{
theArray[i] = rand() % 10000;
cout << theArray[i];
if (i != size - 1) // to beautify output
{
cout << ", ";
}
if (i % 10 == 0 && i != 0)
{
cout << endl;
}
}
cout << "}" << endl << endl;
insertionSort(theArray, size);
}
void insertionSort(int arr[], int size)
{
int begin = clock(); // are for timing the sort
for (int i = 0; i < size - 1; i++) //does the sorting
{
int j = i + 1;
int temp = arr[j];
while (j > 0 && arr[j] < arr[j - 1])
{
// ^^ this ensures that we don't try to access arr[-1]
swap(arr[j], arr[j-1]); //prefer std functions if they do the job you want
j--;//we don't go back
}
}
int end = clock(); // are for timing the sort
cout << endl << "Your sorted array is: {";
for (int i = 0; i < size; i++) // prints out sorted array
{
cout << arr[i];
if (i != size - 1)
{
cout << ", ";
}
if (i % 10 == 0 && i != 0)
{
cout << endl;
}
}
cout << "}" << endl << endl << "Your sort took: " << end - begin << " milliseconds" << endl << endl;
}
At least this is wrong:
void insertionSort(int arr[], int size)
{
int begin = clock(); // are for timing the sort
for (int i = 0; i < size; i++) //does the sorting
{
int j = i + 1;
When i is size-1 then j equals size and you get over the bounds of the array (valid values are from 0 to size-1 included). You need to limit your for loop to i < size-1
First advice : don't do all your printing or clock measure in your sort function. Keep that for your main program. Your sort function must remain clear and concise with no side effect.
Now, i find it better to split the code into 2 simple functions :
First, if arr is assumed already sorted up the index n-1
you want to insert the adequate element of the table at pos offset so that
arr will be sorted up to index n:
void insert(int arr[], int n){
int i=n, temp=arr[n];
while ( (arr[i-1]>temp) && (i>0) )
{
arr[i]=arr[i-1];
i--;
}
arr[i]=temp;
}
Now we just have to call our insertion for all offsets in arr except first one:
void insertionSort(int arr[], int size)
{
for(int n=1; n<size; n++) insert(arr,n);
}
As already mentioned by marom in his answer, when i = size - 1 you set j = size and access memory out of bounds, similarly, consider the case where j is set to the smallest element in the array, in that case you reach the left most position of the array by swapping the elements and decrementing, and eventually, i will become negative (since you do not put a bound to check if i becomes less than 0) and so will j and you will be accessing memory out of your bounds again.
Moreover, you are decrementing the value of i as well, which does not make sense, since by decrementing the value of i you are making extra runs for the external for loop.
So, your function shall look something like this ::
for (int i = 0; i < size - 1; i++) //changed the limit of for loop
{
int j = i + 1;
int temp = arr[j];
while ((j > 0) && (arr[j - 1] > arr[j])) //instead of working with the values of i, now we are doing everything with j
{
arr[j] = arr[j - 1];
arr[j - 1] = temp;
j--;
}
}
Hope this helps!