Ex03-1차원 배열 요소 활용

CODEDRAGON Development/C, C++

반응형

   

1차원 배열 요소 활용

배열 선언과 동시에 데이터를 초기화하고 배열 요소를 이용하여 총점과 평균 구하기

   

메모리 구조

연산 전

연산 후

  

  

   

   

소스코드

  1. #include <stdio.h>
  2.  
  3. int main(void) {
  4.     int array[3] = {85,99,76};    //배열 선언과 동시에 초기화
  5.     int total=0;
  6.  
  7.     total = array[0]+array[1]+array[2];
  8.     printf("array[0]=%d, array[1]=%d, array[2]=%d \n", array[0], array[1], array[2]);
  9.     printf("총점: %d \n" , total);
  10.  
  11.     //%.2lf: 소수점이하 2째짜리까지 lf 출력
  12.     printf("평균: %.2lf \n", (double)total/3);
  13.  
  14.     return 0;
  15. }


 

 

   

출력결과

 

반응형