Assume the existence of a class Arr with two (public) data members: a pointer to integer named arr_ptr, and an integer variable namedlength that will contain the number of elements in the array. Assume the variable a has been declared to be of type Arr, an array has been allocated and assigned to arr_ptr, and length has been assigned the proper value (i.e., the number of elements in the array). Write the code to add 1 to each element of the array.

Respuesta :

CPED

Answer:

for(int i = 0;i<a.length;i++)

{

   a.arr_ptr[i] = a.arr_ptr[i] + 1;  

}

Explanation:

Step by step explanation of above code is as follows:

              for(int i = 0;i<a.length;i++)

  • Initiating the for loop from "i=0" until "i" become equal to the variable length (number of elements in array).

               a.arr_ptr[i] = a.arr_ptr[i] + 1;      

  • Taking each value of "i" from the loop to be the index of array arr_ptr and adding one to each element of array one by one.
ACCESS MORE