Question

Write a function to add two one-dimensional matrices of integer values. The matrices are represented as...

Write a function to add two one-dimensional matrices of integer values. The

matrices are represented as arrays of int and the function must return the result

as a pointer to a new dynamically allocated array of int. You can assume the

parameters are always of length greater than 0 and that no errors can occur.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

check out the solution and let me know if any queries through COMMENTS.

------------------------------------------

#include<iostream>
#include<conio.h>

using namespace std;

// global variable for length of array
int SIZE = 5;

// function defintion
int* add_arr(int arr1[], int arr2[])
{
// dynamic array created as a pointer
int* sum_arr = new int[SIZE];

for(int i=0; i<SIZE; i++)
{
// summimg up corresponding array elements and save it dynamically
sum_arr[i] = arr1[i] + arr2[i];
}

// return the newly dynamically created result array
return sum_arr;
}

// main function
int main()
{
// declare 2 arrays
int arr1[] = {1, 3, 5, 7, 9};
int arr2[] = {0, 2, 4, 6, 8};
// function call and returned result saved in a pointer
int* result_arr = add_arr(arr1, arr2);

// print the resultant array
cout << "\nThe resultant array : \n";
for(int i=0; i<SIZE; i++)
cout << result_arr[i] << "\t";
  
// delete the dynamically created array
delete [] result_arr;
// print as required
cout << "\n\nProgram ended!!!";
  
getch();
return 0;
}

-----------------------------------------------------------------------------

--------

OUTPUT ::

Add a comment
Know the answer?
Add Answer to:
Write a function to add two one-dimensional matrices of integer values. The matrices are represented as...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • IN C++ ADD COMMENTS AS MUCH AS POSSIBLE Exercise 1: Duplicate the Arrays Suppose you are...

    IN C++ ADD COMMENTS AS MUCH AS POSSIBLE Exercise 1: Duplicate the Arrays Suppose you are developing a program that works with arrays of integers, and you find that you frequently need to duplicate the arrays. Rather than rewriting the array-duplicating code each time you need it, you decide to write a function that accepts an array and its size as arguments. Creates a new array that is a copy of the argument array, and returns a pointer to the...

  • Write the code to dynamically allocate ONE integer variable using calloc (contiguous allocation) or malloc (memory...

    Write the code to dynamically allocate ONE integer variable using calloc (contiguous allocation) or malloc (memory allocation) and have it pointed to by a pointer (of type int * ) named ptr_1. Use ptr_1 to assign the number 7 to that dynamically allocated integer, and in another line use printf to output the contents of that dynamically allocated integer variable. Write the code to dynamically allocate an integer array of length 5 using calloc or malloc and have it pointed...

  • C++ Write a program to calculate the sum of two matrices that are stored inside two-dimensional...

    C++ Write a program to calculate the sum of two matrices that are stored inside two-dimensional arrays. Your program should include three functions: one for getting input data, one for calculating the sum of matrices, and one for printing the result. a) Function inputMatrix: This Function prompts the user to enter the data and stores data inside two-dimensional array. b) Function addMatrices: This function calculatesthe sum result of two matrices. c) Function printMatrix: This function prints the matrix to screen....

  • How to initialize a dynamic two-dimensional array with values? (C++) Here's my problem... # include using...

    How to initialize a dynamic two-dimensional array with values? (C++) Here's my problem... # include using namespace std; int main() {    // Q#5 - dynamic 2d arrays, indexing via subscript operator, pointer arithmetic    // tic tac toe board is an array of int pointers    // each int pointer in the board points to a row    // declare a pointer to an array of int pointers (i.e. a pointer to a pointer of type int)    const...

  • Homework Question Write a void function called transformArray that takes two parameters - a reference to...

    Homework Question Write a void function called transformArray that takes two parameters - a reference to a pointer to a dynamically allocated array of ints, and the size of that array.  The pointer is passed by referencebecause you want to change the value of the pointer. The function should dynamically allocate an array that is twice as long, filled with the values from the original array followed by each of those values times 2. For example, if the array that was...

  • Matrix Multiplication with Threads - C/C++ **Please read entire question before answering** In this assignment you...

    Matrix Multiplication with Threads - C/C++ **Please read entire question before answering** In this assignment you will use the Pthreads library to write a program that multiplies two square arrays and compare the difference between the imperative and parallel implementations of this algorithm. Use the matrix mulltiplication algorithm. Write a program that contains three functions: (1) A function that has an integer as a parameter and returns a pointer to square array of integers (i.e. both dimensions should be equal)....

  • Purpose The purpose of the lab is to learn how to pass pointers in C. Process Create the function...

    Programming in C Purpose The purpose of the lab is to learn how to pass pointers in C. Process Create the functions as defined in the following table. Each function will add the last two integer parameters together Function PrototypeReturr int add2a(int,int); Returns the sum as a return value void add2b(int* int,int); Returns the sum in the first parameter int add2c(int*,int,int); Returns the sum as a return value and in the first parameter Returns the sum as a pointer to...

  • write C code that uses pointers, arrays, and C strings. 3. Write a function called pow_xy....

    write C code that uses pointers, arrays, and C strings. 3. Write a function called pow_xy. The function should be passed 2 parameters, as illustrated in the prototype below. int pow_xy(int *xptr, int y); Assuming that xptr contains the address of variable x, pow_xy should compute x to the y power, and store the result as the new value of x. The function should also return the result. Do not use the built-in C function pow. For the remaining problems,...

  • Using C programming

    Using C, create a data file with the first number being an integer. The value of that integer will be the number of further integers which follow it in the file. Write the code to read the first number into the integer variable how_many.Please help me with the file :((This comes from this question:Write the code to dynamically allocate ONE integer variable using calloc (contiguous allocation) or malloc (memory allocation) and have it pointed to by a pointer (of type int...

  • ASSEMBLY LANGUAGE The matrix (two-dimensional array) with ROWS and COLS dimensions of integer values is given....

    ASSEMBLY LANGUAGE The matrix (two-dimensional array) with ROWS and COLS dimensions of integer values is given. Perform various matrix processing activities according to the algorithms below. Store the results into the output vector (one-dimensional array) with appropriate size. For Grade 7) Count the number of odd values (n mod 2 <> 0) for each row. For Grade 9) Calculate the sum of positive values for each column. To obtain inputs and return the results, define appropriate type C/C++ functions. Please...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT