배열과 문자열
본 자료는 2페이지 의 미리보기를 제공합니다. 이미지를 클릭하여 주세요.
닫기
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
해당 자료는 2페이지 까지만 미리보기를 제공합니다.
2페이지 이후부터 다운로드 후 확인할 수 있습니다.

목차

5.1 1차원 배열의 선언

5.2 문자열의 사용

5.3 다차원 배열

5.4 배열의 초기화

5.5 문자열 배열

본문내용

6, 32, 64, 128 };
/* int pwr[8] = { ... }; 와 같은 효과 */
char prompt[] = "Enter your name: ";
** 2차원 배열의 경우, 열의 수는 반드시 주어져야 한다.
int sqr[][3] = {
1, 2, 3,
4, 5, 6,
7, 8, 9
};
** 배열의 초기값은 프로그램의 실행도중에 변경될 수 있다.
예제. CPU와 평균 속도 대응
#include "stdio.h"
long cpu[5][2] = {
8088, 4,
8086, 4,
80286, 10,
80386, 20,
80486, 40
};
main()
{
long processor;
int i;
printf("Enter the number of the processor: ");
scanf("%ld", &processor); /* long int */
for (i=0; i<5; i++)
if (processor == cpu[i][0]); {
printf("Average speed is %d MHz.\n", cpu[i][1]);
break;
}
if (i==5)
printf("Processor not found.\n");
}
배열에 초기값이 주어져도 그 내용은 바뀔 수 있다.
예제. #include "stdio.h"
#include "string.h"
main()
{
char str[80] = "I like C";
strcpy(str, "hello");
printf("%s\n", str);
}
5.5 문자열 배열
문자열 배열은 문자열들을 배열에 저장하는 것으로 2차원 배열을 사용한다.
예. char names[10][40]; 길이가 최대 40인 문자열들을 10개 갖는 문자열 배열
gets(names[2]); names 배열의 세번째 문자열
printf("%s\n", names[0]); 첫번째 문자열을 출력
예. #include "stdio.h"
main()
{
char text[10][80];
int i;
for (i=0; i<10; i++) {
printf("%d: ", i+1);
gets(text[i]);
}
do {
printf("Enter number of string (1-10): ");
scanf("%d", &i);
i--; /* i의 값을 배열의 색인으로 사용하기 위해 조정한다. */
if (i>=0 && i<=9)
printf("%s\n", text[i]);
} while(i>=0);
}
예제. 영어에서 독어로 번역
#include "stdio.h"
#include "string.h"
char words[][2][40] = {
"dog", "Hund",
"no", "nein",
"year", "Jahr",
"child", "Kind",
"I", "Ich",
"drive", "fahren",
"house", "Haus",
"to", "zu",
"", ""
};
main()
{
char english[80];
int i;
printf("Enter English word: ");
gets(english);
i = 0;
while (strcmp(words[i][0], "")) {
if (!strcmp(english, words[i][0])) {
printf("German translation: %s\n", words[i][1]);
break;
}
i++;
} /* while */
if (!strcmp(words[i][0], ""))
printf("Not in dictionary.\n");
}
프로그래밍 연습
5.1절 연습문제 2
5.3절 연습문제 1
5.4절 연습문제 3
5.5절 연습문제 1
종합문제 1-3
종합문제 2-2, 3, 5

키워드

  • 가격1,300
  • 페이지수8페이지
  • 등록일2003.02.10
  • 저작시기2003.02
  • 파일형식한글(hwp)
  • 자료번호#221066
본 자료는 최근 2주간 다운받은 회원이 없습니다.
청소해
다운로드 장바구니