0

I keep getting this debug error that my data is corrupted. I don't know how that is possible nor do I know how to fix it.

Screenshot of error

This program populates a array then uses a selection sort type algorithm to order the numbers. Has you can see it starts to sort the numbers and then stops with this corrupted data error. How can I fix it?

Full Code:

#define _CRT_SECURE_NO_WARNINGS
#define ARY_SIZE 10
#include <stdio.h>
#include <stdlib.h>

void selectionSort(int[], int last);
void ranNumPerm_10(int bubble_1[]);

int main(void)
{
    int list[ARY_SIZE] = { 0 };


    int last;
    last = 10;

    ranNumPerm_10(list);
    for (int i = 0; i < ARY_SIZE; i++)
    {
        printf("%d\n", list[i]);
    }
    printf("\nUnsorted on top \n");

    selectionSort(list, last);

    for (int i = 0; i < ARY_SIZE; i++)
    {
        printf("%d\n", list[i]);
    }

    return 0;
}

void selectionSort(int list[], int last)
{
    int smallest;
    int tempData;

    for (int current = 0; current < last; current++)
    {
        smallest = current;
        for (int walk = current + 1; walk <= last; walk++)
        if (list[walk] < list[smallest])
        {
            smallest = walk;

            tempData = list[current];
            list[current] = list[smallest];
            list[smallest] = tempData;

        }

    }
    return;
}

void ranNumPerm_10(int list[])
{
    int oneRandno;
    int haveRand[ARY_SIZE] = { 0 };

    for (int i = 0; i < ARY_SIZE; i++)
    {
        do
        {
            oneRandno = rand() % ARY_SIZE;
        } while (haveRand[oneRandno] == 1);
        haveRand[oneRandno] = 1;
        list[i] = oneRandno;
    }
    return;
}
Zong
  • 6,160
  • 5
  • 32
  • 46
T.Malo
  • 512
  • 1
  • 7
  • 24

1 Answers1

2

Looks like

for (int walk = current + 1; walk <= last; walk++)

Should be

for (int walk = current + 1; walk < last; walk++)

(Walk can take the value last, which is out of range in the array)

Leo
  • 283
  • 2
  • 4
  • Wow thank you! Please spread the word tell anyone and everyone to never buy the book "Computer Science A Structured Approach Using C BY Behrouz A.Forouzan & Richard F. Gilberg" It is a horrible book syntax errors all the time in it. It makes you spend hours and hours trying to figure out why something wont work. I need to find a better book. – T.Malo Apr 05 '14 at 22:10
  • @T.Malo: If you need a better book you could start with [The Definitive C Book Guide and List](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) – Blastfurnace Apr 05 '14 at 22:25