In: Computer Science
#include #include using namespace std; //Implement function lookUpFirstNeg() here int main() { double *dd, *ddAns; //allocate memory for at least 3 double elements //ensure all elements have been initialized //call function, replace 0 with the proper number of elements pointed to by dd ddAns = lookUpFirstNeg(dd, 0); if(ddAns == NULL) cout << "No negatives found.\n"; else cout << "First negative value is:" << ddAns << endl; //properly deallocate memory allocated above return 0; }
#include <iostream>
using namespace std;
double *lookUpFirstNeg (double * arr, int n){
double *ptr = arr;
for(int i = 0; i<n ; ++i){
if(*(ptr) < 0)
return ptr;
ptr = ptr + 1;
}
return NULL;
}
int main() {
double *dd, *ddAns;
dd = new double[5];
dd[0] = 12.3;
dd[1] = 90;
dd[2] = -7;
dd[3] = 20;
dd[4] = -100;
ddAns = lookUpFirstNeg(dd, 5);
if(ddAns == NULL)
cout << "No negatives found.\n";
else
cout << "First negative value is:" << *ddAns << endl;
delete dd;
return 0;
}
========================================================
SEE OUTPUT, SEE IMAGE FOR BETTER UNDERSTANDING
Thanks, PLEASE COMMENT if there is any concern.