In this programming assignment you are tasked with writing a program which accep
ID: 3762036 • Letter: I
Question
In this programming assignment you are tasked with writing a program which accepts two arrays of values from the user and creates a third array that consists of the sums of the elements of the two user-input arrays. For example, given an array A = I. 3. 5. 7: and an array B = 41 6. 81, the resulting array C = 13.7. 11, 15 }. A recursive function called SumElementsD will be used to calculate the values in the third array. The main function should output the values stored in the three arrays to the screen. The following is the prototype for the SumElements0 function: void SumElements(int * arrayA, int * arrayB, int * arrayC, int size); u The program should prompt the user to enter the size of the arrays that the program will be using. 2) The program should create the arrays according to the size specified by the user. The program should prompt the user to enter values until both of the arrays are full and display the current values stored in the array after each input from the user. 4) After filling the two arrays with values, the SumElementsD functions should be called to set the values of the elements in the third array. s)The SumElonentso function should calculate the values in the third array BECIWSIVE IN. not by using a loop. 6) Lastly, the main function should output the values in each of the three arrays, as is shown in the sample execution. Requirements: You are to write a program in C that completes the assignment as described above. You will write/compik/run on your system and once you are satisfied that your program is correct, submit your source code to myCourses. Your program should be named according to your netID and program number. So. my program name would follow the following naming convention when submitting code: nprp89prog5.c-. Please ensure that you are naming your program appropriately and submitting the right files.Explanation / Answer
Let us solve the problem step by step.
1) Prompt the user to input size of the array and create the arrays according to the size specified by the user.
Solution:
int num_elements; //number of elements in each array
int *arr1, *arr2, *result;
printf("Enter the number of elements in each array... ");
scanf("%d", &num_elements);
arr1 = (int *)malloc(num_elements * sizeof(int)); // Dynamically allocate memory for the three arrays
arr2 = (int *)malloc(num_elements * sizeof(int));
result = (int *)malloc(num_elements * sizeof(int));
2) Prompt the user to input values for both the arrays and display the contents as they are added:
Solution:
printf("Enter the elements in first array ");
for (int i = 0; i < num_elements ; i++)
{
scanf("%d", arr1 + i);
printf("%d", arr1 + i);
}
printf("Enter Elements of Second List ");
for (int i = 0; i < num_elements; i++)
{
scanf("%d", arr2 + i);
printf("%d", arr2 + i);
}
3) Call the SumElements function:
Solution:
SumElements(int *arr1, int *arr2, int *result, num_elements);
4) Displaying content of each array
Complete code:
#include <stdio.h>
#include <malloc.h>
int i, num_elements; //number of elements in each array
int *arr1, *arr2, *result;
void sumElements(int *a, int *b, int *c, int e)
{
for (i = 0; i < e; i++)
{
*(c + i) = *(a + i) + *(b + i);
}
}
int main()
{
printf("Enter the number of elements in each array... ");
scanf("%d", &num_elements);
arr1 = (int *)malloc(num_elements * sizeof(int)); // Dynamically allocate memory for the three arrays
arr2 = (int *)malloc(num_elements * sizeof(int));
result = (int *)malloc(num_elements * sizeof(int));
printf("Enter the elements in first array ");
for (i = 0; i < num_elements ; i++)
{
scanf("%d", arr1 + i);
printf("%d",arr1[i]);
}
printf("Enter the elements in second array ");
for (i = 0; i < num_elements; i++)
{
scanf("%d", arr2 + i);
printf("%d", arr2[i]);
}
sumElements(arr1, arr2, result, num_elements);
for (i = 0; i < num_elements; i++)
{
printf("%d ", result[i]);
//printf("%d", arr2[i]);
}
}
Output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.