[英]Printing a 2D Array in C
我正在嘗試創建一個 20 人的群體,這些人的名字(或氏族)是從池中隨機生成的。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
srand(time(0));
// dice KOPYA = int dice = rand() % 101;
int pop = 20; //Population Number
int genCount = 5; //Number of gens for 1 person in population
char a[] = "a";
char gens[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ";
char target[] = "NuriY";
char population[pop][genCount];
char nextPopulation[pop][genCount];
for(int i=0;i<pop;i++)
{
for(int j=0;j<=genCount-1;j++)
{
//int dice = rand() % 54;
//population[i][j] = gens[dice];
population[i][j] = a[0];
}
printf("%2d. : %s\n",i+1,population[i]);
}
return 0;
}
但我的結果是我不擅長二維數組。 你能解釋一下我的錯是什么嗎?
我不能嘗試任何事情。
像我一樣用 Null 終止字符串。 https://godbolt.org/z/njqf9s3eG
int main()
{
srand(time(0));
// dice KOPYA = int dice = rand() % 101;
int pop = 20; //Population Number
int genCount = 5; //Number of gens for 1 person in population
char a[] = "a";
char gens[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ";
char target[] = "NuriY";
char population[pop][genCount];
char nextPopulation[pop][genCount];
for(int i=0;i<pop;i++)
{
for(int j=0;j<genCount - 1;j++)
{
int dice = rand() % 54;
population[i][j] = gens[dice];
}
population[i][genCount - 1] = 0;
printf("%2d. : %s\n",i,population[i]);
}
return 0;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.