Question

In: Computer Science

Using pseudocode or C++ code, write a function to compute and return the product of two...

Using pseudocode or C++ code, write a function to compute and return the product of two sums. The first is the sum of the elements of the first half of an array A. The second is the sum of the elements of the second half of A. The function receives A and N ≥ 2, the size of A, as parameters. (in c++)

Solutions

Expert Solution

Code (It has been taken into account that the user will provide Array with size that is N>=2):

#include <iostream>

using namespace std;

// Function that returns the product of sum of first half of array and
// second half of array.
int productOfSums(int A[], int N)
{
int sumHalf1 = 0; // For sum of first half.
int sumHalf2 = 0; // For sum of second half.
  
// For loop to calculate sum of first half.
// i is going from index 0 to N/2 - 1 so, there are two cases:
// If N is even then the array will be split exactly into two halves.
// If N is odd then the first half of array will contain one less than the second half.
for(int i=0; i<N/2; i++)
sumHalf1 = sumHalf1 + A[i];
  
// For loop to calculate sum of second half.
for(int i=N/2; i<N; i++)
sumHalf2 = sumHalf2 + A[i];

// Just returning the product of both the sums calculated above.
return sumHalf1*sumHalf2;
}

// main function for testing our function.
int main()
{
int A[] = {1,2,3,4,5,6}; // Sample array for testing our productsOfSums function.
cout<<productOfSums(A,6); // Output the return value to console.
  
return 0;
}

Code Screenshots:

Code Output Screenshot for the input given in main:


Related Solutions

Using pseudocode or C++ code, write code to print “small” if the magnitude M of an...
Using pseudocode or C++ code, write code to print “small” if the magnitude M of an earthquake is in the range [0, 3), “medium” if M is in the range [3, 6), “large” if M is in the range [6, 9) and “epic” if M is greater than or equal to 9, where M is input by a user via the keyboard. (in c++)
write pseudocode for the following problems not c code Pseudocode only Write a C program to...
write pseudocode for the following problems not c code Pseudocode only Write a C program to print all natural numbers from 1 to n. - using while loop Write a C program to print all natural numbers in reverse (from n to 1). - using while loop Write a C program to print all alphabets from a to z. - using while loop Write a C program to print all even numbers between 1 to 100. - using while loop...
1.Using C++ code, write a program named q4.cpp to compute the product (multiplication) of 4 integers...
1.Using C++ code, write a program named q4.cpp to compute the product (multiplication) of 4 integers a, b, c and d, where a is input by a user via the keyboard, b = 2a - 1, c = ab - 2 and d = |a - b - c|. 2.Using C++ code, write a program named q5.cpp to solve the equation 2n = 14.27 for n. 3.Using C++ code, write a program named q3.cpp to divide each of the elements...
Using the pseudocode found below, write only the actual (C++) code for this program. Include all...
Using the pseudocode found below, write only the actual (C++) code for this program. Include all libraries. Specification: Write a program that will repeatedly ask the user for quiz grades in the range from 0 to 20. Any negative value will act as a sentinel. When the sentinel is entered compute the average of the grades entered. Design: Constants None Variables float grade float total = 0 int count = 0 float average ------- Inital Algorithm Repeatedly ask user for...
Write pseudocode for Boolean product of two matrices. Identify how sections of the code computing cycles...
Write pseudocode for Boolean product of two matrices. Identify how sections of the code computing cycles grows with increasing matrix sizes. 10 points pseudocode 10 points description of code computing cycle growth
Please write variables and program plan(pseudocode) of this C++ programming code: #include <iostream> using namespace std;...
Please write variables and program plan(pseudocode) of this C++ programming code: #include <iostream> using namespace std; void leapYear(int x); int main() { int x; cout << "Enter a year: "; cin >> x; leapYear (x);   return 0; } void leapYear(int x ) {    if (x % 400 == 0)    {    cout << "This is a leap Year";}    else if    ((x % 4 == 0) && (x % 100 != 0))    {    cout <<...
Write a function using MATLAB that will accept a structure as an argument and return two...
Write a function using MATLAB that will accept a structure as an argument and return two cell arrays containing the names of the fields of that structure and the data types of each field. Be sure to check that the input argument is a structure, and generate an error message if it is not.
Given two sorted lists, L1 and L2, write an efficient C++ code to compute L1 ∩...
Given two sorted lists, L1 and L2, write an efficient C++ code to compute L1 ∩ L2 using only the basic STL list operations. What is the running time of your algorithm?
Using C++ code, write a program named q3.cpp to compute the total amount of medicine m...
Using C++ code, write a program named q3.cpp to compute the total amount of medicine m absorbed by a rabbit, where the rabbit gets 2 pills containing n1 and n2 grams of medicine, respectively, of which the rabbit absorbs 60% and 35%, respectively, and n1 and n2 are input by a user via the keyboard.  
Using C++ code, write a program named q3.cpp to compute the total amount of medicine m...
Using C++ code, write a program named q3.cpp to compute the total amount of medicine m absorbed by a rabbit, where the rabbit gets 2 pills containing n1 and n2 grams of medicine, respectively, of which the rabbit absorbs 60% and 35%, respectively, and n1 and n2 are input by a user via the keyboard.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT