Click here to Skip to main content
15,885,435 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
1.Define a function void populateArray(int arr[], int length); to populate an array of length n with randomly generated integer values between 0 to max.

2.Define a function void showIntegerPairs(int arr[], int arr_length, int sum); that would find and display all pairs of integers from a given array whose sum is equal

Sample Output
How many numbers do you want to generate? > 10

What is maximum number that can be generated? > 15

What should the sum of the integer pairs? > 9

Generated list:

2, 6, 3, 9, 1, 11, 5, 13, 4, 8

Integer pairs that would sum to 9:

{6,3}

{1,8}

{5,4}

Would you like to continue? ("yes"/"no") >

What I have tried:

C++
#include <stdio.h>
#include <time.h>

void populateArray(int arr[], int length);
void showIntegerPairs(int arr[], int arr_length, int sum);
int main(void)
{   int arr, length, max, sum;
    length = sizeof(arr)/sizeof(arr[0]);
    printf("How many numbers do you want to generate? >");
    scanf("%d",&length);
    printf("Enter the maximum value of random number\n");
   scanf("%d", &max);
    printf("What should the sum of the integer pairs? >");
    scanf("%d",&sum);


   return 0;
}
void populateArray(int array[], int length){

    int i, array[length], max;

    for(i=0;i<length;i++)
    array[i]= rand() % max+1;
{
printf("\nGenerated list:");
for(i=0;i<length;i++)
{
printf("%d",array[i]);

for (int i = 0 ; i < n ; ++i) {
    arr[i] = randtomax();}
void showIntegerPairs(int arr[], int arr_length, int sum){
{
    // read upto to the last element - 1
    for (int i = 0; i < arr_length - 1; i++)
    {
       // read i'th element to last element
        for (int j = i + 1; j < arr_length; j++)
        {
            // if given sum is found
            if (arr[i] + arr[j] == sum)
            {
                printf("Pair of elements can make the given sum by the value of index %d and %d", i, j);
                return;
            }
        }
    }
Posted
Updated 14-Dec-20 20:45pm
v2
Comments
Sandeep Mewara 15-Dec-20 2:25am    
And where are you stuck when you tried it?

Try
C
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void populateArray(int array[], int length, int max);
void showIntegerPairs(int arr[], int arr_length, int sum);
int main(void)
{   int length, max, sum;
    printf("How many numbers do you want to generate? >");
    scanf("%d",&length);
    printf("Enter the maximum value of random number\n");
    scanf("%d", &max);
    printf("What should the sum of the integer pairs? >");
    scanf("%d",&sum);

    int arr[length];

    printf("Generated list:\n");
    populateArray(arr, length, max);

    printf("Integer pairs that would sum to %d\n", sum);
    showIntegerPairs(arr, length, sum);


   return 0;
}
void populateArray(int array[], int length, int max)
{
  int i;

  for(int i=0;i<length;i++)
  {
    array[i]= rand() % max+1;
  }

  printf("\nGenerated list:");
  for(i=0;i<length;i++)
  {
    printf("%d ",array[i]);
  }
  printf("\n");
}
void showIntegerPairs(int arr[], int arr_length, int sum)
{
  // read upto to the last element - 1
  for (int i = 0; i < arr_length - 1; i++)
  {
    // read i'th element to last element
    for (int j = i + 1; j < arr_length; j++)
    {
      // if given sum is found
      if (arr[i] + arr[j] == sum)
        printf("{%d,%d}\n", arr[i], arr[j]);
    }
  }
}
 
Share this answer
 
Writing the functions is one thing: you have to call them as well.
Your app only executes this code:
C++
int main(void)
{   int arr, length, max, sum;
    length = sizeof(arr)/sizeof(arr[0]);
    printf("How many numbers do you want to generate? >");
    scanf("%d",&length);
    printf("Enter the maximum value of random number\n");
   scanf("%d", &max);
    printf("What should the sum of the integer pairs? >");
    scanf("%d",&sum);


   return 0;
}
Which inputs some values. You have to actually call your functions in that code to populate your array, and show the pairs.

So add code to create the array, pass it to populateArray, then again to showIntegerPairs to complete the assignment.

Then test it. Lots. And then debug it!

Hint: read the question caerfully:
Quote:
Define a function void showIntegerPairs(int arr[], int arr_length, int sum); that would find and display all pairs of integers from a given array whose sum is equal
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900