Test/C

배열의 점수에 따른 별 그래프 출력

kiostory 2019. 5. 23. 17:33

배열로 점수를 입력받아 배열값에 따라 별그래프 작성.

별은 5점당 하나. 남는점수는 버림.

배열은 main에 선언, 그래프는 별도로 작성


#include <stdio.h>

void print_graph(int *ps, int size);

int main(void)
{
 int score[5]={72,88,95,64,100};
 int size=sizeof(score)/sizeof(int);

 for(int i=0;i<size;i++){
  print_graph(score+i, size);                     // 배열 요소의 주소를 전달하므로 배열이름이 주소, 형식만큼 더하면 한칸씩 이동
 }
 return 0;
}


void print_graph(int *ps, int size){
 int count=0;
 count=*ps/5;
 printf("(%3d) ",*ps);
 for(;count!=0;count--){
  printf("*");
 }
 printf("\n");
}
--------------------------------------------

( 72) **************
( 88) *****************
( 95) *******************
( 64) ************
(100) ********************