【问题标题】:C program to compare two non equal size arraysC程序比较两个不相等大小的数组
【发布时间】:2019-12-27 19:03:01
【问题描述】:

我编写了一个程序来比较 2 个不同大小的数组,程序应该比较数组并返回第一个数组的索引,其中开始相等,但是对于某些语法或逻辑问题,程序无法正常工作。所以请查看代码并告诉我问题出在哪里。谢谢。

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

   int subArray( int first[], int size1, int second[], int size2)
  {

 int key = second [0] ; //init key of first element in second array
 int i;
 int y;
 int found,count;


 for (i = 0 ;size1 < i; i++) 
{
    if (first[i] == key)
  count ++;   
  found = 1;
}

     if (found== 1)  // should compare first and second array and return -1 if not equal
  {
   for (y = 0 ;size2 < y; y++) 
    {
      if (first[i] != second[y])

        result =-1;
    else
    {
        i++;

    }

  }


  return result=count; //return index of first array where equal startring
}


int main()

{

  int result;
  int sizeArr1;
  int sizeArr2;
  int  arr1[40]={0};
  int  arr2[40]={0}; 
 // taking input and storing it in first array
   scanf_s("%d", &sizeArr1);
   for(int i = 0; i < sizeArr1; ++i)
   {
    scanf_s("%d", &arr1[i]);

   }
     // taking input and storing it in second array
     scanf_s("%d", &sizeArr2);
    for(int i = 0; i < 5; ++i)
   {
    scanf_s("%d", &arr2[i]);

    }
     result = subArray(arr1,sizeArr1,arr2,sizeArr2);
     printf("%d",result);
   }

【问题讨论】:

  • “不工作”从来都不是一个好的问题描述。发生什么了?它会崩溃吗?它什么都不打印吗?它有时会得到正确的答案吗?等给出输入、预期输出和实际输出。还有你做了什么来调试它 - 例如在调试器中运行和/或添加更多调试打印?
  • @Alexander Bazikalo 你想判断第二个数组是否是第一个数组的子数组吗?
  • 学习正确缩进你的代码。良好的缩进可以帮助找到细微的错别字,例如下面的答案所指出的错别字。

标签: c arrays compare


【解决方案1】:

你好像忘了块:

for (i = 0; size1 < i; i++) 
{
    if (first[i] == key)
    {
        count++;   
        found = 1;
    }
}

在您的原始代码中,found 将在每个 for 循环迭代中设置为 1,而不依赖于 if 条件。

注意:您的代码格式非常糟糕,代码缩进和空白(空)行是完全随机的。就是这样。

【讨论】:

  • 如果这是编写的代码,则第 24 行 (if (first[i] != second[y])) 上的条件也可能存在问题,因为它似乎没有任何关联代码。
  • Nope ;) 下面有几行,在单个空行之后 - 这个空行被编译器忽略,但是 - 我同意 - 这样的事情使代码几乎完全不可读。
  • 如果可以的话,请帮忙修改这段代码。
  • @VillageTech 啊,没有意识到这样的空白行被忽略了(我从来没有给自己编写足够有创意的代码来使用这些功能的乐趣)。
  • @AlexanderBazikalo - 抱歉,请尝试自己做,但在尝试之前,请阅读一些关于代码格式化方面的良好做法的内容。相信我,你一定能做到。如果我为你做,你仍然不知道怎么做。
【解决方案2】:

对于初学者来说,函数声明是文盲。

表示数组的参数应使用限定符 const 声明,因为数组不会在函数中更改。

其次,数组中元素的数量应具有size_t 类型。所以函数的返回类型也应该是size_t

如果第二个数组不是第一个数组的子数组,则函数应返回第一个数组的大小,即超出数组有效索引范围的索引。

您的函数 1) 在逻辑上是无效的,因为它只找到了第二个数组的第一个字符的位置,尽管子数组可以从其他位置开始,并且 2) 它有错误,例如这个循环

 for (i = 0 ;size1 < i; i++) 
{
    if (first[i] == key)
  count ++;   
  found = 1;
}

如果 size1 不是负数,则不会执行任何迭代,因为 size1 不能小于 0。

该函数可以通过以下方式声明和定义,如下面的演示程序所示。

#include <stdio.h>

size_t subArray( const int first[], size_t size1, const int second[], size_t size2 )
{
    size_t pos = size1;

    if ( !( size1 < size2 ) )
    {
        for ( size_t i = 0; pos == size1 && i < size1 - size2 + 1; i++ )
        {
            size_t j = 0;

            while ( j < size2 && first[i+j] == second[j] ) j++;

            if ( j == size2 ) pos = i;
        }
    }

    return pos;
}

int main(void) 
{
    int first[] = { 1, 2, 3, 4, 5, 6, 7 };
    int second[] = { 3, 4, 5 };
    const size_t SIZE1 = sizeof( first ) / sizeof( *first );
    const size_t SIZE2 = sizeof( second ) / sizeof( *second );

    size_t pos = subArray( first, SIZE1, second, SIZE2 );

    if ( pos != SIZE1 )
    {
        for ( size_t i = 0; i < SIZE2; i++ )
        {
            printf( "%d ", first[pos + i] );
        }

        putchar( '\n' );
    }

    return 0;
}

程序输出是

3 4 5 

【讨论】:

    【解决方案3】:

    您的int subArray() 函数的右括号丢失了,您的result 变量应该是全局变量。 我在代码中提到的其他错误。

    #include<bits/stdc++.h>
    
    int result;
    
       int subArray( int first[], int size1, int second[], int size2)
      {
    
     int key = second [0] ; //init key of first element in second array
     int i;
     int y;
     int found,count=0;
    
    
     for (i = 0 ;size1 > i; i++)   // comparator sign was opposite
    {
        if (first[i] == key){      // brackets were missing here.
          count = i;   
          found = 1;
          printf("value found in array 1 at index number : ");
        }
    }
    
     if (found== 1)  // should compare first and second array and return -1 if not equal
     {
    
    
    for (y = 0 ;size2 > y; y++)   // comparator sign was opposite
    {
      if (first[i] != second[y])
    
        result =-1;
    else
    {
        i++;
    
    }
    
    
    
    }
      }
    
    
      return result=count; //return index of first array where equal startring
    }
    
    
    int main()
    
    {
    
    
      int sizeArr1;
      int sizeArr2;
      int  arr1[40]={0};
      int  arr2[40]={0}; 
     // taking input and storing it in first array
       scanf("%d", &sizeArr1);
       for(int i = 0; i < sizeArr1; ++i)
       {
        scanf("%d", &arr1[i]);
    
       }
         // taking input and storing it in second array
         scanf("%d", &sizeArr2);
        for(int i = 0; i < sizeArr2; ++i)    // here instead of taking array2 size it was hard coded to 5
       {
        scanf("%d", &arr2[i]);
    }
     result = subArray(arr1,sizeArr1,arr2,sizeArr2);
     printf("%d",result);
    }
    

    【讨论】:

    • 不工作,你能检查一下,我有一些逻辑错误,程序没有比较并返回任何东西......
    • 给出输入和预期输出。
    • @AlexanderBazikalo 使用更正的代码和 cmets 编辑答案。
    猜你喜欢
    • 1970-01-01
    • 2012-02-09
    • 1970-01-01
    • 2019-03-23
    • 1970-01-01
    • 2016-02-24
    • 2023-03-11
    • 2012-03-03
    • 1970-01-01
    相关资源
    最近更新 更多