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:
#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){
{
for (int i = 0; i < arr_length - 1; i++)
{
for (int j = i + 1; j < arr_length; j++)
{
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;
}
}
}