I am just implementing bubble sort in c but i have to be face problem in taking array elements from user its not taking properly, suppose i enter array size 5 after that i am not able to store 5 elements in an array, i wrote this code within main function
#include<conio.h>
#include<stdio.h>
void bubbleSort(int *arr, int size) {
int temp, i, j;
for(i = 1; i < size; i++){
for(j = 0; j <= size - i -1 ; j++){
if(*(arr+j) > *(arr+j+1)) {
temp = *(arr+j);
*(arr+j) = *(arr+j+1);
*(arr+j+1)= temp;
}
}
}
}
void display(int *p, int s) {
for(int i = 0; i < s; i++){
printf("%d, ", *(p+i));
}
}
int main() {
int i, size, arr[size];
printf("\n Enter Array Size ....");
scanf("%d", &size);
printf("\n Enter %d array values...", size);
for(i = 0; i < size; i++){
scanf("%d", &arr[i]);
}
bubbleSort(arr, size);
display(arr, size);
return 0;
}
int size, arr[size]That won't work becausesizeneeds to have a value beforearrayis declared. Declarearrayafter thescanf. Also, always check the return value ofscanfto ensure thesizevalue is successfully set. It's also a good idea to check that the user doesn't enter invalid values such as negatives and extremely large numbers.malloc).