0

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;
}
2
  • 4
    int size, arr[size] That won't work because size needs to have a value before array is declared. Declare array after the scanf. Also, always check the return value of scanf to ensure the size value 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. Commented Mar 3, 2021 at 20:46
  • kaylum is correct. You would need to either allocate the array with a maximal size or allocate it dynamically (malloc). Commented Mar 3, 2021 at 20:53

2 Answers 2

1

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. )

Sign up to request clarification or add additional context in comments.

1 Comment

@Imtiyaz - You are welcome. You can mark one or the other of the answers here as accepted by clicking the hollow check mark.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.