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...
In C++ write a function to find a product of two matrices using arrays. The function...
In C++ write a function to find a product of two matrices using arrays. The function should be general and should accept any size matrices.
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 this code in c++. Using iostream and cstring library. Write a function that will...
Please, write this code in c++. Using iostream and cstring library. Write a function that will delete all words in the given text that meets more that one time. Also note than WORD is sequence of letters sepereated by whitespace. Note. The program have to use pointer. Input: First line contains one line that is not longer than 1000 symbols with whitespaces. Each word is not longer than 30 symbols. Output: Formatted text. example: input: Can you can the can...
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...
Note: Present results using fprintf()and comment your code throughout. Write a function that will compute the...
Note: Present results using fprintf()and comment your code throughout. Write a function that will compute the volume and surface area of a rectangular prism when provided with the object’s 3 numerical dimensions. In other words, the function should be given three inputs (height, width and depth) and return two outputs (the volume and surface area, in that order). Show the following test cases: height: 5   width:3    depth: 2 heignt:1    width:8    depth:4 height: 2   width:2    depth:10 The script should call the...
Using pseudocode, write the code that will perform the necessary file operations as described in the...
Using pseudocode, write the code that will perform the necessary file operations as described in the short scenario below: “During registration at a college, student’s details will be captured by a staff member and written to a file called “studentDetails.dat”. The following student details will be captured: name, surname, studentNumber, registeredCourse. The staff member will be prompted for the student details. After every record added to the file, the staff member will be asked whether they would like to add...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT