2015-10-20 65 views
0

我有与该是这样的练习很难阵列:搜索用C

写计数在每一个阵列不同数量的外观量的计划。整数0之间的数 - 9.取出此阵列为例:

int numbers[] = {1, 5, 4, 7, 1, 4, 1, 7, 5, 5, 3, 1}; 

创建指向该阵列的第一个元素的指针。循环访问数组并计算指向数字的出现数量。在计算出数字1的出现数量之后,指针将指向数字5.计算出现的数量5等等。在循环几次后,指针再次指向1(数组的元素4),程序可能不会再次计数1,因此您需要将得分保持在您已计算的数字的某个位置。

输出应看起来像这样:

的出场1的量:4

的出场3的量为:1

出场的量4是:2

5的出现数量是:3

的出场7次的量为:2

代码我现在有计算每一个元素的外观:

int main() 
{ 
    int getallen[] = {1, 5, 4, 7, 1, 4, 1, 7, 5, 5, 3, 1}; 
    int i , j, *aimedNumber, appearance = 0, arraySize = sizeof(getallen)/sizeof(int); 

    for(i=0;i<arraySize;i++)       
    { 
     aimedNumber = getallen[i];     // every loop, the pointer points to the next element 

     for(j=0; j<arraySize; j++)     // loops through the array comparing the pointer 
     { 
      if(getallen[j]==aimedNumber)   // if the element of the array == pointer 
      { 
       appearance++;      // +1 to the appearance 
      } 
     } 

     printf("the appearance of %i in the array is: %i\n", aimedNumber, appearance); 
     appearance = 0;        // after checking the appearance of the pointed number... 
    }            // reset the appearance variable 

    return 0; 
} 

但我仍然需要有东西,如果我是检查已经计算了一个数字,如果我确定了,请确保该数字不会再被计算在内。

在此先感谢!

+0

如果你可以使用一组,那么你的问题解决了,但它是stl和C++ – macroland

+1

你的外部循环应该运行所有可能的数字,例如从0到9,你的情况是'targeNumber'等于'i'。 (当然,如果你知道可能值的范围,你可以遍历内循环一次,并填充从0到9的每个数字的计数数组。) –

+0

你最近的问题是什么? – Roushan45

回答

0

你的程序看起来应该像下面这样:

#include <stdio.h> 

int main(void) 
{ 
    //Original array of numbers 
    int numbers[] = {1, 5, 4, 7, 1, 4, 1, 7, 5, 5, 3, 1}; 

    //array of 10 integers to count the number of occurences 
    int num_of_occ[10] = {0}; 

    int *ptr = numbers; 

    //tmp variable to hold the value of numbers array in the loop 
    int tmp ,n = 0 ; 

    while(n < 12) 
    { 
     tmp = *ptr; 
     num_of_occ[tmp]++; 
     ptr++; 
     n++; 
    } 

    //print the occurences 
    for(int n = 0 ; n < 10 ; n++) 
    { 
     if(num_of_occ[n] != 0) 
     { 
      printf("The ammount of appearences of %d : %d\n", n , num_of_occ[n]); 
     } 
    } 
} 
+0

是的!谢谢!这样你仍然可以使用指针(如assignement所说),并且你得到正确的输出! – arnofrederiks

+0

查看最后的edit.thanks。@ arnofrederiks –

+0

非常感谢@ machine_1!我对编辑进行了很好的审视,这真是太棒了! – arnofrederiks

0

由于您在计数后似乎不需要getallen阵列,因此您可以在计数时对其进行修改,即当您在阵列中查找所有1时,每次读取1时都可以将其设置为,让我们说,-1(你知道你的所有号码将在[0,9]范围内),这样,当你回到外循环,可以跳过与-1

编辑值的条目:这就是,如果你的答案必须遵循你的例子中描述的那种行为;否则带有额外阵列的解决方案,比如Joachim Pileborg的解决方案更好。

1

你有这样的限制,即数组中的数字只能在0到9之间(包括0和9),并且实际上使它简单得多,因为那么你可以有一个由10个整数组成的“counter”数组您搜索的数组中的数字),并且对于主数组中的每个数字,可以在计数器数组中增加相应的值(使用数字作为索引)。然后只需输出非零的计数器数组值。

像下面

int count[10] = { 0 }; // Initialize all to zero 

for (i = 0; i < arraySize; ++i) 
    ++count[getallen[i]]; 

// Now print out all non-zero values from count, 
// and the index is the number in getallen 
+0

开始非常感谢你,这种方式也可以工作,但是assignement说它必须使用指针。但是,无论如何,我也从中学到了很多! – arnofrederiks

0

您将需要另一个数组称为tempArr例如与getallen数组的大小,初始化它有10个(因为getallen数组中的数字在0-9之间),并在计算下一个数字之前,扫描tempArr并检查数字是否已经存在,如果是,则跳至下一个数字。

0

我只是把另一个循环设置aimedNumber之后,但在进入内循环之前检查阵列的前值对aimedNumber

skip = 0; 
for (j=0; j < i; j++) { 
    if (getallen[j] == aimedNumber) { 
    skip = 1; 
    break; 
    } 
} 
if (skip) { 
    continue; 
}