English 中文(简体)
在合并2个阵列时随机获得数量
原标题:get a random number when merging 2 array
  • 时间:2012-04-08 02:32:08
  •  标签:
  • c

我想把2个阵列合并为1个。 例如:

  • A1= 1,1
  • A2= 2,2
  • then A3 = 1,2,1,2

例如:

  • A1= 1
  • A2= 2,2,2,2
  • then A3 = 1,2,2,2,2

例如:

  • A1= 1,1,1,1
  • A2= 2,2
  • then A3 = 1,2,1,2,1,1

最后,当我掌握我的法典时,我有1,2,1,1,1,20。

最后,我有1,2,32767,2,2。

因此,我猜测我有错误的法典。 在我结束发言之后,我完成了缩短阵列的内容,用谁来填补A3的其余部分。 但是,我怎么说——你们能否帮助我?

法典:

int *p3=arr3;   //arr3 is A3 for example, arr1 = A1..etc, all sizes are defined
int index;
int index1=0;
int index2=0;

for(index = 0; index< sizeofArr3 ; index++)
{
    if(index%2==0)
    {
        if(index1<=sizeofArr1)
            *(p3++) = arr1[index1++];
        else
            *(p3++) = arr2[index2++];
    }
    else 
    {
        if(index2<=sizeofArr2)
            *(p3++) = arr2[index2++];
        else
            *(p3++) = arr1[index1++];
    }
}
最佳回答

这行文如下:

if (index1 <= sizeofArr1)

页: 1 您应使用<代码><而不是<=

其原因与C的零基阵列有关。 元素指数为<0>0至N-1。 由于您重新允许其进入<代码>N(N+1th 部分内容),你实际上重新援引未经界定的行为。

从理论上讲,执行可以在这方面做任何事情,直至包括破坏宇宙。 我猜想你,我刚才决定给你一些稍微令人不安的结果:

问题回答

• 大小OfArr1和2; 你们如何计算其规模?

lo体内的测试应是:

if (index1 < sizeofArr1)

<代码><而不是<=,假设sizeofArr1是阵列中元件数,而不是阵列中最高有效指数。 当阵列长度相同时,这种差异确实如此(因此第一个序列是K),但如果阵列长度不同,则确实如此。





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签