Thanks to everyone for checking out this post. I posted a while ago and Jim helped me out, and now I'm back with a stickier problem!
This program I'm working on accepts an array size from the user, prompts the user to store that many integers, sorts them from smallest to largest, and then searches for duplicates with a simple for loop. The ultimate goal of the assignment was to display duplicates in an array, and the rest of the functions are just how I decided to reach that goal.
Anyway, my program crashes if I choose an array size larger than 7. It sorts and displays duplicates perfectly with 7 or fewer arguments.
The exact moment it crashes is after I enter the final value it prompts me for, so it appears my inputsize() function and my inputarray() function are working, and the error may be in the arrsort() function. Any help is appreciated! Code is below:
Code:
#include <stdio.h>
int funcinputsize(int);
void funcinputarray(int [], int size);
void funcarrsort(int [], int size);
void funcdupe(int [], int size);
int main()
{
int SIZE = 0;
int array[SIZE];
SIZE = funcinputsize(SIZE);
funcinputarray(array, SIZE);
funcarrsort(array, SIZE);
funcdupe(array, SIZE);
return 0;
}
int funcinputsize(int SIZE)
{
printf("Input the size of your array in whole numbers\n");
scanf("%d", &SIZE);
printf("Your array will be %d integers in length\n\n", SIZE);
return SIZE;
}
void funcinputarray(int array[], int SIZE)
{
int i;
for (i = 0; i < SIZE; i++)
{
printf("Enter an integer for array value %d\n", i + 1);
scanf("%d", &array[i]);
printf("The value at array index %d is %d\n\n", i, array[i]);
}
return;
}
void funcarrsort(int array[], int SIZE)
{
int i, j, temp;
for(i = 1; i <= SIZE - 1; i++)
{
j = i;
while (j > 0 && array[j] < array[j - 1])
{
temp = array[j];
array[j] = array[j - 1];
array[j - 1] = temp;
j--;
}
}
printf("Your array, ordered in ascending integer order:\n\n");
for (i = 0; i <= SIZE - 1; i++)
{
printf("%d ", array[i]);
}
printf("\n\n");
return;
}
void funcdupe(int array[], int SIZE)
{
int i;
for (i = 0; i < SIZE; i++)
{
if (array[i] == array[i + 1])
{
printf("%d, located at index %d, is a duplicate of the value at index %d\n\n", array[i], i, i + 1);
}
}
return;
}