language c++ Hi i need help getting the out below i have include my code as well
ID: 3725008 • Letter: L
Question
language c++
Hi i need help getting the out below i have include my code as well i have my main and then my functions but i dont know how to get the largest value of a vector i am stuck there. i need help making that and if you could please explain whats going on in that piece of code. also please correct any mistakes you see in my code. its a makefile .
Expected Output
My main
Functions
Calling function PopulateArray .. . Enter a unique integer value between 1 to 9 (q=quit) 2 Enter a unique integer value between 1 to 9 (q=quit) . 6 Enter a unique integer value between 1 to 9 (q=quit) . 7 Enter a unique integer value between 1 to 9 (q=quit) . 9 Enter a unique integer value between 1 to 9 (q=quit) 1 Enter a unique integer value between 1 to 9 (q-quit) 3 Enter a unique integer value between 1 to 9 (q-quit) Calling function GetLargest . .. The largest element value in the vector is 9Explanation / Answer
To find largest value in the array/vector, first you need to declare a variable and assigne it with first value of vector
and then start traversing vector from index 1 or second element and compare it with alreadey existing value
in the varialble largest, if current vector value is larger than variable largest update it.
largest=vec.at(0);
if(vec.at(x)> largest)
{
largest=vec.at(x);
}
Sample Input/output:
Calling function populateArray...
Enter a unique integer value between 1 to 9 (q=quit).
2
Enter a unique integer value between 1 to 9 (q=quit).
6
Enter a unique integer value between 1 to 9 (q=quit).
7
Enter a unique integer value between 1 to 9 (q=quit).
9
Enter a unique integer value between 1 to 9 (q=quit).
1
Enter a unique integer value between 1 to 9 (q=quit).
3
Enter a unique integer value between 1 to 9 (q=quit).
q
Calling function Getlargest...
The largest element value in the vector is 9.
###################main program *********************************
#include<iostream>
#include "VectorFunction.h"
#include<vector>
using namespace std;
int main()
{
vector<int> v;
int getlargest;
cout<<"Calling function populateArray..."<<endl;
//Calling function and taking vector back.
v=PopulateVector(v);
cout<<"Calling function Getlargest..."<<endl;
cout<<"The largest element value in the vector is "<<Getlargest(v)<<"."<<endl;
return 0;
}
************************Functions***********************
#include<iostream>
#include<vector>
using namespace std;
vector<int> PopulateVector(vector<int> v)
{
char choice;
while(true)
{
cout<<"Enter a unique integer value between 1 to 9 (q=quit)."<<endl;
cin>>choice;
if(choice=='q')
{
break;
}
else
{
//(int) char return ascii value of char like 0 has 48, 1 has 49 ascii value
//So subtract 48
int x=(int)choice-48;
if(x>=1 && x<=9)
{
v.push_back(x);
}
}
}
//Returning vector
return v;
}
int Getlargest(vector<int>v)
{
//Assign minimum value to the largest
int largest=v.at(0);
int x=0;
while(x<v.size())
{
//If current element is greater than already existing largest then update it
if(v.at(x)>largest)
{
largest=v.at(x);
}
x++;
}
return largest;
}
**********************************************************************************
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.