为什么不计算数组的元素?

时间:2014-10-16 20:27:12

标签: c

#include "stdafx.h"
#include<stdio.h>



int main (void)
{

//第1部分。这完全正常

int array[20];
        int n;
        for(n = 0; n <10; ++n)
        {
        array[n] = 0;
    }

    for(n = 10; n <20; ++n)
    {
        array[n]= n-9;
    }

    for(n = 4; n <10; ++n)
    {
        array[n]= array[n]+1;
    }

    for(n = 0; n < 20; ++n)
    {
        array[n] = array[n] * 3;
    }

    for(n=0; n < 20; ++n)
    {
        printf( "%i \t" ,array[n]);
    }

//第2部分。这是我遇到麻烦的地方。我应该创建一个直方图和一个//计算每个响应的表。总共1个,2个等等。我刚刚得到100个1。 //为什么呢?

int array2[100] = {6, 7, 8, 9, 8, 7, 8, 9, 7, 8, 9, 8, 9, 8, 9, 5, 9, 8, 7, 8, 7, `8, 6, 7, 8`, 
9, 3, 9, 8, 7, 8, 9, 9, 8, 9, 8, 9, 8, 9, 7, 8, 9, 6, 7, 8, 7, 8, 7, 9, 7, 
6, 5, 3, 5, 6, 7, 2, 5, 3, 9, 4, 6, 4, 7, 8, 9, 6, 8, 7, 8, 9, 7, 8, 7, 4, 
4, 3, 6, 4, 6, 7, 5, 6, 4, 5, 6, 1, 6, 5, 7, 8, 5, 3, 6, 3, 5, 7, 5, 8, 9};
float sum=0.0;
float  average=0;
int j, i,d;
int max=0;
int mode =0;

for (n=0; n<100; ++n)
    {

        sum = sum + array2[n];
    }
    //int average=0;
      average= sum/100;
    printf("\n\nThe mean of the responses in the array is %.2f", average);
    int  maxCount=0; 
        int modeValue;
        int tally[100]={0};
    for (i = 0; i < 100; i++) 
    {
         tally[array2[i]]++;
    }
    //maxCount = 0;
    //modeValue = 0;
    for (j = 0; j < 100; j++) 
    {
        if (tally[j] > maxCount) 
        {
            maxCount = tally[j];
            modeValue = j;
        }
    }
    printf("\n\nThe mode is %i", modeValue);

//我的问题在这里:为什么它都注册为1。我得到了100个。 //我做错了什么

int onecount=0;
        int twocount=0;
        int threecount=0;
        int fourcount=0;
        int fivecount=0;
        int sixcount=0;
        int sevencount=0;
        int eightcount=0;
        int ninecount=0;

        for (d=0;d<100;d++)
        {
        if (array2[d] =1)

                ++onecount;

            else if (array2[d]=2)

                ++twocount;


            else if (array2[d]=3)

                ++threecount;


            else if (array2[d]=4)

                ++fourcount;


            else if (array2[d] =5)

                ++fivecount;


            else if(array2[d] =6)

                ++sixcount;


            else if(array2[d] =7)

                ++sevencount;


            else if(array2[d] =8)

                ++eightcount;


            else if(array2[d]=9)

                ++ninecount;
        }

        int star=0;

        printf("\n\nHistogram of of Survey responses:\n");
            printf("\n1's: ");
            for(star=0; star<onecount; ++star) //print 1 part of histogram
                {
                printf("*");
                }
            printf("\n2's : ");
            for(star=0; star<twocount; ++star) //print 2part of histogram
                {
                printf("*");
                }
            printf("\n3's: ");
            for(star=0; star<threecount; ++star) //print 3 part of histogram
                {
                printf("*");
                }
            printf("\n4's: ");
            for(star=0; star<fourcount; ++star) //print 4 part of histogram
                {
                printf("*");
                }
            printf("\n5's : ");
            for(star=0; star<fivecount; ++star) //print 5 part of histogram
                {
                printf("*");
                }
            printf("\n6's: ");
            for(star=0; star<sixcount; ++star) //print 6 part of histogram
                {
                printf("*");
                }
            printf("\n7's: ");
            for(star=0; star<sevencount; ++star) //print 7 part of histogram
                {
                printf("*");
                }
            printf("\n8's : ");
            for(star=0; star<eightcount; ++star) //print 8 part of histogram
                {
                printf("*");
                }
            printf("\n9's: ");
            for(star=0; star<ninecount; ++star) //print 9 part of histogram
                {
                printf("*");
                }

            printf("\n\nTable of Survey Responses: \n\n");
            printf("1's: %i\t\t2's:%i\t\t3's:%i\t\t4's:%i\t\t5's:%i\t\t6's:%i\t\t7's:%i\t\t8's:%i\n\t\t9's:%i", onecount, twocount, threecount,fourcount,fivecount,sixcount,sevencount,eightcount,ninecount);

        return 0;
    }

1 个答案:

答案 0 :(得分:1)

在此代码中:

if (array2[d] =1)

        ++onecount;

    else if (array2[d]=2)

        ++twocount;

您需要使用&#39; ==&#39;而不是&#39; =&#39;,这是C中的赋值运算符。

例如,

if( array2[d] == 1 )