数组是一组以单个名称存储的相关数据项。
例如,int学生[30];// student是一个数组名称,其中包含30个带有单个变量名称的数据项集合
搜索-用于查找是否存在特定元素
排序-有助于按升序或降序排列数组中的元素。
遍历-顺序处理数组中的每个元素。
插入-这有助于将元素插入数组中。
删除-它有助于删除数组中的元素。
在数组中查找偶数的逻辑如下-
for(i = 0; i < size; i ++){ if(a[i] % 2 == 0){ even[Ecount] = a[i]; Ecount++; } }
在数组中查找奇数的逻辑如下-
for(i = 0; i < size; i ++){ if(a[i] % 2 != 0){ odd[Ocount] = a[i]; Ocount++; } }
要显示偶数,请按如下所述调用显示功能-
printf("no: of elements comes under even are = %d \n", Ecount); printf("The elements that are present in an even array is: "); void display(int a[], int size){ int i; for(i = 0; i < size; i++){ printf("%d \t ", a[i]); } printf("\n"); }
要显示奇数,请调用显示功能,如下所示:
printf("no: of elements comes under odd are = %d \n", Ocount); printf("The elements that are present in an odd array is : "); void display(int a[], int size){ int i; for(i = 0; i < size; i++){ printf("%d \t ", a[i]); } printf("\n"); }
以下是C语言程序通过使用for循环来分隔数组中的偶数和奇数-
#include<stdio.h> void display(int a[], int size); int main(){ int size, i, a[10], even[20], odd[20]; int Ecount = 0, Ocount = 0; printf("enter size of array :\n"); scanf("%d", &size); printf("enter array elements:\n"); for(i = 0; i < size; i++){ scanf("%d", &a[i]); } for(i = 0; i < size; i ++){ if(a[i] % 2 == 0){ even[Ecount] = a[i]; Ecount++; } else{ odd[Ocount] = a[i]; Ocount++; } } printf("no: of elements comes under even are = %d \n", Ecount); printf("The elements that are present in an even array is: "); display(even, Ecount); printf("no: of elements comes under odd are = %d \n", Ocount); printf("The elements that are present in an odd array is : "); display(odd, Ocount); return 0; } void display(int a[], int size){ int i; for(i = 0; i < size; i++){ printf("%d \t ", a[i]); } printf("\n"); }输出结果
执行以上程序后,将产生以下结果-
enter size of array: 5 enter array elements: 23 45 67 12 34 no: of elements comes under even are = 2 The elements that are present in an even array is: 12 34 no: of elements comes under odd are = 3 The elements that are present in an odd array is : 23 45 67