1 9 4 includestdio h void main int

  • Slides: 51
Download presentation

1차원 배열정리 9 -4] 배열에 초기값을 대입하고 합계를 구하는 예제 수정 #include<stdio. h> void

1차원 배열정리 9 -4] 배열에 초기값을 대입하고 합계를 구하는 예제 수정 #include<stdio. h> void main() { int m[5]; int a, sum=0; for(a=0; a<5; a++) { printf("input number: "); scanf("%d", &m[a]); //scanf()으로 배열원소 값을 초기화 printf("m[%d] = %dn", a, m[a]); sum = sum + m[a]; } printf("sum = %dn", sum); }

예제 9 -6] 10개의 정수형 자료에 10을 곱한 후 합계와 평 균을 구하라. #include

예제 9 -6] 10개의 정수형 자료에 10을 곱한 후 합계와 평 균을 구하라. #include <stdio. h> main( ) { int table[10] = {24, 35, 43, 25, 36, 56, 46, 37, 48, 29}; int total = 0; float average; int index ; for (index = 0; index < 10; index++) table[index] = table[index] * 10; //배열 값에 10을 곱한다 for (index = 0; index <= 10; index++) total = total + table[index]; { //배열 값을 더한다 printf(“table[%d] = %dn", index, table[index]); } printf(" 전체 합계 : %dn", total); average = (float) total / 10; printf(" 평 균 : %3. 2 fn", average); printf(" 배열 table[0]의 번지 ==> %dn", &table[0]); //배열의 첫번째 원소의 시작주소 printf(" 배열 table의 시작 번지 ==> %dn", table); } //배열명의 시작주소

예제 9 -6]의 실행결과 table[0] table[1] table[2] table[3] table[4] table[5] table[6] table[7] table[8] table[9]

예제 9 -6]의 실행결과 table[0] table[1] table[2] table[3] table[4] table[5] table[6] table[7] table[8] table[9] = = = = = 24 35 43 25 36 56 46 37 48 29 *10 *10 *10

신장의 최대값 구하는 배열 프로그램 #include<stdio. h> //배열로 수정하세요 void main() { //배열명 input[]

신장의 최대값 구하는 배열 프로그램 #include<stdio. h> //배열로 수정하세요 void main() { //배열명 input[] int input[5] , i , max=0; for(i=0; i<5; i++) { scanf("%d", &input[i]); if( max < input[i]) max = input[i] ; printf("두 값 %d : %d 비교해서 n", max, input[i]); } printf(" 최대 신장은 =%dcm입니다 n", max); }

3. 1 2차원 배열 예] int unit [3][4]; 1열 1행 행 * 열 =

3. 1 2차원 배열 예] int unit [3][4]; 1열 1행 행 * 열 = 기억장소 크기 2열 3열 4열 unit [0][0] unit [0][1] unit [0][2] unit [0][3] 2행 unit [1][0] unit [1][1] unit [1][2] unit [1][3] 3행 unit [2][0] unit [2][1] unit [2][2] unit [2][3]

3. 1 2차원 배열 컴퓨터 기억형태 배열명: unit[0] 배열명: unit[1] 배열명: unit[2] unit [0][0]

3. 1 2차원 배열 컴퓨터 기억형태 배열명: unit[0] 배열명: unit[1] 배열명: unit[2] unit [0][0] [0][1] [0][2] [0][3] 첫번째 주소값: 0000 unit unit [1][0] [1][1] [1][2] [1][3] [2][0] [2][1] [2][2] [2][3] 0012 + 4 = 0016 0000 + 4 = 0004 + 4 = 0008 + 4 = 0012 0016 + 4 = 0020 + 4 = 0024 + 4 = 0030 + 4 = 0034 + 4 = 0038 + 4 = 0042 + 4 = 0046

2차원배열예제 3] 학생 세명의 총점과 합계 #include <stdio. h> main(void) { int i, j,

2차원배열예제 3] 학생 세명의 총점과 합계 #include <stdio. h> main(void) { int i, j, sum=0; int a[3][3] = {{67, 89, 78}, {90, 90}, {90, 98}}; for( i=0; i<3; i++) { for( j=0; j<3; j++) { sum = sum + a[i][j]; } } } printf("행의 전체 합계 = %d n", sum);

2차원배열 행 합계 실습평가1 #include <stdio. h> main(void) { int day[3][5 = {{12, 56,

2차원배열 행 합계 실습평가1 #include <stdio. h> main(void) { int day[3][5 = {{12, 56, 32, 15, 88}, {99, 66, 77, 88, 60}, {65, 5, 18, 22, 0}}; int i, j, sum=0; // for 문장으로 행과 열 반복 for(i=0; i<3; i++ ){ for(j=0; j<5; j++){ printf(" day[%d]= %dn" , i, j, day[i][j]); sum = sum + day[i][j]; } printf("%d 행의 합계 = %dn", i+1, sum); sum = 0; printf("n"); } }

2차원배열 행 합계 실습평가2 #include <stdio. h> main(void) { int day[3][5] ={{12, 56, 32,

2차원배열 행 합계 실습평가2 #include <stdio. h> main(void) { int day[3][5] ={{12, 56, 32, 15, 88}, {99, 66, 77, 88, 60}, {65, 5, 18, 22, 0}}; int i, j, sum=0; // for 문장으로 행과 열 반복 for(i=0; i<3; i++ ){ for(j=0; j<5; j++){ printf(" day[%d]= %dn" , i, j, day[i][j]); sum = sum + day[i][j]; } //printf("%d 행의 합계 = %dn", i+1, sum); } printf("sum = %d n", sum); }

예제 9 -14] 10명의 3과목 성적을 초기화하여 입력 후 과목별 평균을 구하는 프로그램 #include

예제 9 -14] 10명의 3과목 성적을 초기화하여 입력 후 과목별 평균을 구하는 프로그램 #include <stdio. h> #define MAN 10 // 학생수 #define COURSE 3 // 과목수 main( ) { int score[MAN][COURSE] = {{100, 80, 70}, {30, 90, 54}, {76, 82, 69}, {56, 88}, {38, 40, 66}, {66, 72, 78}, {72, 78, 90}, {92, 94, 100}, {80, 84}, {86, 88, 94}}; int total[COURSE] = {0, }; float average[COURSE]; int row, column , count = 0; for (row = 0; row < MAN; row++) for (column = 0; column < COURSE; column++) total[column] += score[row][column]; while (count < COURSE) { average[count] = total[count] / (float) MAN; printf(“<%d번 과목>합계=%d 평균= %5. 2 f n", count + 1, total[count], average[count]); count++; } } 2022 -01 -12 종이위에 쓰는 C 프로그래밍 43

문자열 배열의 초기화 #include<stdio. h> #include<string. h> //strcpy() 사용하여 배열 초기화 void main() {

문자열 배열의 초기화 #include<stdio. h> #include<string. h> //strcpy() 사용하여 배열 초기화 void main() { char b[10] ; strcpy(b, "chosun"); //”chosun”을 배열 b로 복사 b[7]='u'; b[8]='n'; b[9]='i'; //나머지 uni는 개별원소 대입 printf("%s %c%c%cn“, b, b[7], b[8], b[9]); printf("%d %dn", strlen(b), sizeof(b)); }

문자열 배열의 초기화 #include<stdio. h> #include<string. h> //gets() 함수를 이용한 초기화 void main() {

문자열 배열의 초기화 #include<stdio. h> #include<string. h> //gets() 함수를 이용한 초기화 void main() { char b[10] ; gets(b); b[7]='u'; b[8]='n'; b[9]='i'; printf("%s %c%c%cn", b, b[7], b[8], b[9]); // printf("%sn", b); printf("%d %dn", strlen(b), sizeof(b)); }

홀수 합 • #include <stdio. h> • • main(void) { • int day[10] ={1,

홀수 합 • #include <stdio. h> • • main(void) { • int day[10] ={1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; • int i, sum=0; • // for 문장으로 행과 열 반복 • for(i=0; i<10; i++){ • if((day[i]) % 2 == 1) • // sum = sum + day[i]; } • sum = sum + day[i]; • printf(" day[%d] = %dn" , i, day[i]); } • printf("sum = %dn", sum); } •