Table of Contents
Question
Solution
Algorithm
Example
Output
Home Backend Development C++ C program written using pointers to find the type of an array entered by the user

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

Aug 31, 2023 am 08:57 AM
array type c program pointer

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How do generic functions handle pointers and reference types in Golang? How do generic functions handle pointers and reference types in Golang? Apr 16, 2024 pm 04:06 PM

When a generic function handles pointer types in Go, it will receive a reference to the original variable, allowing the variable value to be modified. Reference types are copied when passed, making the function unable to modify the original variable value. Practical examples include using generic functions to compare strings or slices of numbers.

How to enable or disable enhanced pointer precision on Windows 11 How to enable or disable enhanced pointer precision on Windows 11 Sep 27, 2023 pm 12:21 PM

Pointer precision is crucial in situations where higher precision and better cursor positioning are required. It is enabled by default in Windows 11, but you may need to reconfigure enhanced pointer precision for better performance. For example, you might not want Windows to automatically re-adjust the pointer speed, but instead cover a fixed distance when making similar mouse movements. What is enhanced pointer precision? Enhanced pointer precision adjusts how far the cursor moves based on how fast the mouse is moving. Therefore, the faster the mouse moves, the greater the distance covered. For those wondering what Windows Enhanced Pointer Precision does, it changes mouse sensitivity. How to turn enhanced pointer precision on or off in Windows 11? 1. Press through Settings

C++ program to find the value of the inverse hyperbolic sine function taking a given value as argument C++ program to find the value of the inverse hyperbolic sine function taking a given value as argument Sep 17, 2023 am 10:49 AM

Hyperbolic functions are defined using hyperbolas instead of circles and are equivalent to ordinary trigonometric functions. It returns the ratio parameter in the hyperbolic sine function from the supplied angle in radians. But do the opposite, or in other words. If we want to calculate an angle from a hyperbolic sine, we need an inverse hyperbolic trigonometric operation like the hyperbolic inverse sine operation. This course will demonstrate how to use the hyperbolic inverse sine (asinh) function in C++ to calculate angles using the hyperbolic sine value in radians. The hyperbolic arcsine operation follows the following formula -$$\mathrm{sinh^{-1}x\:=\:In(x\:+\:\sqrt{x^2\:+\:1})}, Where\:In\:is\:natural logarithm\:(log_e\:k)

C program uses rename() function to change file name C program uses rename() function to change file name Sep 21, 2023 pm 10:01 PM

The rename function changes a file or directory from its old name to its new name. This operation is similar to the move operation. So we can also use this rename function to move files. This function exists in the stdio.h library header file. The syntax of the rename function is as follows: intrename(constchar*oldname,constchar*newname); The function of the rename() function accepts two parameters. One is oldname and the other is newname. Both parameters are pointers to constant characters that define the old and new names of the file. Returns zero if the file was renamed successfully; otherwise, returns a nonzero integer. During a rename operation

Advanced Golang pointer type methods to improve programming skills Advanced Golang pointer type methods to improve programming skills Apr 07, 2024 pm 06:42 PM

The pointer type approach is available in Go language, which allows you to define a function of pointer type in order to modify the value pointed to without explicitly passing the pointer in the method signature. This provides code simplicity and efficiency since copy-by-value passes do not need to be copied. The syntax of pointer type method is: typeTypeName*Type\nfunc(t*TypeName)MethodName(). To use pointer type methods, you create a pointer to an instance of the type and then use that pointer to call the method. The benefits of pointer type methods include code simplicity, efficiency, and modifiability. It should be noted that the pointer type method can only be used for pointer types, and you need to be careful when using it, because the structure value pointed to may be accidentally

How to use C++ reference and pointer parameter passing? How to use C++ reference and pointer parameter passing? Apr 12, 2024 pm 10:21 PM

References and pointers in C++ are both methods of passing function parameters, but there are differences. A reference is an alias for a variable. Modifying the reference will modify the original variable, while the pointer stores the address of the variable. Modifying the pointer value will not modify the original variable. When choosing to use a reference or a pointer, you need to consider factors such as whether the original variable needs to be modified, whether a null value needs to be passed, and performance considerations.

In-depth understanding of reference types in Go language In-depth understanding of reference types in Go language Feb 21, 2024 pm 11:36 PM

Reference types are a special data type in the Go language. Their values ​​do not directly store the data itself, but the address of the stored data. In the Go language, reference types include slices, maps, channels, and pointers. A deep understanding of reference types is crucial to understanding the memory management and data transfer methods of the Go language. This article will combine specific code examples to introduce the characteristics and usage of reference types in Go language. 1. Slices Slices are one of the most commonly used reference types in the Go language.

C++ program to check if a character is alphabetic or non-alphabetic C++ program to check if a character is alphabetic or non-alphabetic Sep 14, 2023 pm 03:37 PM

Using strings or characters is sometimes very useful when solving some logic programming problems. A string is a collection of characters, which is a 1-byte data type used to hold symbols in ASCII values. Symbols can be English letters, numbers, or special characters. In this article, we will learn how to check if a character is an English letter or a letter of the alphabet using C++. Checking the isalpha() function To check if a number is a letter, we can use the isalpha() function in the ctype.h header file. This takes a character as input and returns true if it is an alphabet, false otherwise. Let us look at the following C++ implementation to understand the usage of this function. The Chinese translation of Example is: show

See all articles