Home > Backend Development > C++ > body text

C program written using pointers to find the type of an array entered by the user

WBOY
Release: 2023-08-31 08:57:19
forward
902 people have browsed it

C program written using pointers to find the type of an array entered by the user

Question

Write a C program to find the array type by pointer we need to check whether a given element in the array is even, odd or both The combination.

Solution

The user must enter an array of integers and then the type of the array is displayed.

Example 1 − Input: 5 3 1, output: odd array

Example 2 − Input: 2 4 6 8, output: even number Array

Example 3 - Input: 1 2 3 4 5, Output: Mixed Array

Algorithm

Refer to the algorithm given below to find users Input array type

Step 1: Read the size of the array at runtime.

Step 2: Enter the array elements.

Step 3: Declare pointer variables.

Step 3: Use pointer variables to check whether all elements of the array are odd numbers.

Then, print "Odd".

Step 4: Use pointer variables to check whether all elements of the array are even numbers.

Then, print "Even".

Step 5: Otherwise, print "Mixed".

>

Example

The following is a C program to find the array type entered by the user through a pointer-

Live demonstration

#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;
}
Copy after login

Output

When executing the above program, the following output will be produced-

Enter the size of array
4
Enter the elements of array
12
14
16
18
The array type is Even
Copy after login

The above is the detailed content of C program written using pointers to find the type of an array entered by the user. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template