【问题标题】:How to correctly compare and print out matching elements in this array in C?如何在 C 中正确比较和打印此数组中的匹配元素?
【发布时间】:2014-07-28 06:24:51
【问题描述】:

我有一个简单的问题,我正在尝试用 C 语言编写解决方案。

If an array arr contains n elements, then write a program to check 
if arr[0] = arr[n-1], arr[1] = arr[n-2] and so on.

我的代码看起来像这样-

#include<stdio.h>
int main()
{
    int arr[10],i=0,j;
    int k=0;
    printf("\n Enter 10 positive integers: \n");
    for(k=0;k<=9;k++)
    scanf("%d",&arr[k]);

    while(i<=9)
    {
        for(j=9;j>=0;j--)
        {
            if(arr[i]==arr[j])
            {
                printf("\n The array element %d is equal to array element %d\n", arr[i],arr[j]);
            }

            i++;
            continue;
        }
    }

    return 0;
}

在输入此输入时-

 Enter 10 positive integers:
10
20
30
40
50
60
40
80
20
90

我得到的输出是-

 The array element 20 is equal to array element 20

 The array element 40 is equal to array element 40

 The array element 40 is equal to array element 40

 The array element 20 is equal to array element 20

现在,这段代码有两个问题——

  1. 如您所见,程序打印出匹配的数组元素两次。这是因为,按照我构建程序的方式,一旦变量i 从第一个元素到最后一个元素循环遍历数组,然后j 从最后一个元素循环到第一个元素。因此,每个都打印出匹配的数组元素一次,从而产生两组值。
  2. 我的第二个问题是 - 在我的代码中,我在 for 循环 (0 to 9 for an array of 10 elements) 中硬编码了数组的长度。可以进行哪些更改,以便用户输入的数组长度可以直接在 for 循环中使用?

我读过,在 C 中,array dimensions(声明时)不能是 variable。所以,像这样的declaration(这是我的第一个想法)是行不通的-

int n; // n is no. of elements entered by the user

int arr[n];

我是编程新手,所以如果问题听起来/太简单、质量低下,我深表歉意。

谢谢。

【问题讨论】:

    标签: c arrays loops nested-loops


    【解决方案1】:

    1) 你可以遍历数组一半时间来获得一次打印。您可以使用 for(j=9;j&gt;=9/2;j--) 代替 for(j=9;j&gt;=0;j--)

    2)

    int n;
    int arr[n].
    

    最近的编译器支持此声明。如果你不喜欢使用这个,你可以为数组动态分配内存。

    【讨论】:

      【解决方案2】:
      My second question is- In my code, I've hard-coded the length of the array in the for loops(0 to 9 for an array of 10 elements). What change can be done so that the length of the array, as entered by the user, can directly be used in the for loops?
      

      使用动态内存分配。使用 ma​​lloc()

      所以代码将是

      {
       int num_elements;
       int* arr;
       printf("Enter number of elements\n");
       scanf("%d", &num_elements);
      
       arr = (int *) malloc(num_elements * sizeof(int));  // Use this 'arr' for holding input data from user
      
       // Your remaining code comes here
      
      
       free(arr);  // Free the pointer in the end of program
      }
      

      【讨论】:

        【解决方案3】:

        可变长度创建对我有用:

        #include<stdio.h>
        
        int main(){
           int a, i;
           scanf("%i", &a);
           int blah[a];
        
           for (i = 0; i < a; i++){
              printf("/n%i", blah[a]);
           }
        }
        

        另一种方法是创建最大长度数组,而不是简单地使用前 n 个元素。

        正如前面的答案所述,您可以确保只检查每个元素一次,因此在元素 n/2 处停止。将 n/2 四舍五入到最接近的较小整数可能很重要,因此乍一看可能会以不同方式处理奇数个参数。但由于它只省略了中间元素,因此它与自身相同。

        【讨论】:

          【解决方案4】:

          第一次查询

          for(i=0;i<n/2;i++)
          {
               if(a[i]==a[n-(i+1)])
               {
                   printf("\n The array element %d is equal to array element %d\n",a[i],a[n-(i+1)]);
               }
          }
          

          对于您的第二个查询,您可以使用条件 i

          如果你想从 0 循环到 9,你可以使用

          for(i=0;i<n;i++)             
          

          对于 n 个元素组成的数组,其中 n 是一个变量,要么制作一个始终大于 n 的元素数组,要么制作一个动态数组。

          http://www.cs.swarthmore.edu/~newhall/unixhelp/C_arrays.html

          【讨论】:

            【解决方案5】:

            更正:

            #include<stdio.h>
            int main()
            {
                int i=0, size; // size of array
                int k=0;       // counter
            
                printf("enter size of array\n");
                scanf("%d", &size);                    // ask user for desired size
                int *arr = malloc(size * sizeof(int)); // allocate memory for array
            
                printf("\n Enter 10 positive integers: \n"); // fill your array of size size
                for(k=0;k<size;k++)
                scanf("%d",&arr[k]);
            
                k = 0;                           // reset this counter
                for(i=0; i<size/2; i++)          // check only for half of it
                {
                    if(arr[i] == arr[size-i-1])   // try it with paper and pincil
                    {
                       printf("match arr[%d]=arr[%d]=%d\n", i,size-i-1, arr[i]);
                       k++;
                    }
                }
            
                if(k==0) printf("No matching");
                return 0;
            }
            

            【讨论】:

            • 谢谢,更正很有帮助。但是,看起来您没有在最后一个 for 循环中增加 k 。因此,就目前而言,即使有匹配项,您也会得到“无匹配”声明。你是不是打算每次有匹配的时候在for循环中增加k,然后打印出k的值来表示匹配的个数?
            • @DarkKnight:哦!好像我发布了一个不完整的答案版本对不起!但是是的,我打算这样做,请参阅编辑
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-07-16
            • 1970-01-01
            相关资源
            最近更新 更多