Ask Question

Write a C+ + program to find the number of pairs of integers in a given array of integers whose sum is equal to a specified number.

+3
Answers (1)
  1. 3 January, 04:41
    0
    int main ()

    {

    int A1 = {5,3,4,8,9,0,7};

    int SArri1 = sizeof (A1);

    printf ("Original array: ");

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

    printf ("", A1[i]);

    int i, sum = 20, n = 0;

    printf ("/nArray pairs whose sum equal to 20: ");

    for (int i=0; i
    for (int j=i+1; j
    if (A1[i]+A1[j] = = sum)

    {

    Printf ("/n", array1[i])

    Printf (",", array1[j]);

    n++;

    }

    printf ("/nNumber of pairs whose sum equal to 20: ", n)

    return 0;

    }

    Explanation:

    First of all, you should create the array of integers, and put random numbers. Then you have to save in a constant the size of the array (in this code is called SArr1) With a for you can print all the numbers that are on the array because then you will print all array pairs whose sum is equal to a specified numer. in this code we are going to use 20.

    then with a condition (if) you are going to compare one of number of the array with all the others and check if its equal to 20. If yes, it going to print the numbers that answer to that condition and it's going to add + 1 to the variable n (for this you will need to use two bucles for)

    Then you can print the number of pairs whose sum is equal to 20 by printing n
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “Write a C+ + program to find the number of pairs of integers in a given array of integers whose sum is equal to a specified number. ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers