배열은 단일 이름으로 저장되는 관련 데이터 항목의 그룹입니다.
예를 들어, int 학생[30]; //student는 단일 변수 이름을 가진 30개의 데이터 항목 컬렉션을 보유하는 배열 이름입니다.
배열의 연산
-
검색 − 특정 요소가 존재하는지 여부를 찾는 데 사용됩니다.
-
정렬 − 배열의 요소를 오름차순 또는 내림차순으로 정렬하는 데 도움이 됩니다.
-
횡단 − 배열의 모든 요소를 순차적으로 처리합니다.
-
삽입 − 배열에 요소를 삽입하는 데 도움이 됩니다.
-
삭제 − 배열의 요소를 삭제하는 데 도움이 됩니다.
배열에서 모든 산술 연산을 수행하는 논리는 다음과 같습니다. -
for(i = 0; i < size; i ++){
add [i]= A[i] + B[i];
sub [i]= A[i] - B[i];
mul [i]= A[i] * B[i];
div [i] = A[i] / B[i];
mod [i] = A[i] % B[i];
} 프로그램
다음은 배열에 대한 산술 연산을 위한 C 프로그램입니다 -
#include<stdio.h>
int main(){
int size, i, A[50], B[50];
int add[10], sub[10], mul[10], mod[10];
float div[10];
printf("enter array size:\n");
scanf("%d", &size);
printf("enter elements of 1st array:\n");
for(i = 0; i < size; i++){
scanf("%d", &A[i]);
}
printf("enter the elements of 2nd array:\n");
for(i = 0; i < size; i ++){
scanf("%d", &B[i]);
}
for(i = 0; i < size; i ++){
add [i]= A[i] + B[i];
sub [i]= A[i] - B[i];
mul [i]= A[i] * B[i];
div [i] = A[i] / B[i];
mod [i] = A[i] % B[i];
}
printf("\n add\t sub\t Mul\t Div\t Mod\n");
printf("------------------------------------\n");
for(i = 0; i <size; i++){
printf("\n%d\t ", add[i]);
printf("%d \t ", sub[i]);
printf("%d \t ", mul[i]);
printf("%.2f\t ", div[i]);
printf("%d \t ", mod[i]);
}
return 0;
} 출력
위의 프로그램이 실행되면 다음과 같은 결과가 생성됩니다 -
Run 1: enter array size: 2 enter elements of 1st array: 23 45 enter the elements of 2nd array: 67 89 add sub Mul Div Mod ------------------------------------ 90 -44 1541 0.00 23 134 -44 4005 0.00 45 Run 2: enter array size: 4 enter elements of 1st array: 89 23 12 56 enter the elements of 2nd array: 2 4 7 8 add sub Mul Div Mod ------------------------------------ 91 87 178 44.00 1 27 19 92 5.00 3 19 5 84 1.00 5 64 48 448 7.00 0