Question

write the code using c++ ( textbook from Date Structure using c++) Write a function that...

write the code using c++ ( textbook from Date Structure using c++)

Write a function that generates the squares of the first N integers and then separates these squares into 2 sets so that difference between the sum of the squares in the first set and the the sum of the squares in the second set is a small as possible. The function's parameter is N. The function returns the two sets of squares and two sums of their squares.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <bits/stdc++.h>

using namespace std;

int findMinRec(int arr[], int i, int n, int sumCalculated, int sumTotal)

{

  if (i==0) {

    return abs((sumTotal-sumCalculated) - sumCalculated);

}

return min(findMinRec(arr, i-1, n, sumCalculated+arr[i-1], sumTotal),

      findMinRec(arr, i-1, n, sumCalculated, sumTotal));

}

int findMin(int n)

{

  int arr[n];

for (int i=1; i<=n; i++) {

arr[i-1] = i*i;

}

int sumTotal = 0;

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

    sumTotal += arr[i];

  return findMinRec(arr, n, n, 0, sumTotal);

}

int main()

{

  int n = 10;

  cout << "The minimum difference between two sets is " << findMin(n);

  return 0;

}

Add a comment
Know the answer?
Add Answer to:
write the code using c++ ( textbook from Date Structure using c++) Write a function that...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Positive and negative: Return these four results using C++ reference parameter Write a function, named sums(),...

    Positive and negative: Return these four results using C++ reference parameter Write a function, named sums(), that has two input parameters; an array of floats; and an integer, n, which is the number of values stored in the array. Compute the sum of the positive values in the array and the sum of the negative values. Also count the number of positives and negative numbers in each category. Write a main program that reads no more than 10 real numbers...

  • write code using void function i need the code in C++ but make sire u solve...

    write code using void function i need the code in C++ but make sire u solve by using void function visor (GCD) of two integers is the largest nacd that returns the greatest com- its digits reversed. For example, 5.31 (Greatest Common Divisor) The greatest common divisor (GCD) of two in integer that evenly divides each of the numbers. Write a function gcd that returns the mon divisor of two integers. . . for Numeric Grades) Write a function qualityPoints...

  • Using C Write the function: long sum (long *a, long n); This function takes an array...

    Using C Write the function: long sum (long *a, long n); This function takes an array a and an integer n, and returns the sum of the n first long integers in a. You are not allowed to use the notation a[i], you must use pointer arithmetic.

  • Write the C-code for a function which returns the least common character in an set of...

    Write the C-code for a function which returns the least common character in an set of characters that occurs at least once. (The function's prototype is char least_common(int, unsigned char*);)

  • 11. Using a 'for' loop in the code, write a function that takes two values, ​m​...

    11. Using a 'for' loop in the code, write a function that takes two values, ​m​ and ​n​, as parameters and returns the sum of the numbers from ​n​ to ​m​. You may assume ​m​ < ​n​.

  • Code in C++ please! Write a function that takes an array of integers as an input...

    Code in C++ please! Write a function that takes an array of integers as an input parameter (the address of the first value of the array). It returns nothing. It prints out the array as a single line, with commas between each number, and when the array is finished being printed, it prints an endl; so that we flush the buffer and move to a new line. (I’m having you write this function because you’ll be wanting to print out...

  • PLEASE WRITE IN C# Problem: Summing up sums. For this question, you’ll understand the importance of...

    PLEASE WRITE IN C# Problem: Summing up sums. For this question, you’ll understand the importance of using the results of smaller sets of work. You’re going to create a recursive function that takes in an array of random integers and returns a “sum of sums”. This is best explained through an example. If you have 5 elements in an array, the function should return the sum of elements 0-4, 1-4, 2-4, 3-4, and 4.   So, if we had an array...

  • answer in c++ Using the table below, complete the C++ code to write the function prototype...

    answer in c++ Using the table below, complete the C++ code to write the function prototype statement, and the void function header. The void function should receive three double variables: the first two by value and the last one by reference. Name the formal parameters num1, num2 and answer. The function should divide the num1 variable by the num2 variable and then store the result in the answer variable. Name the function calcQuotient. Also write an appropriate function prototype for...

  • See the image below and write the code using C++, without using variables, only recursion. 3....

    See the image below and write the code using C++, without using variables, only recursion. 3. 5 a [DO NOT USE ANY VARIABLES Write a C++ function that takes two integer parameters (a and b) and prints a list of all the integers between a and b-1 (inclusive). one (2, 12) should print 2 34567 8 9 10 11. one (2, 13) should print 2 34567 8 9 10 11 12 b. [D NOT USE ANY VARIABLES Write a C++...

  • Creating and defining function

    Define a function called summation() which has at least one parameter called n. The function adds the first n integers and returns the result. Now, write a program using the summation() function to find the sum of the first 100 integer numbers.

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT