0
#include <stdio.h>
#include <conio.h>

void sorter(int b, int a[]);

int main(void){
    int i, j, arraySize;
    int arrayNums[arraySize];

    printf("Please enter how many numbers you wish to enter: ");
    scanf("%d", &arraySize);

    for(i =0; i<arraySize; i++)
    {
        printf("Enter Value No. %d: ", i+1);
        scanf("\n%d", &arrayNums[i]);
    }

    for(i=0; i<arraySize; i++)
    printf("%d", arrayNums[i]);

    sorter(arraySize,arrayNums);

    printf("after sorting");

    for(i=0; i<arraySize; i++)
    {
        printf("\n%d", arrayNums[i]);
    }
}

void sorter(int b, int a[]){
    int i,j, swap;

    for(i = 0; i<b; i++)
        for(j=i; j<b; j++)
        {
            if(a[i]>a[j]){
                swap= a[i];
                a[i]=a[j];
                a[j]=swap;
            }
        }
}

when I compile it in CodeBlocks, it gives:

Process terminated with status 0 (0 minute(s), 0 second(s))
0 error(s), 0 warning(s) (0 minute(s), 0 second(s))

and when I run the program, it just crashes.

Any clues?

5
  • And is there any output when you run it? Commented Oct 19, 2015 at 10:19
  • Compile it with -Wall option. You will discover a lot of things... Commented Oct 19, 2015 at 10:27
  • whoops! previously arrayNums was declared after taking arraySize's value (when I was simply sorting the array without the function), don't know when and why I changed it. How did I nott notice this. sorry for wasting your time Commented Oct 19, 2015 at 10:30
  • You have to allocate dynamically, since you do not know the size of the array at compile time Commented Oct 19, 2015 at 11:10
  • can you please explain it?? @m47h, I mean what you want to say?? Commented Oct 19, 2015 at 14:33

1 Answer 1

2

The problem is :- you are using arraySize without initializing it.

I've just swaped two statements and it works fine.:-

     printf("Please enter how many numbers you wish to enter: ");
     scanf("%d", &arraySize);

     int arrayNums[arraySize];
Sign up to request clarification or add additional context in comments.

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.