Short Pregramming Problems (8 points) (4 poins) Write an iterative (loop) functi
ID: 3695482 • Letter: S
Question
Short Pregramming Problems (8 points) (4 poins) Write an iterative (loop) function that reverses a vector of elements in type string, by swapping corresponding elements in the vector. Youcannot ase oxdditiomal vectors in your solwtion or call a fionction of vector to directly reverse t 1. void ReverseString Vectorf 2. (4 point) Define a function CoordUpdate0 that transforms its first two input parameters xlon and yin into two output parameters xOut and yOut. The function returns void. The update is defined as new(20.old +3). Ex: Ifx1n-3and yin = 4, then xOut is 63 and yout is 83. See example call to CoordUpdate) below in function maino. Winclude ciostream using namespace std; int maino ( int xValNew-0 int yValNew = 0; CoordUpdate(3, 4, xValNew, yValNew) coutExplanation / Answer
1. CoordUpdate function:
-----------------------------------------------------
//function to update coordinates. Output parameters to be passed by reference
void CoordUpdate(int xln,int yln,int &xOut,int &yOut)
{
xOut=20*xln+3;
yOut=20*yln+3;
}
--------------------------------------------
2. ReverseStringVector function:
--------------------------------------------------
//function to reverse a string vector
void ReverseStringVector(vector<string>& v)
{
int length=v.size();
string temp;
for(int i=0,j=length;i<length/2;i++,j--)
{
if(j>0)
{
temp=v.at(i);
v.at(i)=v.at(j-1);
v.at(j-1)=temp;
}
}
}
-------------------------------------------------------------
3. SwapStrings function:
------------------------------------------------------------
//function to swap two string
void SwapStrings(string &left, string& right)
{
string temp;
temp=left;
left=right;
right=temp;
}
-----------------------------------------------
4. maximum value in a vector:
----------------------------------------------
int maxVal=v.at(0);
for(int i=0;i<v.size();i++)
{
if(v.at(i) > maxVal)
{
maxVal=v.at(i);
}
}
cout<<"Max value is:"<<maxVal<<endl;
--------------------------------------
5. Multiple choice questions
1) D
2) B
3) C
4) D
5) C
6. main function:
--------------------------------------------
//main function
int main() {
int xValNew=0;
int yValNew=0;
CoordUpdate(3,4,xValNew,yValNew);
cout << "(3,4) becomes " << "(" << xValNew << "," << yValNew << ")" << endl;
std::vector<string> v;
std::vector<string>::iterator it;
it=v.begin();
it=v.insert(it,"string1");
it=v.begin();
v.insert(it,1,"string2");
it=v.begin();
v.insert(it,1,"string3");
it=v.begin();
v.insert(it,1,"string4");
it=v.begin();
v.insert(it,1,"string5");
cout<<"Before reversing..."<<endl;
for(int i=0;i<v.size();i++)
cout<<v.at(i)<<endl;
ReverseStringVector(v);
cout<<"After reversing..."<<endl;
for(int i=0;i<v.size();i++)
cout<<v.at(i)<<endl;
string left="Fresno";
string right="Merced";
SwapStrings(left,right);
cout<<left<<" "<<right<<endl;
return 0;
}
---------------------------------------------
7. Output:
-----------------------------------------------
(3,4) becomes (63,83)
Before reversing...
string5
string4
string3
string2
string1
After reversing...
string1
string2
string3
string4
string5
Merced Fresno
-------------------------------------------
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.