
编写一个 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</p><p>");
scanf("%d",&n);
printf("Enter the elements of array</p><p>");
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</p><p>");
}
if(d==n){
printf("The array type is Odd</p><p>");
}
if(c!=n && d!=n){
printf("The array type is Mixed</p><p>");
}
return 0;
}执行上述程序时,会产生以下输出 -
Enter the size of array 4 Enter the elements of array 12 14 16 18 The array type is Even
以上就是使用指针编写的C程序,用于查找用户输入的数组类型的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号