简体   繁体   中英

implementing bubble sort program in c

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;
}

If you run your compiler with warnings turned on (eg in gcc -Wall would be an option.) you would see warning similar to the following:

Build Status (so.prj - Debug)
 s0_15.c - 1 warning
  25, 27    warning: variable 'size' is uninitialized when used here 
      25, 21    note: initialize the variable 'size' to silence this warning
 Link so.exe
 "c:\Play\so.exe" successfully created.
Build succeeded. 

A minimal fix to this warning is to simply initialize the variable size before using it...

int main(void) {
         int i, size;
         printf("\n Enter Array Size ....");
         if(scanf("%d", &size) != 1) {/*handle error and leave*/ }
         if(size <= 0){/*handle error and leave*/ }
         int arr[size];//because this is a VLA, it cannot be 
                       //initialized upon declaration...
         memset(arr, 0, sizeof arr);//...so initialize here before using.
         ...  

( Note that VLAs are available when using C99, and optionally defined in compilers since then. Read your compiler documentation to know before attempting this approach. )

This is your problem:

int i, size, arr[size];

When you declare an array, its size has to be known at the point of declaration - it either needs to be a constant expression (like 5 ), or it needs to be a variable that has been properly initialized or assigned a good value.

At this point, size has not been initialized or assigned, and its value is indeterminate . arr will be sized to whatever that value is, and if the value is 0 or negative, then the behavior will be undefined. That size will not change after the declaration, regardless of whatever you store to size later on.

So you need to defer the declaration of arr until after you've read a value into size :

int i, size;
printf("\n Enter Array Size ....");
scanf("%d", &size);

int arr[size];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM