2015-10-14 57 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