如何用C语言将值附加到数组

时间:2018-12-02 14:27:07

标签: c arrays

给定一组10位数字,我需要查找索引在给定数字中出现的次数,例如 给定const acronymEnum = { MT: 'Mountain', AMER: 'American', PL: 'Place', UDC: 'Univeristy of the District of Columbia', AU: 'American University', AVE: 'Avenue', CUA: 'Catholic University of America', NOMA: 'North of Massechusets Avenue', GMU: 'George Mason University', VT: 'Virginia Tech', UVA: 'University of Virginia', RAEGAN: 'DCA', ST: 'Street', SW: 'South West', SQ: 'Square', PENN: 'Pennsylvania', }; function convertStationAcronym(stationName) { return Object.keys(acronymEnum).reduce((previous, current) => { if (stationName.toLowerCase().includes(current.toLowerCase())) { console.log('trigger the match', current) return stationName.concat(` ${acronymEnum[current]}`).toLowerCase(); } else { return previous.toLowerCase(); } }, ''); } console.log(convertStationAcronym('U Street African Amer'))作为数组元素,数组索引从0到9,所以我有类似的东西

2 2 0 0 0 3 0 0 1 0

第一个索引0出现两次,第二个索引1出现两次,第三个索引0出现0次,因此它被忽略,依此类推,所以结果是0 1 2 3 4 5 6 7 8 9 2 2 0 0 0 3 0 0 1 0 ---> input 00115558 -----> output ,现在我需要重新排列输出,使它为0一定不能开始,所以我需要像00115558这样的东西 我能够获得像10015558这样的初始输出,但是我需要将其保存到数组中,以便可以遍历数组并检查大于0的值并交换位置。

00115558

我试图将输出保存到另一个数组中,并在循环外打印该数组以查看内容,但结果不是我想要的。我如何将我的值保存到output []数组中。谢谢你的帮助

1 个答案:

答案 0 :(得分:0)

#include<stdio.h>
#include<conio.h>
int main() 
{
         int i,j,len;
         int array[10],output[10];
         printf("Enter 10 digit number separated by space: \n");
         for (i = 0; i < 10; i++)
         scanf("%d", &array[i]);
         len = sizeof(array)/sizeof(array[0]);
         for(i=0; i<len; i++)
    {
        if(array[i] != 0)
        {

        for(j=0; j<array[i]; j++)
           //output[j] = i;
           printf("%d ", i);
        }

}
}

先生,我在“ Dev c ++”中尝试了上述确切的代码。我得到的答案是00115558。我认为您的守则是绝对正确的。更改您的IDE软件或进行更新。