问题
编写一个 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#includeint*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
"); } 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程序,用于查找用户输入的数组类型的详细内容,更多请关注【创想鸟】其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。
发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2584740.html