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
热门推荐
10 儿子立冬祝福语简短独特
11 对当兵的祝福语简短
12 侄儿高考试祝福语简短
13 伴郎红包祝福语朋友简短
14 媳妇生日简短祝福语朋友
15 公司年会祝福语简短最好
16 元旦感恩祝福语简短大全
17 红包祝福语简短10字
18 周六早晨祝福语简短