I am writing a C++ program to determine the lowest and highest trading prices, and determine the differences between those prices by querying the user for 15 trading prices,computing the difference through the use of adjacent difference, printing out the differences, computing the lowest and highest recorded prices through the use of sort, and print out those values.
I am familiar with the use of sort, but I am not familiar with adjacent_difference. I read my textbook several times, searched the Internet and attempted to alter my code many times, but I am still getting errors and I am lost.
Here is my code:
#include <iostream>
#include <vector>
#include <numeric>
#include <algorithm>
using namespace std;
int main (void)
{
vector <int> p (15);
vector <int>::iterator it1;
cout<<"This program determines the daily differences, lowest and highest of 15 user-entered trading prices."<<endl;
for (int i=0; i<p.size(); i++)
{
cout<<"Please enter a trading price."<<endl;
cin>>p[i];
}
adjacent_difference(p.begin(), p.end(), p(it1));
for (int i=0; i<p.size(); i++)
{
cout<<"The difference in daily trading prices is: "<<adjacent_difference(it1)<<endl;
}
sort (p.begin(), p.end());
cout<<endl;
cout<<"The lowest trading price recorded was "<<p[0]<<"."<<endl;
cout<<"The highest trading price recorded was "<<p[14]<<"."<<endl;
return 0;
}
Here are the errors:
Can someone please explain the syntax involved? Thank you!
The arguments to adjacent_difference
are all iterators. The first two you've supplied correctly: they're the beginning and end of the range of inputs.
The third argument is an output iterator where you're doing to put the results. For example:
#include <vector>
#include <numeric>
// Just some arbitrary inputs:
std::vector<int> inputs { 1, 2, 17, 15, 4, 19, 0, 2};
std::vector<int> results;
std::adjacent_difference(inputs.begin(), inputs.end(),
std::back_inserter(results));
If you're just going to print out the results anyway, you can do that directly by specifying an ostream_iterator for the third argument:
std::adjacent_difference(inputs.begin(), inputs.end(),
std::ostream_iterator<int>(std::cout, "\t"));
Do note, however, that the first value you get from std::adjacent_difference
is the first input. Each subsequent result is the difference from the previous.
Also note that you don't need to completely sort the input just to find the highest and lowest prices. The most obvious way to do that would be with `std::minmax_element, something like this:
auto hilo = std::minmax_element(inputs.begin(), inputs.end());
std::cout << "Lowest price: " << *hilo.first << "\n"
<< "highest price: " << *hilo.second << "\n";
Your call to adjacent_difference
is incorrect. Try this instead:
vector <int> p (15);
vector <int> result(15);
for (int i=0; i < p.size(); ++i) {
cout << "Please enter a trading price." << endl;
cin >> p[i];
}
adjacent_difference(p.begin(), p.end(), result.begin());
for (vector<int>::const_iterator i = result.begin(); i != result.end(); ++i) {
cout << *i << ' ';
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With