【问题标题】:Array not being updated inside if statement Cif 语句 C 内部未更新数组
【发布时间】:2014-07-30 10:33:17
【问题描述】:

在下面的代码中,我试图计算给定 int 中不同奇数的数量。为此,我首先使用模数将整数分成个位数的整数,然后将所有奇数放入数组oddInts。这很好用,但是,数组似乎只在 if 语句中保存值。在 if 语句之外,数组似乎有一堆随机数。

我的问题是,如何修改 if 语句中的数组,以便之后可以操作该数组。

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

int lenHelper(unsigned x) {
    if(x>=1000000000) return 10;
    if(x>=100000000) return 9;
    if(x>=10000000) return 8;
    if(x>=1000000) return 7;
    if(x>=100000) return 6;
    if(x>=10000) return 5;
    if(x>=1000) return 4;
    if(x>=100) return 3;
    if(x>=10) return 2;
    return 1;
}

int main(int argc, const char * argv[])
{
    int inputInt=987654;
    int size=lenHelper(inputInt);
    int input[size];
    int count=0;
    // int oddCount=0;
    int oddInts[5];

    for (int i=0; i<size; i++) {
        input[i]=inputInt%10;
        inputInt=inputInt/10;

        if (input[i]%2==1) {
            //  oddCount=input[i];
            oddInts[i]=input[i];
            printf("%d", oddInts[i]);
        }
    }
    return 0;
}

【问题讨论】:

  • lenHelper(987654) 看起来会返回 6,那么为什么 oddInts 声明为仅包含 5 个值?
  • 您的意思是:oddInts[ i/2 ] = input[i];
  • 使用调试器(并在编译器中启用所有警告和调试信息)

标签: c arrays if-statement modulus


【解决方案1】:

如果我理解正确,那么数组 oddInts 必须保持原始数字中每个奇数出现的频率。如果是这样,那么代码可以如下所示

// preceding declarations

int main(int argc, const char * argv[])
{
    unsigned int inputInt = 987654;
    int size = lenHelper( inputInt );
    unsigned int input[size];
    unsigned int oddInts[5] = { 0 };

    for ( int i = 0; i < size; i++ ) 
    {
        input[i] = inputInt % 10;
        inputInt = inputInt / 10;

        if ( input[i] % 2 == 1 ) 
        {
            int j = ( input[i] - 1 ) / 2;
            oddInts[j]++;
            printf( "%d\t%d", input[i], oddInts[j] );
        }
    }

    return 0;
}

对于我来说,我会按照以下方式编写程序(使用 C99 之前的 C)

#include <stdio.h>

#define BASE    10

int main( void ) 
{
    while ( 1 )
    {
        unsigned int oddInts[BASE / 2] = { 0 };
        unsigned int x = 0;
        unsigned int i;

        printf( "Enter a positive integer number (0 - exit): " );

        scanf( "%u", &x );

        if ( !x ) break;

        do 
        {
            unsigned int digit = x % BASE;

            if ( digit % 2 == 1 ) oddInts[(digit - 1 ) / 2]++;
        } while ( x /= BASE );

        for ( i = 0; i < BASE / 2; i++ )
        {
            printf( "\n%u\t%u", 2 * i + 1, oddInts[i] );
        }
        puts( "" );
    }

    return 0;
}

如果输入是

1234576543
0

然后输出将是

Enter a positive integer number (0 - exit): 1234576543
1   1
3   2
5   2
7   1
9   0
Enter a positive integer number (0 - exit): 0

【讨论】:

    【解决方案2】:
    int main(int argc, const char * argv[])
    {
    
        int value=0;     //Newly added variable
    
        for (int i=0; i<size; i++) {
            input[i]=inputInt%10;
            inputInt=inputInt/10;
    
            if (input[i]%2==1) {
                //  oddCount=input[i];
                oddInts[value]=input[i];     //oddInts[] is updated  
                printf("%d", oddInts[value]);//oddInts[value] is printed
                value++;                     //'value' is incremented
            }
        }
        return 0;
    }
    

    【讨论】:

      【解决方案3】:

      现在当数字不是奇数时你什么都不做,所以数组的这些元素可能包含垃圾,这就是你在那里有随机数的原因。 我在代码中添加了“else”以帮助您了解问题的原因。

       for (int i=0; i<size; i++) {
          input[i]=inputInt%10;
          inputInt=inputInt/10;
      
          if (input[i]%2==1) {
              //  oddCount=input[i];
              oddInts[i]=input[i];
              printf("%d ", oddInts[i]);
          } else {
              oddInts[i] = 0; //the digit is not odd
          }
      }
      
      
      for (i=0; i<size; i++) {
              printf("%d\n", oddInts[i]);
      }
      

      此外,您的代码不会按原样计算不同的奇数。它只是用它在数字中找到的所有奇数填充数组oddInts。 它不计算它们,也不检查数字是否不同。

      【讨论】:

        【解决方案4】:
        #include<stdio.h>
        #include <math.h>
        
        int lenHelper(unsigned x) {
                if(x>=1000000000) return 10;
                if(x>=100000000) return 9;
                if(x>=10000000) return 8;
                if(x>=1000000) return 7;
                if(x>=100000) return 6;
                if(x>=10000) return 5;
                if(x>=1000) return 4;
                if(x>=100) return 3;
                if(x>=10) return 2;
                return 1;
        }
        
        int main(int argc, const char * argv[])
        {
                int inputInt=987654;
                int size=lenHelper(inputInt);
                int input[size];
                int count=0,i,k=0;
                /* int oddCount=0; */
                int oddInts[5];
        
                for ( i=0; i<size; i++)
                {
                        input[i]=inputInt%10;
                        inputInt=inputInt/10;
                        if (input[i]%2==1)
                        {
                                //  oddCount=input[i];
                                oddInts[k]=input[i];
                                k++;
                        }
                }
        
                for(i=0;i<k;i++)
                {
                        printf("%d", oddInts[i]);
                }
                return 0;
        }
        

        【讨论】:

          猜你喜欢
          • 2016-03-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-07-06
          • 2016-08-31
          相关资源
          最近更新 更多