Question

In: Computer Science

Write a function that removes all even numbers from an array. The function should take the...

Write a function that removes all even numbers from an array. The function should take the array, length of the array, and a pointer for number of odd numbers found as arguments and return a pointer to the new array. If there are no odd numbers found in the array, your code should print "No odds found." and return NULL.

Use the function header:

int *removeEvens(int *a, int length, int *oddsFound);

Example:

Input array a[ ] = {3, 4, 5, 6, 7}

*oddsFound = 3

return array = {3, 5, 7}

The size of the return array needs to be the number of odd numbers found.

Note: You can determine if a number is even by checking if a[x] % 2 == 0.

Solutions

Expert Solution

C++ Program:

#include <iostream>

using namespace std;

//Function that removes even numbers from array
int *removeEvens(int *a, int length, int *oddsFound)
{
    int i, k=0;

    int *oddNumbers;

    //Initializing oddsFound to 0
    *oddsFound = 0;

    //Counting number of odd numbers
    for(i=0; i<length; i++)
    {
        //Evaluating for odd number
        if(a[i] % 2 != 0)
            //Incrementing count
            *oddsFound += 1;
    }

    //If there are no odd numbers
    if(oddNumbers == 0)
    {
        //Printing error message and returns NULL
        cout << "\n\n No odds found. \n\n";
        return NULL;
    }
    else
    {
        //Allocating space to array
        oddNumbers = new int[*oddsFound];

        //Storing odd numbers
        for(i=0; i<length; i++)
        {
            //Searching for odd number
            if(a[i] % 2 != 0)
            {
                //Adding to array
                oddNumbers[k] = a[i];
                k++;
            }
        }

        //Returns odd numbers array
        return oddNumbers;
    }
}

//Main function
int main()
{
    //Array
    int a[] = {3, 4, 5, 6, 7};
    int oddsFound;
    int *oddNumbers;
    int i;

    //Calling function
    oddNumbers = removeEvens(a, 5, &oddsFound);

    cout << "\n\n Odd numbers: ";

    //Printing odd numbers
    for(i=0; i<oddsFound; i++)
    {
        cout << " " << oddNumbers[i];
    }

    cout << "\n\n";
    return 0;
}

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------

Sample Output:


Related Solutions

** * Write a recursive function that removes the first k even numbers * from the...
** * Write a recursive function that removes the first k even numbers * from the stack. If there are less than k even elements in the stack, * just remove all even elements. Do not use any loops or data structures * other than the stack passed in as a parameter. * @param stack * @param k * @return Returns the number of elements removed from the stack. */ public static int removeEvenNumbers(Stack<Integer> stack, int k) { return 0;...
in C programming language Write a function removeDups that removes all duplicates in a given array...
in C programming language Write a function removeDups that removes all duplicates in a given array of type int. Sample Test Case: input -> {1,2,2,2,3,3,4,2,4,5,6,6} output -> {1,2,3,4,5,6,0,0,0,0,0,0} More specifically, the algorithm should only keep the first occurance of each element in the array, in the order they appear. In order to keep the array at the same length, we will replace the removed elements with zeros, and move them to the end of the array.
Write a function that takes two integer inputs and returns the sum of all even numbers...
Write a function that takes two integer inputs and returns the sum of all even numbers between these inputs, and another function that takes two integer inputs and returns the sum of odd numbers between these inputs .In main function, the program will asks the user to enter two integer numbers and then passes them to these two functions and display the result of each of them
Write a function that will generate an array of random numbers. It needs to:
DO IN C++Write a function that will generate an array of random numbers. It needs to:Take 3 integers as parameters-The first is the minimum value-the second is the maximum value-the third is the size of the new array-create a dynamically allocated array of the correct size-generate a random number (between min and max) for each element in the array-return a pointer to the arrayCreate a main() function that tests the above function and displays the values in the random array.
Write a function that will take in an array (of type double), and will return the...
Write a function that will take in an array (of type double), and will return the array with all of the elements doubled. You must use pass-by-reference and addressing and/or pointers to accomplish this task. C++
Write a loop that will calculate the sum of all even numbers from 2 to 30...
Write a loop that will calculate the sum of all even numbers from 2 to 30 ( including 30) store the result in the variable called thirtySum. Declare and initialize all variables. Answer using programming in c.
Use the following two loop strategies to write a function that sums all even numbers before...
Use the following two loop strategies to write a function that sums all even numbers before an odd number is observed in any given numeric vector, and test your code in R. For example, if the input vector is 2, 4, -2, 3, 4, 5, then the first odd number appears in the fourth position, which is 3. The output should be the sum of the first to the third numbers, which will be 2 + 4 − 2 =...
Java Write a method that removes duplicates from an array of strings and returns a new...
Java Write a method that removes duplicates from an array of strings and returns a new array, free of any duplicate strings.
I need to write a function the will take in an array (of type double), and...
I need to write a function the will take in an array (of type double), and will return the array with all of the elements doubled while using pass-by-reference and addressing and/or pointers. This is what i have so far, but im messing up in line 31 where i call the function i believe so im not returning the correct array? please help edit. #include <iostream> #include <iomanip> using namespace std; double *doubleArray ( double arr[], int count, int SIZE);...
Write a function of interest to you. It should that take at least 3 inputs from...
Write a function of interest to you. It should that take at least 3 inputs from different types and return at least three different values. Call your function from the main. Print the resultant outputs in the main (not in the function).
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT