Lab 1 In class we discussed some basic ideas about how to convert (map) an Algor
ID: 3733067 • Letter: L
Question
Lab 1 In class we discussed some basic ideas about how to convert (map) an Algorithm into C++, develop your skills by converting the following algorithm based on an answer (see below) to question 6 from section 3.1 in the textbook. Your solution should be a C++ main.cpp file that looks like this: // Your name: // Lab Number || Date: // Description: #include #include // your descriptive comments int negatives(vector d) { // your work goes here int main() { vector data = {-1, 5, 5, -4, 5, 6, 33, -2, 44 }; // arbitrary numbers int n = negatives(data); std::coutExplanation / Answer
#include <vector>
#include <iostream>
using namespace std;
int negatives(vector<int> d)
{
// declaring varaibles
int count = 0;
vector <int> :: iterator each;
// looping from begining to the end
for (each = d.begin(); each != d.end(); ++each)
{
// if the value is less than 0, it is -ve. So, incrementing count by 1
if(*each < 0)
count ++;
}
return count;
}
int main() {
vector<int> data = {-1, 5, 5, -4, 5, 6, 33, -2, 44};
int n= negatives(data);
cout << "there are " << n << " negative integers ";
return 0;
}
/*SAMPLE OUTPUT
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.