Question

In: Computer Science

Please write program in c++ with using pointer. create a function that can find the number...

Please write program in c++ with using pointer.
create a function that can find the number of even numbers that are between the maximum and minimum elements of an given array.The program have to use pointer.
example
8
-1 2 2 1 9 2 4 0
output: 2

Solutions

Expert Solution

#include<iostream>
using namespace std;
void even(int *arr,int c,int *count);
int main()
{
    int c,count=0;
    cout<<"Enter the size of array : ";
    cin>>c;
    int a[c];
    cout<<"Enter the elements of array ";
    for(int i=0;i<c;i++)
    {
        cin>>a[i];
    }
    even(a,c,&count);
    cout<<"The even numbers between max and min is : "<<count;
}
void even(int *a,int c,int *count)
{
    int max=a[0],min=a[0];
    int index1=0,index2=0;
    for(int i=1;i<c;i++)
    {
        if(a[i]<min)
        {
        min=a[i];
        index1=i;
        }
        if(a[i]>max)
        {
        max=a[i];
        index2=i;
        }
    }
    for(int i=index1+1;i<index2;i++)
    {
        if(a[i]%2==0)
        {
            *count+=1;
        }
    }
    
}

Here is the above code which asks for the size of the array then it uses the size in order to enter the values and then passing the array,size,count to the function as pointer and the as we are using pointer there is no need to return a value as the change made can be seen in the main program which is the main use of pointer.so we directly print the count value which gives even numbers between the min and max values.

SCREENSHOT OF THE OUTPUT :


Related Solutions

USING PYTHON Write a program to create a number list. It will call a function to...
USING PYTHON Write a program to create a number list. It will call a function to calculate the average values in the list. Define main ():                        Declare variables and initialize them                        Create a list containing numbers (int/float)                        Call get_avg function that will return the calculated average values in the list.                                       Use a for loop to loop through the values in the list and calculate avg                        End main()
write C++ program using functions (separate function for each bottom) Write a program to find if...
write C++ program using functions (separate function for each bottom) Write a program to find if a number is large word for two given bottom base - bottom1 and bottom2. You can predict that a number, when converted to any given base shall not exceed 10 digits. . the program should ask from user to enter a number that it should ask to enter the base ranging from 2 to 16 after that it should check if the number is...
Can you write a small program in c++ demonstrate the use of pointer (*) & (&)....
Can you write a small program in c++ demonstrate the use of pointer (*) & (&). Can you also explain the pointer system and how to use them. Thank you.
IN C++ Write a program to find the number of comparisons using binarySearch and the sequential...
IN C++ Write a program to find the number of comparisons using binarySearch and the sequential search algorithm as follows: Suppose list is an array of 1000 elements. 3 Search list for some items as follows: a. Use the binary search algorithm to search the list. (You may need to modify the algorithm given in this chapter to count the number of comparisons.) b. Use the binary search algorithm to search the list, switching to a sequentialsearch when the size...
IN C++ Write a program to find the number of comparisons using binarySearch and the sequential...
IN C++ Write a program to find the number of comparisons using binarySearch and the sequential search algorithm as follows: Suppose list is an array of 1000 elements. 2 Use any sorting algorithm to sort list.
In C++ Write a program to find the number of comparisons using binarySearch and the sequential...
In C++ Write a program to find the number of comparisons using binarySearch and the sequential search algorithm as follows: Suppose list is an array of 1000 elements. 5.1 Use a random number generator to fill list.
Write a program in C++ to find the number of comparisons using binarySearch and the sequential...
Write a program in C++ to find the number of comparisons using binarySearch and the sequential search algorithm as follows: Suppose list is an array of 1000 elements. a. Use a random number generator to fill list. b. Use any sorting algorithm to sort list. c. Search list for some items as follows: i. Use the binary search algorithm to search the list. (You may need to modify the algorithm given in this chapter to count the number of comparisons.)...
Please write a C++ program to find the largest number among three numbers using nested if...else...
Please write a C++ program to find the largest number among three numbers using nested if...else statements You need to prompt the user to enter three integer numbers from the keyboard, and then display the largest number among the three. Please create a function to find the largest number among three numbers, and call the maxAmongThree function in the main function.
Can you solve this C program by using Function? Q1. Write a C program to ring...
Can you solve this C program by using Function? Q1. Write a C program to ring the computer bell at any number of times you specify. Use the system clock as a delay, you need to include the time header file.
Please, write code in c++. Using iostream and cstring library Write a function that will find...
Please, write code in c++. Using iostream and cstring library Write a function that will find and return most recent word in the given text. The prototype of the function have to be the following void mostRecent(char *text,char *word) In char *word your function have to return the most recent word that occurce in the text. Your program have to be not case-sensitive(ignore case - "Can" and "CAN" are the same words) Also note than WORD is sequence of letters...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT