Saturday, June 20, 2020

Array Type

Write a program to find the type of array using functions. An array is said to be “Even” if all the elements in the array are even. An array is said to be “Odd” if all the elements in the array are odd. An array is said to be “Mixed” if it contains some odd elements and some even elements.

Input & Output Format:

Input consists of n+1 integers where n corresponds to the number of elements in the array.

The first integer corresponds to n and the next n integers correspond to the elements in the array.

Assume that the maximum number of elements in the array is 20.

Sample Input & Output:

Enter the number of elements in the array

5

Enter the elements in the array

2

4

1

3

5

The array is Mixed

SOLUTION:

#include<iostream>

using namespace std;

void arrayFunction(int a[], int n){

  int even = 0, odd = 0;

  for(int j = 0; j < n; j++){

    if(a[j] % 2 == 0){

      even = 1;

    }

    else{

      odd = 1;

    }

  }

  if(even != 0 && odd == 0){

    cout<<"The array is Even";

  }

  else if(even == 0 && odd != 0){

    cout<<"The array is Odd";

  }

  else{

    cout<<"The array is Mixed";

  }

}

int main()

{

  int n, i;

  cout<<"Enter the number of elements in the array"<<endl;

  cin>>n;

  int a[n];

  cout<<"Enter the elements in the array"<<endl;

  for(i = 0; i < n; i ++){

    cin>>a[i];

  }

  arrayFunction(a, n);

}

No comments:

Post a Comment

horizontal ads