2015-10-19 35 views
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; 
      } 
     } 
} 

,當我在代碼塊編譯它,它給:使用功能排序數組中C,但計劃只是不運行

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

,當我運行程序,它只是崩潰。

任何線索?

+0

什麼是你的輸入? – Downvoter

+0

當你運行它時有沒有輸出? – Arc676

+0

使用'-Wall'選項編譯它。你會發現很多東西... – LPs

回答

2

問題是: - 您正在使用arraySize而未初始化它。

我剛剛swaped兩個語句和它的作品fine.:-

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

    int arrayNums[arraySize]; 
+1

你糾正它,只是沒有意識到它 –

相關問題