如何在C中初始化未知大小的数组

罗素·戴维斯(Russell Davis)

我正在为C语言编程课的入门做作业。

我需要编写一个程序,该程序查看int未知大小数组(我们将获得一个初始化程序列表作为要使用的测试用例),并确定该数组中的所有重复项。

为了确保不会发现已经重复的元素,我想对原始元素使用一个并行数组,该数组将保存所有重复元素的数量。

我需要此数组的大小与原始数组的大小相同,当然,在提供初始化程序列表之前,我们当然并不真正知道它。

我尝试使用sizeof()来实现此目的,但Visual Studio表示这是错误的,因为可变大小(const int size = sizeof(array1);)不一致。我没有正确使用sizeof吗?还是这种逻辑有缺陷?

也许还有另一种方法可以解决这个问题,但是我还没有想出一个办法。

这是下面包含的代码,希望注释不要太难阅读。

// Dean Davis
// Cs 1325
// Dr. Paulk
// Duplicates hw

#include <stdio.h>

int main()
{
    int array1[] = {     0,0,0,0,123,124,125,3000,3000,82,876,986,345,1990,2367,98,2,444,993,635,283,544,    923,18,543,777,234,549,864,39,97,986,986,1,2999,473,776,9,23,397,15,822,1927,1438,1937,1956,7, 29,- 1 };
const int size = sizeof(array1);
int holdelements[size]; 
int a = 0; // counter for the loop to initialize the hold elements array
int b = 0; // counter used to move through array1 and be the element number of the element being tested
int c = 0; // counter used to move through holdelements and check to see if the element b has already been tested or found as duplicates
int d = 0; // counter used to move through array1 and check to see if there are any duplicates 
int e = 0; // counter used to hold place in hold element at the next element where a new element number would go. sorry if that makes no sense
int flag = 0; // used as a boolian to make sure then large while loop ends when we reach a negative one value.
int flag2 = 0; // used as a boolian to stop the second while loop from being infinite. stops the loop when the end of hold elements has been reached
int flag3 = 0; // used to close the third while loop; is a boolian
int numberofduplicates=0;// keeps track of the number of duplicates found

for (a; a < size; a++)
{
    if (a == (size - 1))
        holdelements[a] = -1;
    else
        holdelements[a] = -2;
}

while (!flag)
{
    flag2 = 0;
    flag3 = 0;
    if (array1[b] == -1)
        flag = 1;
    else
    {
        while ((!flag) && (!flag2))
        {
            if (holdelements[c] == -1)
                flag2 = 1;
            else if (array1[b] == holdelements[c])
            {
                b++;
                c = 0;
                if (array1[b] == -1)
                    flag = 1;
            }
        }
        while (!flag3)
        {
            if (array1[d] == -1)
                flag3 = 1;
            else if (array1[b] == array1[d] && b != d)
            {
                printf("Duplicate of %d, index %d, was found at index %d.\n", array1[b], b, d);
                holdelements[e] = d;
                d++;
                e++;
                numberofduplicates++;
            }
        }
    }
    b++;
}
printf("Total Duplicates Found: %d\n", numberofduplicates);
return 0;
}
dl

重做以下内容:

const int size = sizeof(array1)/sizeof(int);

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在C / C ++中初始化大小未知的数组

来自分类Dev

在C / C ++中初始化大小未知的数组

来自分类Dev

如何初始化大小最初未知的数组?

来自分类Dev

在初始化的未知大小的数组中使用sizeof()-C ++

来自分类Dev

如何在C ++类中初始化大小可变的数组?

来自分类Dev

如何在C ++ 11标准中初始化动态大小的数组?

来自分类Dev

如何在C ++ 11标准中初始化动态大小的数组?

来自分类Dev

使用C中的宏初始化未知大小的2D数组

来自分类Dev

在C中声明和初始化未知大小的2D数组

来自分类Dev

如何在 C 编程中初始化多维数组

来自分类Dev

如何在lua中初始化表大小

来自分类Dev

如何在lua中初始化表大小

来自分类Dev

如何初始化模板大小的数组?

来自分类Dev

如何在结构中初始化此数组数组?

来自分类Dev

如何在BCPL中初始化数组的数组?

来自分类Dev

如何在结构中初始化此数组数组?

来自分类Dev

如何在不使用常量数组大小的情况下初始化私有类中的数组?

来自分类Dev

如何在Ruby中初始化类数组

来自分类Dev

如何在Kotlin中初始化注释的数组属性

来自分类Dev

如何在Verilog中初始化参数数组

来自分类Dev

如何在Android中初始化位图数组?

来自分类Dev

如何在Swift中声明和初始化数组

来自分类Dev

如何在JavaScript中初始化布尔数组

来自分类Dev

如何在结构中初始化静态数组

来自分类Dev

如何在Fortran中初始化大数组?

来自分类Dev

如何在结构中初始化数组

来自分类Dev

如何在Kotlin中初始化按钮数组

来自分类Dev

如何在MIPS组件中初始化庞大的数组?

来自分类Dev

如何在Verilog中初始化参数数组?

Related 相关文章

热门标签

归档