Question

The array a[1..n] contains arbitrary integers. Write C++ function reduce(a,n) that reduces the array a[1..n] by...

The array a[1..n] contains arbitrary integers. Write C++ function reduce(a,n) that reduces the array a[1..n] by eliminating from it all values that are equal to three largest different odd integers. For example, if a[ ]={9,1,1,6,7,1,2,3,3,5,6,6,6,6,7,9} then three largest different odd integers are 5,7,9 and after reduction the reduced array will be a[ ]={1,1,6,1,2,3,3,6,6,6,6}, and n=11. If you have less than 3 largest different odd integers, eliminate only those that you found.

Homework Answers

Answer #1

#include <iostream>
#include <bits/stdc++.h>
using namespace std;

void reduce(int a[], int n)
{
int b[n], c[3], k = 0;
for(int i = 0; i < n; i++)
{
b[i] = a[i]; // replicate the array
}
  
sort(b, b + n); // sort the array b

for(int i = n - 1; i >= 0; i--)
{
if(k <= 2 && b[i] != b[i - 1])
{
if(b[i] % 2 != 0) // store the largest three odd numbers
{
c[k] = b[i];
k++;
}
}
}
  
int count = 0;
int d[n];
for(int i = 0; i < n; i++)
{
d[i] = 0; // initialize array d with the 0's
}
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < n; j++)
{
if(c[i] == a[j]) // noting the positions that not to print
{
count++;
d[j] = 1;
}
}
}
for(int i = 0; i < n; i++) // print the modified array
{
if(d[i] == 0)
{
cout << a[i] << " ";
}
}
  
}

int main()
{
cout << "Enter the size of the array : ";
int n;
cin >> n; // read the size of the array
  
int a[n];
cout << "Enter the elements : ";
for(int i = 0; i < n; i++)
{
cin >> a[i]; // read the array
}
reduce(a, n);
return 0;
}
OUTPUT :

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
c++ please 1. Write and test the function maximum that is passed an array of n...
c++ please 1. Write and test the function maximum that is passed an array of n pointers to integers and returns the maximum value among the n integers. The function must use the traveling pointer notation to traverse the array. you have to ask the user for the size of the array and ask the user to input the values in the main. The function has the following prototype. int maximum ( int *p [ ], int n);
Write a function that takes an array of integers and its size as parameters and prints...
Write a function that takes an array of integers and its size as parameters and prints the two largest values in the array. In this question, the largest value and the second largest value cannot be the same, even if the largest value occurs multiple times. In the first sample, the two largest values are 9 and 8 (even though the value 9 appears twice). In the second sample, all the values are equal and the program recognizes this by...
c++ please 1. Write and test the function maximum that is passed an array of n...
c++ please 1. Write and test the function maximum that is passed an array of n pointers to integers and returns the maximum value among the n integers. The function must use the traveling pointer notation to traverse the array. The function has the following prototype. int maximum ( int *p [ ], int n);
C Programming: Write a function that takes in an array of integers and an integer containing...
C Programming: Write a function that takes in an array of integers and an integer containing the count of elements, then have it returns the sum of all the even values inside the array.
1. Write a function named "countPositiveNegative" that accepts an array of integers, its size and return...
1. Write a function named "countPositiveNegative" that accepts an array of integers, its size and return the number of positive and negative in the array through the use of parameters. In addition, it also returns the number of 0s in the array as the return value. For example, array of {10, 20, -30, -40, 50, 0, 60} will return 4 for positives, 2 for negatives and 1 for 0's as the return value. 2. Write a function named "rotateRight" that...
Write a Scheme function isneg that will examine an arbitrary list integers and take the value...
Write a Scheme function isneg that will examine an arbitrary list integers and take the value 0 if the list doesn’t contain a negative integer, and r10 if it does contain a negative integer. You may assume that the arbitrary list passed to isneg contains at least one integer (Hint: car and cdr are your friends.)
Write a C++ function which accepts two array of integers (like arr1 and arr2) of the...
Write a C++ function which accepts two array of integers (like arr1 and arr2) of the same size (100), then create a new array (like arr3) with the same size (100) and assign the sum of corresponding elements in arr1 and arr2 to the new array (arr3) and return back arr3 from the function. You don't need to write the main function. For example sum of corresponding elements in arr1 and arr2 to be assigned to arr3 should be like:...
Write a function that returns the largest element of an array? Your function should accept a...
Write a function that returns the largest element of an array? Your function should accept a 1-D array as an input and return the largest number. You may assume all numbers are integers. CODE IN C++ PLEASE
Complete following function which receives an array of integers and the length of the array, and...
Complete following function which receives an array of integers and the length of the array, and then returns the sum of all the positive numbers in the array. For example, if an array that is passed to this function contains following numbers: -1, 2, 0, 3, 4, -3, 0, 2, 0, and then the return value of the function should be 11. Will this function be working correctly? Yes or No? int sumPositive(int a[],int length) { int s=0;     for(int...
Use C++ 1 a)Write a console program which creates an array of size 100 integers. Then...
Use C++ 1 a)Write a console program which creates an array of size 100 integers. Then use Fibonacci function Fib(n) to fill up the array with Fib(n) for n = 3 to n = 103: So this means the array looks like: { Fib(3), Fib(4), Fib(5), ...., Fib[102) }. For this part of assignment you should first write a recursive Fib(n) funcion, as was done in class.For testing, print out the 100 integers. 1 b) For second part of this...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT