This is to be done in C++. Preferably using basic functions. Write a loop that r
ID: 3792316 • Letter: T
Question
This is to be done in C++. Preferably using basic functions.
Write a loop that reads one double each time around.
Define two variables to keep track of which is the smallest and which is the largest value you have seen so far.
Each time through the loop write out the value entered. If it's the smallest so far, write the smallest so far after the number. If it is the largest so far, write the largest so far after the number.
Let the user add a unit to each double entered; that is, enter values such as 10cm, 2.5in, 5ft, or 3.33m.
Accept the four units: cm, m, in, ft.
Assume conversion factors 1m == 100cm, 1in == 2.54cm, 1ft == 12in.
Read the unit indicator into a string.
You may consider 12 m (with a space between the number and the unit) equivalent to 12m (without a space).
Reject values without units or with "illegal" representations of units, such as y, yard, meter, km, and gallons.
Keep track of the sum of values entered (as well as the smallest and the largest) and the number of values entered. When the loop ends, print the smallest, the largest, the number of values, and the sum of values. note that to keep the sum, you have to decide on a unit to use for that sum; use meters.
Keep all the values entered (converted into meters) in a vector. At the end, write out those values.
Before writing out the values from the vector, sort them (that'll make them come out in increasing order).
1. Write a loop that reads one double each time around o Define two variables to keep track of which is the smallest and which is the largest value you have seen so far. o Each time through the loop write out the value entered If it's the smallest so far, write the smallest so far after the number. If it is the largest so far, write the largest so far after the number 2. Let the user add a unit to each double entered: that is, enter values such as 10cm, 2.5in, 5ft, or 3.33m. o Accept the four units: cm, m, in, ft. o Assume conversion factors 1m 100cm, lin 2.54cm, 1ft 12in. o Read the unit indicator into a string. o You may consider 12 m with a space between the number and the unit equivalent to 12m without a space 3. Reject values without units or with "illega representations of units, such as y, yard, meter, km, an gallons. d 4. Keep track of the sum of values entered (as well as the smallest and the larges and the number of values entered. When the loop ends, print the smallest, the largest, the number of values, and the sum of values. note that to keep the sum, you have to decide on a un to use for that sum use meters. 5. Keep all the values entered (converted into meters in a vector. At the end, write out those values 6. Before writing out the values from the vector, sort them (that'll make them come out in increasing order).Explanation / Answer
C++ code:
#include <bits/stdc++.h>
using namespace std;
#include <string>
int main()
{
int n = 5;
int i = 0;
std::vector<float> myvector;
while(i< n)
{
string s ;
float minn = FLT_MIN;
float maxx = FLT_MAX;
cout << "Please enter new value" << endl;
getline(cin , s);
char c = s[s.length() - 1];
string number;
if(c == 'm')
{
c = s[s.length() - 2];
if(c == 'c')
{
c = s[s.length() - 3];
if(c == ' ')
{
number = s.substr(0,s.length() - 3);
float f = atof(number.c_str());
myvector.push_back(f/100.0);
}
else
{
number = s.substr(0,s.length() - 2);
float f = atof(number.c_str());
myvector.push_back(f/100);
}
}
else
{
c= s[s.length() - 2];
if( c == ' ')
{
number = s.substr(0,s.length() - 2);
float f = atof(number.c_str());
myvector.push_back(f);
}
else
{
number = s.substr(0,s.length() - 1);
float f = atof(number.c_str());
myvector.push_back(f);
}
}
}
else if( c == 't')
{
c = s[s.length() - 3];
if( c == ' ')
{
number = s.substr(0,s.length() - 3);
float f = atof(number.c_str());
myvector.push_back(f*(0.3048));
}
else
{
number = s.substr(0,s.length() - 2);
float f = atof(number.c_str());
myvector.push_back(f*(0.3048));
}
}
else if(c == 'n')
{
c = s[s.length() - 3];
if(c == ' ')
{
number = s.substr(0,s.length() - 3);
float f = atof(number.c_str());
myvector.push_back(f*0.0254);
}
else
{
number = s.substr(0,s.length() - 2);
float f = atof(number.c_str());
myvector.push_back(f*0.0254);
}
}
else
{
cout << "Please enter values in given units only: cm, m, in, ft!" << endl;
i--;
}
i++;
}
float summ = 0;
int size = myvector.size();
for (int i = 0; i < size; ++i)
{
summ = summ + myvector[i];
}
cout << "Minimum: " << *min_element(myvector.begin(), myvector.end()) << " m" << endl;
cout << "Maximum: " << *max_element(myvector.begin(), myvector.end()) << " m"<< endl;
cout << "Number of Elements: " << myvector.size() << endl;
cout << "Sum: " << summ << " m"<< endl;
for (int i = myvector.size() - 1; i > 0 ; i--)
{
for (int j = 0; j < i; j++)
{
if(myvector[j] > myvector[j+1] )
{
float tmp = myvector[j];
myvector[j] = myvector[j+1];
myvector[j+1] = tmp;
}
}
}
cout << "Overall vector" << endl;
for (int i = 0; i < size; ++i)
{
cout << myvector[i] << " ";
}
return 0;
}
Sample Output:
Please enter new value
12m
Please enter new value
120cm
Please enter new value
121ft
Please enter new value
122in
Please enter new value
10m
Minimum: 1.2 m
Maximum: 36.8808 m
Number of Elements: 5
Sum: 63.1796 m
Overall vector:
1.2 3.0988 10 12 36.8808
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.