Question

In: Computer Science

Write a C function to add the elements of two same-sized integer arrays and return ptr...

Write a C function to add the elements of two same-sized integer arrays and return ptr to a third array.

int *addTwoArrays(int *a1, int *b1, int size);

The function should follow the following rules:

  • If the sum for any element is negative, make it zero.
  • If a1 and b1 point to the same array, it returns a NULL
  • If any input array is NULL, it returns a NULL.

Please call this function with the following arrays and print the sums (print sum array elements in separate lines for cases i, ii below).

i. int a1[] = {1, -15, 2, 14, 3, -13, 0};

int b1[] = {0, 16, 2, -15, -3, 10, 0};

ii. int a2[] = {100, 101, 200, -3011};

int b2[] = {1000, 1010, -300, 10000};

Hint: For the array with sum, you may have to declare a global var

Solutions

Expert Solution

#include<stdio.h>
int sum[];

int *addTwoArrays(int *a1, int *b1, int size)
{
if(a1==b1)
{
return NULL;
}
if((a1==NULL)||(b1==NULL))
{
return NULL;
}
for(int i=0;i<size;i++)
{
sum[i] = 0;
}
for(int i=0;i<size;i++)
{
sum[i] = a1[i] + b1[i];
}
printf("Sum is: ");
for(int i=0;i<size;i++)
{
if(sum[i]<0)
{
sum[i]=0;
}
printf("\t%d\t",sum[i]);
}
  
}
int main()
{
int i;

//for the execution of case1
int a1[]={1,-15,2,14,3,-13,0};
int b1[]={0,16,2,-15,-3,10,0};
int size=7;
printf("\ncase 1:\n");
printf("\ta1[]=\t");
for(i=0;i<size;i++)
printf("%d\t\t",a1[i]);
printf("\n\tb1[]=\t");
for(i=0;i<size;i++)
printf("%d\t\t",b1[i]);
printf("\n");
addTwoArrays(a1,b1,size);
  
//for the execution of case2
int a[]={100,101,200,-3011};
int b[]={1000,1010,-300,10000};
size=4;
printf("\n\ncase 2:\n");
printf("\ta1[]=\t");
for(i=0;i<size;i++)
printf("%d\t\t",a[i]);
printf("\t");
printf("\n\tb1[]=\t");
for(i=0;i<size;i++)
printf("%d\t\t",b[i]);
printf("\n");
addTwoArrays(a,b,size);
return 0;
}

OUTPUT GENERATED:

case 1:
   a1[]=   1       -15       2       14       3       -13       0      
   b1[]=   0       16       2       -15       -3       10       0      
Sum is:    1       1       4       0       0       0       0  

case 2:
   a1[]=   100       101       200       -3011          
   b1[]=   1000       1010       -300       10000      
Sum is:    1100       1111       0       6989


Related Solutions

In C++ Write a function that accepts two int arrays of the same size. The first...
In C++ Write a function that accepts two int arrays of the same size. The first array will contain numbers and the second array will be filled out inside the function. THE FUNCTION SHOULD FIND ALL NUMBERS IN THE ARRAY THAT ARE GREATER THAN OR EQUAL TO THE AVERAGE. You need to design the function. so the output code should be: (show contents of 1st array) The numbers that are greater than the average of the first array are: (the...
Write a c program Write a function to swap two elements of an integer array. Call...
Write a c program Write a function to swap two elements of an integer array. Call the function to swap the first element, i[0] with last element i[n], second element i[1] with the last but one element i[n-1] and so on. Should handle arrays with even and odd number of elements. Call the swap function with the following arrays and print results in each case before and after swapping. i. int arr1[] = {0, 1, 2, 3, 30, 20, 10,...
Write a C++ function template to add two inputs and return their result. Make exceptions for...
Write a C++ function template to add two inputs and return their result. Make exceptions for Characters (such that sum of two characters is a character associated with sum of their ASCII) and String (such that sum of two strings is their concatenation)
Write a C function to swap the first and last elements of an integer array. Call...
Write a C function to swap the first and last elements of an integer array. Call the function from main() with an int array of size 4. Print the results before and after swap (print all elements of the array in one line). The signature of the arrItemSwap() function is: void arrItemSwap(int *, int, int); /* array ptr, indices i, j to be swapped */ Submit the .c file. No marks will be given if your pgm does not compile...
Write a function called splice() that “splices” two integer arrays together by first allocating memory for...
Write a function called splice() that “splices” two integer arrays together by first allocating memory for a dynamic array with enough room for both integer arrays, and then copying the elements of both arrays to the new array as follows:             first, copy the elements of the first array up to a given position,             then, copy all the elements of the second array,             then, copy the remaining elements in the first array. The function should have parameters for...
In C++, write a function that takes in as inputs two arrays, foo and bar, and...
In C++, write a function that takes in as inputs two arrays, foo and bar, and their respective array sizes. The function should then output the concatenation of the two arrays as a singly linked list. You may assume that you are provided a singly linked list header file.
Write a C++ function which will search for TWO elements. The two elements must be right...
Write a C++ function which will search for TWO elements. The two elements must be right next to each other, contiguously. The prototype of the function should be: bool search_adjacent_elements(int const *x, size_t n, int target1, target2); It will return true if target1 and target2 exist in the array x somewhere, with target1 immediately to the left of target2. For example: int x[] = { 3, 5, 10, 14, 19, 25, 45, 60 }; cout << search_adjacent_elements(x, sizeof x /...
You are given two integer arrays a and b of the same length. Let's define the...
You are given two integer arrays a and b of the same length. Let's define the difference between a and b as the sum of absolute differences of corresponding elements: difference = |a[0] - b[0]| + |a[1] - b[1]| + ... + |a[a.length - 1] - b[b.length - 1]| You can replace one element of a with any other element of a. Your task is to return the minimum possible difference between a and b that can be achieved by...
Write a function that will accept two integer matrices C and D by reference parameters. The...
Write a function that will accept two integer matrices C and D by reference parameters. The function will compute the transpose of C and store it in D. For your information, the transpose of matrix C is D, where D[j][i] = C[i][j]. [7 marks] Explain the time complexity of this function inside of the function code as a comment. [3 marks] in C++
Write a function in C that uses the Merge Sort sorting algorithm with arrays. The function...
Write a function in C that uses the Merge Sort sorting algorithm with arrays. The function must not be void and must output type int* i.e. it must take the form: int* merge_sort(int a[], int n) where a[ ] is the input matrix and n is the size of the matrix. You may use an auxiliary functions such as "merge." The returned array should be sorted using merge_sort and should not modify the array that was input (a[ ] ).
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT