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

. (a) Define a polymorphic function? Give an example of a polymorphic function.

ID: 3735475 • Letter: #

Question

. (a) Define a polymorphic function? Give an example of a polymorphic function. (c) An array of length 4 (28, 44, 8, 16) and a scalar (4) are the two inputs to a (d) Two arrays, one of length 3 (7,28, 35) and the other of length 4 (5, 13,9,2) An array of length 3 (15, 9, 43) and a scalar (23) are the two inputs to an add function. Show these and the resulting output array. (b) 17 division function. Show these and the resulting output array. are inputs to an add function. Show these and the resulting output array

Explanation / Answer

a)A polymorohic function means it has diferent forms or having different behaviours
depending on their runtime conditions. It is an OOPS concept.Assume a base class
Animal having a function called Move. Now all the subclasses of Animal will have their own
implementation of move. So move is a polymorphic function.So a pointer of type Animal , if currently holding a subclass object say insect, them move function of the insect will be called and not of the base class. Also there is static polymorphism where a function with same
name but different type of arguments and return type behave differently as per their arguments. For example we have two add functions one fo integer and one for float.

int add(int a, int b)
float add(float a , float b)

Also called as fnction overloading.


b) void add(int *p, int a, int len){
       int i;
       for (i = 0; i < len; i++){
          p[i] = p[i] + a;
       }
   }

The above function can be used for both b and c

The output for b)
   [38, 32, 66]
for c
   [32,48,12,20]

d) void add(int *p, int *q, int len1, int len2){
       int i, j;
       for (i = 0; i<len2; i++){
          for (j = 0; j<len1; j++){
              add(p,q[i],len1);
          }
       }
   }

We have used function overlaoding as we have used the add function in b

The output array will be:

[36,57,64]