Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Write a program merge.c that include the following function: void merge(int n

ID: 3844985 • Letter: 1

Question

1. Write a program merge.c that include the following function:

void merge(int n, int a1[], int a2[], int a3[]);

that has a parameter as an integer array a1[] of length n and another parameter an integer array a2[] of length n. The function merges the contents of the two integer arrays a1 and a2 to a3. Thus, if a1[] contains the values -9, 16, 0, 2, and a2[] contains 1, 3, 5, 4, then make a3 equal to -9, 1, 16, 3, 0, 5, 2, 4. Nothing is returned.

In the main function, ask the user to enter the length of the arrays, declare the arrays, read in the values for the two arrays, and call the merge function to compute the third array. The main function should display the result of the third array.

Enter the length of the array: 3

Enter the elements of the first array: 3 4 9

Enter the elements of the second array: 7 2 1

Output: The output array is: 3 7 4 2 9 1 2.

2. Write a program range.c that include the following functions.

int consecutive(int a[], int n);

int inRange(int a[], int n, int low, int high);

The consecutive function tests whether the integer array has three consecutive numbers with the same value. The function returns 1 if there are three consecutive numbers with the same value and return 0 if not. Assumes that the array has at least three elements;

The inRange function compares elements of the array with low and high, returns 1 if all the elements of the array are in the range (inclusive) and returns 0 if there is element in the array that are not in the range.

In the main function, ask the user to enter the length of the array, declare the array with the length, read in the values for the array and low and high, and call the two functions. The main function should display the result.

Sample run #1:

Enter the length of the array: 5

Enter the elements of the array: 3 4 4 4 9

Enter low and high: 2 9

Output:

There are three consecutive numbers with the same value in the array.

All numbers are in the range.

Sample run #2:

Enter the length of the array: 5

Enter the elements of the array: 3 7 4 4 1

Enter low and high: 2 9

Output:

The array does not contain three consecutive numbers with the same value.

There are elements in the array that are not in the range.

Explanation / Answer

QUESTION 1

#include <stdio.h>

void merge(int n, int a1[], int a2[], int a3[]); //function declaration

int main()
{
int g,n,i;
printf("Enter the length of the array "); //accepting the length of the array
scanf("%d",&n);
g=n+n;
int b[n],c[n],d[g];
printf("Enter the element in the first array "); //accepting the elements in the first array
for(i=0;i<n;i++)
{
scanf("%d",&b[i]);
}
printf("Enter the element in the second array "); //accepting the elements in the second array
for(i=0;i<n;i++)
{
scanf("%d",&c[i]);
}
merge(n,b,c,d); //calling the function with the parameters
return 0;
}

void merge(int n, int a1[], int a2[], int a3[])
{
int k,m,i,temp,j;
k=n+n;
m=0;
for(i=0;i<n;i++) //merging the two array
{
a3[m]=a1[i];
m=m+1;
a3[m]=a2[i];
m=m+1;
}
printf("The new merged array is "); //displaying the new array
for(i=0;i<k;i++)
printf("%d ",a3[i]);
}

QUESTION 2

#include <stdio.h>
int consecutive(int a[], int n); //function declaration
int inRange(int a[], int n, int low, int high);

int main()
{
int n,i,l,h,v1,v2;
printf("Enter the length of the array "); //accepting the length of the array
scanf("%d",&n);
int b[n];
printf("Enter the element in the array "); //accepting the elements in the first array
for(i=0;i<n;i++)
{
scanf("%d",&b[i]);
}
printf("Enter the low ");
scanf("%d",&l);
printf("Enter the high ");
scanf("%d",&h);
v1=inRange(b,n,l,h); //calling the inrange function
v2=consecutive(b,n); //calling the consecutive function
if(v2==1) //checking with return calue of consecutive function
printf("There are three consecutive numbers with the same value in the array. ");
else
printf("The array does not contain three consecutive numbers with the same value. ");
if(v1==1) //checking the return value of the inRange function
printf("All numbers are in the range. ");
else
printf("There are elements in the array that are not in the range. ");
return 0;
}

int inRange(int a[], int n, int low, int high)
{
int i,flag;
for(i=0;i<n;i++)
{
if(a[i]>=low && a[i]<=high) //checking the arrya elements whenther in the given range
{
flag=1;
continue;
}
else
{
flag=0;
break;
}
}
return flag; //returnuing the value
}
int consecutive(int a[], int n)
{
int i,flag;
flag=0;
for(i=0;i<n;i++)
{
if(a[i]==a[i+1] && a[i]==a[i+2]) //checking for the consecutive same value
{
flag=1;
break;
}
}
return flag; //returning the value.
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote