first of all, I'm just starting to learn C programming on my own so please don't be mad if I'm explaining or doing something wrong, I tried to search similar question to my problem on StackOverflow but I couldn’t find one so please if I'm doing something wrong, take it easy on me I'm only trying to learn.
I have an issue I can't understand, I wrote a sorting list program using the Insertion sort algorithm, that runs differently on different compilers. when I enter only positive numbers everything works smoothly but if I add some negatives numbers, depending on the compiler, it sometimes works and sometimes does not work at all/ prints a weird result
#include <stdio.h>
#include <string.h>
int main()
{
// gathering sources for sorting an array:
int myArray[5];
int count = 1;
for (int arrayIndex = 0; arrayIndex < 5; arrayIndex )
{
printf("\nEnter the #%d array element: ", count);
scanf(" %d", &myArray[arrayIndex]);
count ;
}
// calculate the last array index
int myInt = myArray[0];
int arrayLength = ((sizeof(myArray)) / (sizeof(myInt))) - 1;
// printing the unsorted array
printf("\nthe array was: ");
for (int i = 0; i <= arrayLength; i )
{
printf(" %d ", myArray[i]);
}
// sorting the array using insertion sorting algorithm:
for (int index = 1; index <= arrayLength; index )
{
int numberCheck = index;
while (index >= 1 && myArray[numberCheck] < myArray[numberCheck - 1])
{
// swap the places:
int temp;
temp = myArray[numberCheck];
myArray[numberCheck] = myArray[numberCheck - 1];
myArray[numberCheck - 1] = temp;
// move the next element
numberCheck--;
}
}
// printing the sorted array:
printf("\nthe sorted array is now: ");
for (int i = 0; i <= arrayLength; i )
{
printf(" %d ", myArray[i]);
}
return 0;
}
for example if I enter (0,-2,-4,12,5) on C Online Compiler - Programiz I get this result :
the array was: 0 -2 -4 12 5
the sorted array is now: -4 -2 0 5 12
but if I enter the same exact code on the Vscode Zsh compiler (I'm using a MacBook and to my knowledge, I didn't change anything on the compiler settings) I get the result :
the array was: 0 -2 -4 12 5
the sorted array is now: 5 6
CodePudding user response:
I tested out your code and found a few areas where processing of array elements was going out of bounds. Some places, the value of the index was -1 and some places the value was equal to the array length value (e.g. index values that equate to myArray[5] which again is out of bounds).
Following is a copy of your code with a bit of cleanup to illustrate some usual and customary methods for processing "for" loops and processing arrays.
#include <stdio.h>
#include <string.h>
int main()
{
// gathering sources for sorting an array:
int myArray[5];
int count = 1;
for (int arrayIndex = 0; arrayIndex < 5; arrayIndex )
{
printf("\nEnter the #%d array element: ", count);
scanf(" %d", &myArray[arrayIndex]);
count ;
}
// calculate the last array index
int myInt = myArray[0];
int arrayLength = ((sizeof(myArray)) / (sizeof(myInt))); /* Omittted the subtraction of 1 */
// printing the unsorted array
printf("\nthe array was: ");
for (int i = 0; i < arrayLength; i ) /* Set loop test to be less than array length */
{
printf(" %d ", myArray[i]);
}
// sorting the array using insertion sorting algorithm:
for (int index = 1; index < arrayLength; index )
{
int numberCheck = index;
//while (index >= 1 && myArray[numberCheck] < myArray[numberCheck - 1]) /* This line of code was allowing numberCheck - 1 to be less than zero */
while (numberCheck > 0 && myArray[numberCheck] < myArray[numberCheck - 1]) /* Revised version of the test */
{
// swap the places:
int temp;
temp = myArray[numberCheck];
myArray[numberCheck] = myArray[numberCheck - 1];
myArray[numberCheck - 1] = temp;
// move the next element
numberCheck--;
}
}
// printing the sorted array:
printf("\nthe sorted array is now: ");
for (int i = 0; i < arrayLength; i ) /* Revised this to not go out of bounds */
{
printf(" %d ", myArray[i]);
}
printf("\n");
return 0;
}
It appears in the program that you are attempting set up for loop and range testing based on a range of "1" to "array length"; whereas, the usual range processing is from "0" to "less than array length". When I did those bits of cleanup, I was able to acquire a properly sorted array from the five values entered.
@Una:~/C_Programs/Console/Sorting/bin/Release$ ./Sorting
Enter the #1 array element: 700
Enter the #2 array element: 343
Enter the #3 array element: 2
Enter the #4 array element: 58
Enter the #5 array element: 400
Array length: 5
the array was: 700 343 2 58 400
the sorted array is now: 2 58 343 400 700
Note the comments I added to hopefully clarify bits for you. Try that out and see if it meets the spirit of your project.