我生成了一個包含 10 個整數的陣列。我必須找出一個元素重復了多少次,但我總是得到0一個輸出。在這段代碼中,countcolor變數是我的計數值。例如countcolor1是我的整數計數值1。這是我到目前為止所擁有的:
#include <stdio.h>
#include <time.h>
int i;
double entropy_calculator(int bucket[10]);
int main() {
srand(time(NULL));
int countings;
int bucket[10];
for (i = 0; i < 10; i) {
bucket[i] = 1 rand() % 10;
printf("%d \n", bucket[i]);
}
countings = entropy_calculator(bucket);
return 0;
}
double entropy_calculator(int bucket[10]) {
int x;
int countcolor1 = 0, countcolor2 = 0, countcolor3 = 0,
countcolor4 = 0, countcolor5 = 0, countcolor6 = 0;
for (x = 0; x <= 10; x) {
if (bucket[10] == 1)
countcolor1 ;
if (bucket[10] == 2)
countcolor2 ;
if (bucket[10] == 3)
countcolor3 ;
if (bucket[10] == 4)
countcolor4 ;
if (bucket[10] == 5)
countcolor5 ;
if (bucket[10] == 6)
countcolor6 ;
}
printf("%d,%d,%d,%d,%d,%d",
countcolor1, countcolor2, countcolor3,
countcolor4, countcolor5, countcolor6);
}
uj5u.com熱心網友回復:
代碼注釋
的回傳型別
entropy_calculator是double但你不回傳任何東西。如果您不希望回傳任何內容,請將回傳型別設定為void。在
main中,您嘗試將 的回傳值分配給entropy_calculator被int呼叫的countings。如果你要回傳一些double值,countings應該是一個double.未定義的行為。根據 C 標準,如果陣列下標超出范圍,程式的行為是未定義的。
bucket是一個由 10 個整陣列成的陣列。具有元素的陣列的有效索引N通常是0, 1, 2, ..., N - 1; 換句話說,第一個元素被分配了索引0,第二個元素被分配了索引1,...,N第一個元素被分配了索引N - 1。bucket因此,陣列中的有效索引是閉區間中的任何整數[0, 10 - 1] = [0, 9]。在您的entropy_calculator函式中,您正嘗試bucket使用 index訪問元素10;唯一有效的索引是 之一0, 1, ..., 9。在
entropy_calculator中,假設我們將所有 10 更改為 9,以便回圈從x = 0,x = 1, ... 運行。,并且某些inx = 9的表格檢查被替換為. 所有這六項檢查都只是檢查第 10 個元素是否是 1、2、... 或 6 之一。您忽略了其他 9 個隨機生成的數字,因此您永遠不會將它們包括在計數中。您還忽略了其他可能的隨機生成值,即 7、8、9 和 10,因為您當前僅將 10 的第 10 個元素與 1、2、...和 ??6 進行比較。([bucket[10] == j)j[1, 6]([bucket[9] == j)bucketbucketbucket
解決方案
我假設你的任務是
- 生成 10 個隨機整數
[1, 10]并將它們存盤在 s 的陣列中int,例如bucket. - 創建一個函式,該函式接受一個 10
ints 陣列(即指向 an 的指標int)作為引數,并列印陣列中1s,2s, ...,10s 的出現次數。
為了讓程式更通用一點,我們定義了宏MAX_LEN并讓它代表數字 10。
在main中,首先,我們通過將種子設定為當前時間來初始化亂數生成器。MAX_LEN int其次,我們定義了一個名為 s的陣列bucket。bucket第三,我們用偽隨機整數填充 中的每個元素[1, MAX_LEN]。最后,我們呼叫函式entropy_calculator,作為唯一的引數傳遞bucket,然后呼叫return 0。
在函式 中,我們定義了一個稱為 sentropy_calculator的陣列,每個元素都初始化為零。我們創建自己的內部映射,這樣 的第 元素表示在 中找到的 s的數量,對于每個in 。等效地,對于每個in ,在 bucket 中找到的 s 的數量由帶有 index的元素表示。然后我們回圈遍歷陣列的元素,并使用我們的映射,遞增陣列中的相應元素。然后我們列印出 的所有元素。MAX_LEN intcountsncountsnbucketn{1, 2, ..., MAX_LEN}n{1, 2, ..., MAX_LEN}ncountsn - 1bucketcountscounts
- 例如,對于我們程式中陣列的有效索引集合中的一些
i,即,{0, 1, ..., MAX_LEN - 1}如果我們發現bucket[i]是或者,更一般地說,。55countsncountsncounts[5 - 1]counts[bucket[i] - 1]
程式
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAX_LEN 10
void entropy_calculator(int bucket[]);
int main(void) {
/* initialize random number generator */
srand((unsigned) time(NULL));
/* will contain MAX_LEN random ints in [1, MAX_LEN] */
int bucket[MAX_LEN];
/* generate pseudo-random ints in [1, MAX_LEN], storing them in bucket */
for (int i = 0; i < MAX_LEN; i ) {
bucket[i] = 1 rand() % MAX_LEN;
printf("%d\n", bucket[i]);
}
entropy_calculator(bucket);
return 0;
}
/****************************************************************************
* entropy_calculator: given an array of MAX_LEN integers in [1, MAX_LEN], * *
* prints the number of occurrences of each integer in *
* [1, MAX_LEN] in the supplied array *
****************************************************************************/
void entropy_calculator(int bucket[]) {
int counts[MAX_LEN] = {0}; /* initialize counts to all 0s */
int i; /* loop variable */
for (i = 0; i < MAX_LEN; i )
counts[bucket[i] - 1] ;
/* printing all elements of counts */
for (i = 0; i < MAX_LEN; i ) {
if (i % 4 == 0) printf("\n");
printf(" -*: %d", i 1, counts[i]);
}
printf("\n");
}
示例會話
3
9
2
6
10
9
3
8
3
1
1*: 1 2*: 1 3*: 3 4*: 0
5*: 0 6*: 1 7*: 0 8*: 1
9*: 2 10*: 1
簡化版
如果任務是簡單地生成MAX_LEN(宏表示值10)隨機整數,[1, MAX_LEN]并計算生成了多少1s, 2s, ..., (MAX_LEN - 1), MAX_LENs,那么可以簡單地完成如下。
我們創建一個MAX_LEN整數陣列,稱為counts。與關聯的有效索引counts是0, 1, ..., MAX_LEN - 1。我們形成了自己的內部映射,使得countswith index的元素代表inn - 1隨機生成n的 s 的數量。n{1, 2, ..., MAX_LEN}
當我們在 中生成一個隨機整數時[1, MAX_LEN],我們將它分配給,并且我們用索引cur遞增 的元素,因為該元素表示該數字的出現次數。countscur - 1cur
程式
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAX_LEN 10
int main(void) {
srand((unsigned) time(NULL)); /* initialize random number generator */
int counts[MAX_LEN] = {0}; /* initialize counts to all 0s */
int i, cur;
for (i = 0; i < MAX_LEN; i ) {
cur = 1 rand() % MAX_LEN; /* pseudo-random int in [1, MAX_LEN] */
printf("%d\n", cur);
counts[cur - 1] ;
}
/* printing all elements of counts */
for (i = 0; i < MAX_LEN; i ) {
if (i % 4 == 0) printf("\n");
printf(" -*: %d", i 1, counts[i]);
}
printf("\n");
return 0;
}
示例會話
8
4
6
2
4
1
10
9
2
10
1*: 1 2*: 2 3*: 0 4*: 2
5*: 0 6*: 1 7*: 0 8*: 1
9*: 1 10*: 2
uj5u.com熱心網友回復:
#include<stdio.h>
#include<time.h>
int i;
double entropy_calculator(int bucket[10]);
int main()
{
srand(time(NULL));
int countings;
int bucket[10];
for(i=0; i<10; i)
{
bucket[i] = 1 rand() % 10;
printf("%d ", bucket[i]);
}
printf("\n");
countings = entropy_calculator(bucket);
return 0;
}
double entropy_calculator(int bucket[10])
{
int x;
int countcolor1=0, countcolor2=0, countcolor3=0, countcolor4=0, countcolor5=0, countcolor6=0;
for(x=0; x<10; x)
{
if (bucket[9]==1)
countcolor1 ;
if (bucket[9]==2)
countcolor2 ;
if (bucket[9]==3)
countcolor3 ;
if (bucket[9]==4)
countcolor4 ;
if (bucket[9]==5)
countcolor5 ;
if (bucket[9]==6)
countcolor6 ;
}
printf("%d,%d,%d,%d,%d,%d",countcolor1,countcolor2,countcolor3,countcolor4,countcolor5,countcolor6);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/464579.html
