随机产生100个元素的一维数组a和10X10的二维数组b,求a,b数组的最大值,最小值,均值,总和.

1个回答

  • #include

    #include

    void main()

    {

    x05int i,j;

    x05float suma,sumb;

    x05float avga,avgb;

    x05int maxa,maxb;

    x05int mina,minb;

    x05int a[100];

    x05int b[10][10];

    x05unsigned seed;

    x05printf("please input seed:");

    x05scanf("%d",&seed);

    x05srand(seed);

    x05suma = 0.0;

    x05for(i = 0; i < 100; i++)

    x05{

    x05x05a[i] = rand() % 100;

    x05x05suma += a[i];

    x05}

    x05avga = suma / 100.0;

    x05sumb = 0.0;

    x05for(i = 0; i < 10; i++)

    x05x05for(j = 0; j < 10; j++)

    x05x05{

    x05x05x05b[i][j] = rand() % 100;

    x05x05x05sumb += b[i][j];

    x05x05}

    x05x05avgb = sumb / 100.0;

    x05mina = maxa = a[0];

    x05minb = maxb = b[0][0];

    x05for(i = 0; i < 100; i++)

    x05{

    x05x05if(mina > a[i])

    x05x05x05mina = a[i];

    x05x05if(maxa < a[i])

    x05x05x05maxa = a[i];

    x05}

    x05for(i = 0; i < 10; i++)

    x05x05for(j = 0; j < 10; j++)

    x05x05{

    x05x05x05if(minb > b[i][j])

    x05x05x05x05minb = b[i][j];

    x05x05x05if(maxb < b[i][j])

    x05x05x05x05maxb = b[i][j];

    x05x05}

    x05x05printf("数组a的最大值是%d,最小值是%d,均值是 %.3fn,总和是%.3fnn",maxa,mina,avga,suma);

    x05printf("数组b的最大值是%d,最小值是%d,均值是 %.3fn,总和是%.3fnn",maxb,minb,avgb,sumb);

    }