我有与该是这样的练习很难阵列:搜索用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;
}
但我仍然需要有东西,如果我是检查已经计算了一个数字,如果我确定了,请确保该数字不会再被计算在内。
在此先感谢!
如果你可以使用一组,那么你的问题解决了,但它是stl和C++ – macroland
你的外部循环应该运行所有可能的数字,例如从0到9,你的情况是'targeNumber'等于'i'。 (当然,如果你知道可能值的范围,你可以遍历内循环一次,并填充从0到9的每个数字的计数数组。) –
你最近的问题是什么? – Roushan45