C 程序使用指针查找用户输入的数组类型。
问题
编写一个C程序来使用指针查找我们需要检查的数组类型,数组中的给定元素是偶数还是奇数或两者的组合。
解决方案
用户必须输入一个整数数组,然后显示数组的类型。
示例1-输入:531,输出:奇数数组
示例2-输入:2468,输出:偶数数组
示例3-输入:12345,输出:混合数组
算法
参考下面给出的算法来查找用户在指针的帮助下输入的数组类型。
步骤1:在运行时读取数组的大小。
第二步:输入数组元素。
第三步:声明指针变量。
步骤3:使用指针变量检查数组的所有元素是否都是奇数。
然后,打印“奇数”。
步骤4:使用指针变量检查数组的所有元素是否为偶数。
然后,打印“偶数”。
第5步:否则,打印“Mixed”。
示例
以下是在指针的帮助下查找用户输入的数组类型的C程序-
#include#include int*createArray (int); void readArray(int,int *); int findType(int , int *); int main(){ int *a,n,c=0,d=0; printf("Enter the size of array\n"); scanf("%d",&n); printf("Enter the elements of array\n"); createArray(n); readArray(n,a); findType(n,a); return 0; } int *createArray(int n){ int *a; a=(int*)malloc(n*sizeof(int)); return a; } void readArray(int n,int *a){ for(int i=0;i 输出结果 执行上述程序时,它会产生以下输出-
Enter the size of array 4 Enter the elements of array 12 14 16 18 The array type is Even