C语言 将值按适当的组排序:计数器数组


  #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 Salespeoplen");

for (i = 0; i < 30; i++)
    {
        printf("%dn", 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("%fn", 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 : %dn", temp[0]);
        printf("$200-$299 : %dn", temp[1]);
        printf("$300-$399 : %dn", temp[2]);
        printf("$400-$499 : %dn", temp[3]);
        printf("$500-$599 : %dn", temp[4]);
        printf("$600-$699 : %dn", temp[5]);
        printf("$700-$799 : %dn", temp[6]);
        printf("$800-$899 : %dn", temp[7]);
        printf("$900-$999 : %dn", temp[8]);
        printf(">>>$1000  : %dn", temp[9]);
        break;
    }
}
return 0;

}

我正在编写一个程序,它在一个数组中随机选择30个数字,然后对这些数字使用数学方程,然后按每组对数字进行排序。我已经能够让一切工作,除了当它涉及到将它们分类到每一个选择性组。我做错了什么,我该怎么补救。顺便说一句,我对编程非常陌生,所以任何帮助都非常感谢

请试试:

#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 Salespeoplen" );
  for ( i = 0; i < 30; i++ )
  {
    printf( "%dn", 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( "%fn", 100 + ( float ) sales[i] * 0.09 );    //Mathematical equation
//  }

//让我们做点什么对于(I = 0;我& lt;30;我+ +){Printf("对%dn应用一些计算",sales[i]);销售额[i]=100 +(浮动)销售额[i] * 0.09;//数学方程Printf("并且它变成%fn", (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 : %dn", temp[0] );
    printf( "$200-$299 : %dn", temp[1] );
    printf( "$300-$399 : %dn", temp[2] );
    printf( "$400-$499 : %dn", temp[3] );
    printf( "$500-$599 : %dn", temp[4] );
    printf( "$600-$699 : %dn", temp[5] );
    printf( "$700-$799 : %dn", temp[6] );
    printf( "$800-$899 : %dn", temp[7] );
    printf( "$900-$999 : %dn", temp[8] );
    printf( ">>>$1000  : %dn", temp[9] );
  return 0;
}

最新更新