免责声明这不是我的代码,这段代码来自删除C- Javatpoint中的数组中的重复元素
我想知道的是示例2编码部分。(我为我编辑了一些代码,否则您可以清楚地看到代码。)
/* program to delete the duplicate elements from sorted array in C. */
#include <stdio.h>
int duplicate_element ( int arr[], int num)
{
// check num is equal to 0 and num == 1
if (num == 0 || num == 1)
{
return num;
}
// create temp array to store same number
int temp [num];
// declare variable
int i, j = 0;
// use for loop to check duplicate element
for (i = 0; i < num - 1; i++)
{
// check the element of i is not equal to (i + 1) next element
if (arr [i] != arr[i + 1])
{
temp[j++] = arr[i];
}
}
temp[j++] = arr[ num - 1];
// check the original array's elements with temporary array's elements
for (i = 0; i < j; i++)
{
arr[i] = temp[i];
}
return j;
}
int main ()
{
int num;
printf (" Define the no. of elements of the array: ");
scanf (" %d", &num);
int arr[num], i;
printf (" Enter the elements: ");
// use loop to read elements one by one
for ( i = 0; i < num; i++)
{
scanf (" %d", &arr[i]);
}
printf (" \n Elements before removing duplicates: ");
for ( i = 0; i < num; i++)
{
printf (" %d", arr[i]);
}
num = duplicate_element (arr, num);
// print array after removing duplicates elements
printf (" \n Display array's elements after removing duplicates: ");
for ( i = 0; i < num; i++)
{
printf (" %d", arr[i]);
}
return 0;
} 问题是,函数duplicate_element中的所有duplicate_element都是做什么的?(如果可能的话,我想知道代码正在做什么,因为行// use for循环检查重复的元素,直到返回。这部分我只是好奇我是否正确地知道它。)
这是我的理解(j是arr[]的最终大小)。在第一个问题中,执行时
现在的J是0
tempj++
它是否加上j的值1,然后将值arri赋给temp1。(这样做对吗?)
第二个问题,在第一个for循环中,当arri中的值不等于arri +1中的值时,然后在tempj++中用arri中的值赋值,直到for循环结束,然后用arrnum -1分配tempj++。
(j++现在依赖于if条件,例如,当所有值都不等于num -1的j++ ==值和num -1等于arr的最后值时)
在最后一个for循环中,它用Array temp分配数组arr中的每个值。(这样做对吗?)
发布于 2022-10-28 21:06:35
首先,代码是非常糟糕的。
首先,函数应该声明为
size_t duplicate_element ( int arr[], size_t num ); 也就是说,应该使用无符号整数类型size_t来指定传递数组的大小,而不是使用带符号的int类型int。否则,变量长度数组的声明
// create temp array to store same number
int temp [num]; 连同这句话
temp[j++] = arr[ num - 1];将调用未定义的行为,如果用户将负数作为第二个参数传递,并且根据函数规范,它允许传递负数。
其次,使用可变长度数组temp。
// create temp array to store same number
int temp [num]; 使功能不安全。这种情况可能导致程序无法定义此可变长度数组。
这种方法过于复杂、混乱和效率低下。
至于您关于后缀操作符++的问题,则按照C标准(6.5.2.4后缀增量和递减运算符)
2后缀++运算符的结果是操作数的值。副作用是,操作数对象的值增加(也就是说,适当类型的值1被添加到它中)。
所以事实上这句话
temp[j++] = arr[i];可以等效地重写如下
temp[j] = arr[i];
j += 1;当函数将数组temp添加到数组arr中的一系列重复元素中的最后一个元素时,然后在主循环之后
// use for loop to check duplicate element
for (i = 0; i < num - 1; i++)
{
// check the element of i is not equal to (i + 1) next element
if (arr [i] != arr[i + 1])
{
temp[j++] = arr[i];
}
}需要将最后一个元素pf数组arr添加到数组temp中。
temp[j++] = arr[ num - 1];下面是一个演示程序,演示如何重写该函数,并使其看起来更简单。
#include <stdio.h>
size_t duplicate_element( int a[], size_t n )
{
size_t m = 0;
for (size_t i = 0; i < n; i++)
{
if (i == 0 || a[i] != a[m-1])
{
if (i != m) a[m] = a[i];
++m;
}
}
return m;
}
int main( void )
{
int a[] = { 1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5 };
const size_t N = sizeof( a ) / sizeof( *a );
for (size_t i = 0; i < N; i++)
{
printf( "%d ", a[i] );
}
putchar( '\n' );
size_t m = duplicate_element( a, N );
for (size_t i = 0; i < m; i++)
{
printf( "%d ", a[i] );
}
putchar( '\n' );
}程序输出是
1 2 2 3 3 3 4 4 4 4 5 5 5 5
1 2 3 4 5发布于 2022-10-28 20:00:52
简而言之,声明
temp[j++] = arr[i];等于
int old_value_of_j = j;
j = j + 1;
temp[old_value_of_j] = arr[i];https://stackoverflow.com/questions/74240261
复制相似问题