2015-10-14 53 views
0
#include <stdio.h> 
    #include <stdlib.h> 
    #include <math.h> 




int main(){ 

int i; 
int sales[30]; 
int arraySize = 0; 
int temp[10] = { 0 }; 

for (i = 0; i < 30; i++) 
    { 
     sales[i] = (rand() % 15000) + 1;  between 0 and 15000 
    } 


    printf("Gross Sales of all 30 Salespeople\n"); 


for (i = 0; i < 30; i++) 
    { 
     printf("%d\n", sales[i]);    //Displays Orginal List and Lists all values that were randomly selected 
    } 


printf("\nWage based on Gross Sales \n");      //Displays calculated wages with math equation 


for (i = 0; i < 30; i++) 
    { 
     printf("%f\n", 100 + (float)sales[i] * 0.09); //Mathematical equation 
    } 


for (i = 0; i < 30; i++) 

while (arraySize >= 0) 
{ 
    { 
     if (sales[arraySize] >= 1000) 
      temp[9]++; 
     else if (sales[arraySize] >= 900) 
      temp[8]++; 
     else if (sales[arraySize] >= 800) 
      temp[7]++; 
     else if (sales[arraySize] >= 700) 
      temp[6]++; 
     else if (sales[arraySize] >= 600) 
      temp[5]++; 
     else if (sales[arraySize] >= 500) 
      temp[4]++; 
     else if (sales[arraySize] >= 400) 
      temp[3]++; 
     else if (sales[arraySize] >= 300) 
      temp[2]++; 
     else if (sales[arraySize] >= 200) 
      temp[1]++; 
     else 
      temp[0]++; 


     arraySize--; 

     printf("\n"); 
     printf("$100-$199 : %d\n", temp[0]); 
     printf("$200-$299 : %d\n", temp[1]); 
     printf("$300-$399 : %d\n", temp[2]); 
     printf("$400-$499 : %d\n", temp[3]); 
     printf("$500-$599 : %d\n", temp[4]); 
     printf("$600-$699 : %d\n", temp[5]); 
     printf("$700-$799 : %d\n", temp[6]); 
     printf("$800-$899 : %d\n", temp[7]); 
     printf("$900-$999 : %d\n", temp[8]); 
     printf(">>>$1000 : %d\n", temp[9]); 

     break; 
    } 
} 
return 0; 

}排序值成正確的羣組:計數器陣列

我正在一個程序,在陣列中隨機地選擇30個號碼,然後使用對數的數學方程式,然後排序按照每個數組。除了將它們分類到每個選擇性組中時,我一直能夠使所有工作都能夠正常工作。我做錯了什麼,我該如何解決這個問題。 BTW即時消息程序,所以任何的幫助深表感謝

+1

'arraySize'設置爲'0',然後不只是在最後'for'循環(它看起來像它預計將有非零值)又變了。 – kaylum

+0

您將'0'分配給'arraySize',但在查看'sales [arraySize]'之前不要做任何其他操作,只需查看'sales [0]'。我認爲你的意思是遍歷「銷售」並根據內容進行排序?所以你只需要像計算時那樣使用for循環,並在比較中查看'sales [i]'。 – DigitalNinja

+1

此外,您還打印出「數學公式」的值,但實際上並未將結果重新分配到「sales」數組中(看起來您要計算這些值的出現次數而非原始銷售值)。 – kaylum

回答

0

請試試這個很新:

#include <stdio.h> 
#include <stdlib.h> 
#include <math.h> 

int main() 
{ 
    int i; 
    int sales[30]; 
    int arraySize = 0; 
    int temp[10] = { 0 }; 

    for (i = 0; i < 30; i++) 
    { 
    sales[i] = (rand() % 15000) + 1; // between 0 and 15000 
    } 

    printf("Gross Sales of all 30 Salespeople\n"); 

    for (i = 0; i < 30; i++) 
    { 
    printf("%d\n", sales[i]); //Displays Orginal List and Lists all values that were randomly selected 
    } 
    printf("\nWage based on Gross Sales \n"); //Displays calculated wages with math equation 


// this does not do anything 
// for (i = 0; i < 30; i++) 
// { 
// printf("%f\n", 100 + (float) sales[i] * 0.09); //Mathematical equation 
// } 

//允許用於做點什麼 (i = 0;我< 30;我++) { 的printf( 「在這裏應用一些計算到%d \ n」,sales [i]); sales [i] = 100 +(float)sales [i] * 0.09; //數學公式 printf(「它變成%f \ n」,(float)sales [i]); }

for (i = 0; i < 30; i++) 
     { 
      printf("\n sorting sales %d == %d " , i , sales[i]); 

    if (sales[i] >= 1000) 
     temp[9]++; 
    else if (sales[i] >= 900 && sales[i]<=999) 
     temp[8]++; 
    else if (sales[i] >= 800 && sales[i]<=899) 
     temp[7]++; 
    else if (sales[i] >= 700 && sales[i]<=799) 
     temp[6]++; 
    else if (sales[i] >= 600 && sales[i]<=699) 
     temp[5]++; 
    else if (sales[i] >= 500 && sales[i]<=599) 
     temp[4]++; 
    else if (sales[i] >= 400 && sales[i]<=499) 
     temp[3]++; 
    else if (sales[i] >= 300 && sales[i]<=399) 
     temp[2]++; 
    else if (sales[i] >= 200 && sales[i]<=299) 
     temp[1]++; 
    else 
     temp[0]++; 

} 

    printf("\n"); 
    printf("$100-$199 : %d\n", temp[0]); 
    printf("$200-$299 : %d\n", temp[1]); 
    printf("$300-$399 : %d\n", temp[2]); 
    printf("$400-$499 : %d\n", temp[3]); 
    printf("$500-$599 : %d\n", temp[4]); 
    printf("$600-$699 : %d\n", temp[5]); 
    printf("$700-$799 : %d\n", temp[6]); 
    printf("$800-$899 : %d\n", temp[7]); 
    printf("$900-$999 : %d\n", temp[8]); 
    printf(">>>$1000 : %d\n", temp[9]); 
    return 0; 
} 
+0

除了行:printf(「%f \ n」,100 +(float)sales [i] * 0.09)是需要的,因爲對於生成的每個隨機數,我必須應用該方程,然後使用該結果編號排序到相應的組,而不是將隨機數直接排序到組 – bobblehead808

+0

好吧,我用'讓我做點事'取代它,但如果你想計算9%的銷售額,我認爲你的公式是錯誤的。 – BobRun

+0

「,它變成%f \ n」。我不認爲是這樣。 '銷售[i]'是一個'int'。一旦存儲到'sales [i]'中,float值已經被截斷。所以將它轉換爲'float'並不是那麼有用(它只會導致它在小數點後面輸出大量的'0'值)。 – kaylum