Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在这个C代码中需要修改什么才能得到所需的结果_C - Fatal编程技术网

在这个C代码中需要修改什么才能得到所需的结果

在这个C代码中需要修改什么才能得到所需的结果,c,C,我已经写了一小段代码,可以在一维数组上执行运行长度编码之类的东西,但仍然远远没有达到预期的效果 main() { int a[8]={2,0,0,0,3,0,0,9}; int i,temp,ct=0,flag,m; int found[90]={0}; for(i=0;i<=7;i++) { if(!a[i]) { ct++; if(!found[a[i]])

我已经写了一小段代码,可以在一维数组上执行运行长度编码之类的东西,但仍然远远没有达到预期的效果

main()
{
    int a[8]={2,0,0,0,3,0,0,9};
    int i,temp,ct=0,flag,m;
    int found[90]={0};
    for(i=0;i<=7;i++)
    {
        if(!a[i])
        {
            ct++;
            if(!found[a[i]])
            {
                flag=i;
                found[a[i]]=1;
            } 
        } 
    }

    a[flag]=ct;
    m=ct;    
    for(i=0;i<m;i++)
    {
        printf("%d",a[i]);
    }       
}/* end of main*/
但是我的代码让我

 2 5 0 0 3

对此我有什么建议吗?

运行长度编码不应该将2,0,0,3,0,0,9转换为2 1 0 3 1 2 0 9 1吗

1) 我看到的第一个错误是,您没有查看整个阵列。使用<可以在8之前停止,但也可以在7时停止,因此只能计算数组项0-6

2) 如果ct代表计数,则永远不会重置(仅在声明时ct=0)。它的赋值也是这样的:a[flag]=ct;这会覆盖原始数据。它基本上跟踪i的值

这是我刚刚编写的版本:

#define SZ 8

main()
{
    int a[SZ]={2,0,0,0,3,0,0,9};
    int i; //absolute position

    int runningCount = 1; //because we start at array index 1 and not zero

    for (i = 1; i <= SZ; i++) {
        if (a[i - 1] == a[i]) //value same as one before it...
           runningCount++;
        else { // new value found. print last one, and the count of the last one.
            printf("%d %d ", a[i - 1], runningCount);
            runningCount = 1; //reset for next loop
        }
    }

    return 0;
}

输出:
2 5 0 3 9

您需要a您似乎要做的是(a)计算0在数组中出现的次数,以及(b)用该计数替换第一次出现的0。目前还不清楚这是如何成为一种有用的编码

在任何情况下,您都不会得到期望的结果,至少部分是这样,因为您只修改了数组的一个元素。我怀疑您想要的,或者至少您认为您想要的,是在遇到数组中的非零元素时将它们向左移动


以您建议的方式压缩阵列的效用是什么?其他代码是否需要重建原始代码?如果需要,您希望如何从预期结果中重建原始代码?

121个问题,到目前为止,您还没有学会如何格式化代码?请让我知道如何格式化代码在每行前插入4个空格。您可以在记事本++中通过粘贴代码并选择all,hit tab,然后将粘贴复制到上面的SOSee中-@dsolimano已经为您完成了这项工作-请尝试并努力使您的代码格式正确,以备将来的问题-如果人们能够轻松地阅读您的代码,他们就更有可能回答您的问题。这不完全是运行长度编码,但我正在尝试将0的数量放在一个位置,可以吗请详细说明你是如何得到你想要的答案2 5 0 3 9的,因为我不知道如何到达那里?这2 5 0 3 9是你想要的。如果我们看到原始数组2 0 0 3 0 9,它有五(5)个0出现,需要放在一个地方,比如2 5 0 3 9,还需要删除额外的0,只有一个0应该在那里告诉这个元素是在重复中发生的。我现在明白了。直接从数组中得到2、3和9,5和0表示找到的0的总数。然后是零本身。你知道零出现在3的两边吗?你没有说每边有多少(1,2,3,4),所以你会用2 5 0 3 9回到原始数组吗?回到原始数组不是我关心的问题,我只是想用一种方式执行它。这是真的,但实际上并没有改变结果。这里还有更大的问题。老实说,我不知道这个编码方案的用途,它只是在一次采访中被问到的。这个问题的原始版本在二维数组中携带了这种编码的东西。
#define SZ 8

main()
{
    int a[SZ]={2,0,0,0,3,0,0,9};
    int i; //absolute position

    int runningCount = 1; //because we start at array index 1 and not zero

    for (i = 1; i <= SZ; i++) {
        if (a[i - 1] == a[i]) //value same as one before it...
           runningCount++;
        else { // new value found. print last one, and the count of the last one.
            printf("%d %d ", a[i - 1], runningCount);
            runningCount = 1; //reset for next loop
        }
    }

    return 0;
}
#define SZ 8

main()
{
    int a[SZ]={2,0,0,0,3,0,0,9};
    int i; //absolute position

    int zero_count = 0; //target zeros specifically...

    for (i = 0; i < SZ; i++) {
        if (a[i] == 0)
           zero_count++;
    }

    //now write it out in a bizarre, unparsable format again...

    for (i = 0; i < SZ; i++) {

        if (a[i] != 0)           //write out all non zero values
            printf("%d ", a[i]);

        if (i == 0) { //this says put the zero count after the first number was printed
           printf("%d 0 ", zero_count); //inserting it into a strange place in the array
        }

    }

    return 0;
}
for(i=0;i<=7;i++)
for(i=0;i< 7;i++)