您好,欢迎访问一九零五行业门户网

使用指针编写的C程序,用于查找用户输入的数组类型

问题编写一个 c 程序,通过指针查找我们需要检查的数组类型,数组中给定的元素是偶数、奇数还是两者的组合。
解决方案用户必须输入一个整数数组,然后显示该数组的类型。
示例1 − 输入:5 3 1,输出:奇数数组
示例 2 − 输入:2 4 6 8,输出:偶数数组
示例3 - 输入:1 2 3 4 5,输出:混合数组
算法参考下面给出的算法来查找用户输入的数组类型
第1步:运行时读取数组的大小。
第2步:输入数组元素。
第3步:声明指针变量。
第三步:使用指针变量检查数组的所有元素是否都是奇数。
然后,打印“odd”。
第四步:使用指针变量检查数组的所有元素是否为偶数。
然后,打印“even”。
第 5 步:否则,打印“mixed”。
>
示例以下是通过指针查找用户输入的数组类型的 c 程序 -
 现场演示
#include<stdio.h>#include<stdlib.h>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
"); scanf("%d",&n); printf("enter the elements of array
"); 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<n;i++){ scanf("%d",a+i);}}int findtype(int n, int *a){ int c=0,d=0; for(int i=0;i<n;i++){ if(a[i]%2==0){ c++; } else{ d++; }} if(c==n){ printf("the array type is even
"); } if(d==n){ printf("the array type is odd
"); } if(c!=n && d!=n){ printf("the array type is mixed
"); } return 0;}
输出执行上述程序时,会产生以下输出 -
enter the size of array4enter the elements of array12141618the array type is even
以上就是使用指针编写的c程序,用于查找用户输入的数组类型的详细内容。
其它类似信息

推荐信息