2017-05-25 27 views
0

我有一些女傭這樣的事情。該程序需要用戶輸入5個單詞,然後將其長度打印爲「直方圖」。問題是,我該怎麼做。爲了使長話短說,如何編寫一個循環,轉變爲int從word_length陣列的每一個細胞需要,並打印正是很多「#」標誌:P如何從數組中取出循環參數並將它們更改爲打印的標記

char word_input[20]; 
int counter = 0; 
int word_lenght[5]; 

while(counter < 5) 
{ 
    printf("Please, write a word of your choice: "); 
    scanf("%20s", word_input); 
    word_lenght[counter] = strlen(word_input); 
    counter++; 

} 
printf("\n"); 
printf("Your histogram: \n"); 
for(int i = 0; i < counter; i++) 
{ 
    printf("%d.%d\n",i,word_lenght[i]); 
} 

另外,我想在這裏問,沒不想創建另一個帖子。我正在做普拉塔的書(也是在把每一個練習放在那裏學習git)。從C開始足夠了嗎?我看到了這個:https://github.com/open-source-society/computer-science#core-applications,我想通過,至少在編程的一切:P

+0

'%20s' - >'%19s' –

回答

1

我向你提出一個想法,肯定不是最好的,但它是你開始考慮它。

#define SIZE_ARRAY 5 

int main(void) 
{ 
    int word_length[SIZE_ARRAY] = {4, 2, 6, 3, 7}; 
    int max_value = 0; 

    for (int i = 0 ; i < SIZE_ARRAY ; ++i) 
    { 
     if (word_length[i] > max_value) 
      max_value = word_length[i]; 
    } 

    for (; max_value > 0 ; max_value--) 
    { 
     for (int j = 0 ; j < SIZE_ARRAY ; j++) 
     { 
      if (word_length[j] >= max_value) 
       putchar('#'); 
      else 
       putchar(' '); 
     } 
     putchar('\n'); 
    } 

    return (0); 
} 
相關問題