开发者

increment the contents of array when match is found

main()
{

   int  i,j,mul_count[16]={0},mul;

   int *ptr;

   ptr =&mul_count;

   for(i=1;i<=4;i++)    
   {
         for(j=1;j<=4;j++)

         mul = j*i;

         ptr = (ptr+(mul*4));

         mul_count[mul] = ++*ptr;
   }

   for(i=1;i<=16;i++)    
      printf("mul_count[%d]=%d\n",i,mul_count[i]);

}

can any one correct this code. The code is i generate multiple for all possible combination from 1 to 4,and increment the array contents for that particular value, i.e when i do 2*2 = 4 and 4*1=4 , then mul_count[4] should be set a 2 and so on, when i encounter 4 again in the multiples it should be incremented to 3 and so on.this has to be done for all generated multiples that corresponding array value开发者_开发知识库 should be incremented


I don't see any point in your usage of the ptr variable.

Also your code misses the required brackes { and } for the inner loop. You can get away much easier with something like this instead:

for(i=1;i<=4;i++)
{
    for(j=1;j<=4;j++) {
        mul = j*i;
        mul_count[mul]++;
    }
}


  1. You have forgot braces for your second loop.
  2. ptr is always incremented, so you are going out of your array. Anyway this pointer is really not need.

Try this:

for(j=1; j<=4; ++j)
{
    mul = j*i - 1;
    mul_count[mul]++;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜