97.2k views
1 vote
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.

User Sherna
by
5.5k points

1 Answer

3 votes

Answer:

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

{

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

}

Step-by-step 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.
User Srgb
by
5.1k points