Question

Given an array A[] of integers find sum of product of all pairs of array elements...

Given an array A[] of integers find sum of product of all pairs of array elements

Input:
First line consists of T test cases. First line of every test case consists of N, denoting number of elements of array. Second line consists of elements of array.

Output:
Single line output, print the sum of products.

Constraints:
1<=T<=100
1<=N<=100

Solve the problem using pointers and (if possible) using operators

Example:

Input:
2
3
1 3 4
4
2 3 4 5

Output:
19
71

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

Answer:

Explanation:

Outer loop is for the number of testcases.

Then in each test case, first using a for loop, n array elements are taken input and stored in array a.

Then using nested for loops, each pair is accessed using pointers(as mentioned above) and sum of products is calculated and printed at the end of test case.

Code:


#include <iostream>

using namespace std;

int main()
{
int t;
cin>>t;
  
for(int k = 0; k<t; k++)
{
int n, sum = 0;
cin>>n;
  
int a[n];
  
for(int i=0; i<n; i++)
{
cin>>a[i];
}
  
for(int i=0; i<n-1; i++)
{
for(int j=i+1; j<n; j++)
{
sum = sum + (*(a+i) * *(a+j));
}
}
  
cout<<sum<<endl;
}

return 0;
}

Output:

PLEASE UPVOTE IF YOU FOUND THIS HELPFUL!

Add a comment
Know the answer?
Add Answer to:
Given an array A[] of integers find sum of product of all pairs of array elements...
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
  • Write in Java. Program need to have runtimes < n^2 to satisfy the runtime efficiency of...

    Write in Java. Program need to have runtimes < n^2 to satisfy the runtime efficiency of some of the testsets. Question 2: Understanding Orders Given an array A of size N, find the number of ordered pairs (i, j) such that i < j and A[i] < A[j]. Input: • First line contains one integer, N, size of array. • Second line contains N space separated integers denoting the elements of the array A. Output: Print the total number of...

  • Java code INNER PRODUCT 4E Input Standard input Output Standard output Topic Array & Array Processing!...

    Java code INNER PRODUCT 4E Input Standard input Output Standard output Topic Array & Array Processing! Problem Description The inner product (also known as the dot product or scalar product) of the elements of set A and the elements of set B of size is defined as the sum of the products of corresponding terms. For example, given set A as the array (2, 3, 4, 5, 6, 7, 8, 9; and set B as 6,5, 4, 3, 2, 7,...

  • Given an array of integers representing heights of consecutive steps, we say that a tumble is...

    Given an array of integers representing heights of consecutive steps, we say that a tumble is a sequence of strictly decreasing numbers and the height of the tumble is the difference between the height of the top step and the bottom step. For example, if there are consecutive steps with heights 16, 9, 4 then they form a tumble of height 12 (= 16-4). With consecutive steps of heights, 0, 4, 12, 4, 5, 7, 2, 1 there is a...

  • In Java, Implement a class MyArray as defined below, to store an array of integers (int)....

    In Java, Implement a class MyArray as defined below, to store an array of integers (int). Many of its methods will be implemented using the principle of recursion. Users can create an object by default, in which case, the array should contain enough space to store 10 integer values. Obviously, the user can specify the size of the array s/he requires. Users may choose the third way of creating an object of type MyArray by making a copy of another...

  • (+30) Provide a python program which will Populate an array(list) of size 25 with integers in...

    (+30) Provide a python program which will Populate an array(list) of size 25 with integers in the range -100 (negative 100)   to +100 inclusive Display the array and its length (use the len function) Display the average of all the integers in the array Display the number of even integers (how many) Display the number of odd integers (how many) Display the number of integers > 0   (how many) Display the number of integers < 0   (how many) Display the...

  • Java code ABOVE AVERAGE 40 Input Standard input Output Standard output Topic Array & Array Processing...

    Java code ABOVE AVERAGE 40 Input Standard input Output Standard output Topic Array & Array Processing Problem Description Understanding how to interpret test scores is a valuable skill for every teacher. That's because test score interpretation enables teachers to understand how their students' performance for each tests of the course. Average test score is one of the indicators to determine the class performance for the test. For each test, we need to calculate the average score and determine the percentage...

  • Use C programming Swap two arrays using pointers Given two integer arrays, swap the two arrays...

    Use C programming Swap two arrays using pointers Given two integer arrays, swap the two arrays using pointers. Input:     4     1 2 3 4     5     4 5 6 7 8     where: First line represents the number of elements in the first array. Second line represents the elements in the first array. Third line represents the number of elements in the second array. Fourth line represents the elements in the second array. Output:     4 5 6 7...

  • Please submit only Python source code. 1. Arithmetic trees 50 marks You are given an input file with multiple pairs of...

    Please submit only Python source code. 1. Arithmetic trees 50 marks You are given an input file with multiple pairs of input lines. The first line of each pair is a tree given as a predecessor array. The second line is the value at the corresponding node. Values at leaf nodes (nodes with no children) are integers. At non-leaf nodes, the two possible values are + or *. The tree represents an arithmetic expression where the value at a non-leaf...

  • Problem 3: (5 2 points) Design an algorithm that takes an array of positive integers A...

    Problem 3: (5 2 points) Design an algorithm that takes an array of positive integers A of length n and a positive integer T as an input and finds the largest N < T such that N can be written as a sum of some elements of A and returns such a representation of N. The complexity of the algorithms has to be O(nT). For example, for A 3,7, 10 and T 19, the output is 17 7+10, because we...

  • C++ You're given the pointer to the head nodes of two linked lists. Compare the data...

    C++ You're given the pointer to the head nodes of two linked lists. Compare the data in the nodes of the linked lists to check if they are equal. The lists are equal only if they have the same number of nodes and corresponding nodes contain the same data. Either head pointer given may be null meaning that the corresponding list is empty. Input Format You have to complete the int CompareLists (Node headA, Node* head B) method which takes...

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