IDNLearn.com offers a unique blend of expert answers and community insights. Ask any question and receive comprehensive, well-informed responses from our dedicated team of experts.

Create a function called makePositive that accepts a single argument: an integer array. The function should walk through the array and change any negative numbers to positive. You can assume that the array passed to the function will have a 0 at the end of the array. In other words, you do not know the size of the array; you instead know that a certain condition will be true at the end of the array.

Sagot :

Answer:

This solution is implemented in C++

void makePositive(int arr[]){

   int i =0;

   while(arr[i]!=0){

       if(arr[i]<0){

           arr[i] = abs(arr[i]);

       }

       i++;

   }

   i = 0;

   while(arr[i]!=0){

    cout<<arr[i]<<" ";

    i++;

}  

}

Explanation:

This defines the function makePositive

void makePositive(int arr[]){

This declares and initializes i to 0

   int i =0;

The following iteration is repeated until the last element in the array

   while(arr[i]!=0){

This checks if current array element is negative

       if(arr[i]<0){

If yes, it changes it to positive

           arr[i] = abs(arr[i]);

       }

The next element is then selected

       i++;

   }

This sets i to 0

   i = 0;

The following iteration prints the updated content of the array

   while(arr[i]!=0){

    cout<<arr[i]<<" ";

    i++;

}  

}

See attachment for full program which includes the main

View image MrRoyal
Thank you for contributing to our discussion. Don't forget to check back for new answers. Keep asking, answering, and sharing useful information. IDNLearn.com is committed to providing accurate answers. Thanks for stopping by, and see you next time for more solutions.