Hi guys, In my lab I was given a source code and was tasked to write the function to compute the largest value of a vector, and a for loop to show how elements does the vector has.But the output is now right,it gives me the largest element is zero,and he prints only one element inside a vector,can anyone help?))))
#include <iostream>
#include <vector>
// using std namespace
using namespace std;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// Function Prototypes.
// Make sure to add the proper comments (as per 233 Commenting Guideline)
// in this section
//
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
void read_vector(vector<int>& v);
// Students to write function comments here.
// Hint: This function reads in the size of vector v followed by its elements
// from cin. It assumes that there are no input failues (i.e. all the
// information can be read properly from cin)
void display_vector(const vector<int>& v);
// Students to write function comments here.
// Hint: This function displays vector size and vector elements on cout.
// Output is printed on a single line, which only looks good when
// the number of elements is small.
int largest_element(const vector<int>& v);
// Students to write function comments here.
// Hint: This function returns value of largest element of v.
// Don't call this function if size of v = 0.
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// Main Program
//
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
int main()
{
vector<int> user_vector;
read_vector(user_vector);
// Part A:
// Uncomment the following line once you have written the
// function definitions for display_vector() and largest_element()
display_vector(user_vector);
// Part B:
// Uncomment the following lines once you have written the
// function definitions for largest_element()
if (user_vector.size() > 0)
{
cout << "The largest element value is "
<< largest_element(user_vector) << '.' << endl;
}
else
cout << "Can't compute largest element values." << endl;
return 0;
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// Functions Definitions
//
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
void read_vector(vector<int>& v)
{
int size_input;
cout << "Reading data into a vector<int> " << endl;
cin >> size_input;
if (size_input == 0)
{
cout << "Making vector empty." << endl;
v.resize(0);
}
else
{
v.resize(size_input);
for (int i = 0; i < v.size(); i++)
cin >> v.at(i);
}
}
void display_vector(const vector<int>& v)
{
cout << "size is " << v.size();
if (v.size() == 0)
cout << "; no elements." << endl;
else
{
cout << "; elements are { ";
for (int i=0;i<v.size();i++)//the fuction I've written
{ cout<<v.at(i)<<" ";
cout <<endl; }
}
}
int largest_element(const vector<int>& v)//The function I've written
{
int largest=v[1];
for (int i=0;i<v.size();i++)
{ if (v[i]>largest)
largest=v[i];
}}