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

4) (20 points) Write a function that takes as input an array Vin and returns a n

ID: 2074162 • Letter: 4

Question

4) (20 points) Write a function that takes as input an array Vin and returns a new array Vout with the following characteristics. The elements of Vin that are greater than zero are replaced by ones. The elements that are less than zero are replaced by -1, and the elements that are equal to zero are left unchanged. You are requested to use a while loop to iterate over the input array Example: If Vin = [4 2 1-3 5-2 0 1 3], then Vout = [1 1 1-1 1-1 0 1 1] Test your function with the following two arrays a. Vin=[-2 0-6 0 4 5 81; b.Vin=[0 0 0 0 12-876-231);

Explanation / Answer

#include <iostream>
#include <cmath>
using namespace std;

double a[1000];
int n;

void array()
{
    int i=0;
    while(i<n)
    {
        if(a[i]>0)
        a[i]=1;
        else if(a[i]<0)
        a[i]=-1;
        else if(a[i]==0)
        a[i]=0;
        i++;

    }

}

int main()
{

    cout<<"enter the length of the array ";
    cin>>n;
    for(int i=0;i<n;i++)
    cin>>a[i];

    array();
    for(int j=0;j<n;j++)
    cout<<a[j]<<" ";
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote