Question

In: Computer Science

This is to be done in C++. Preferably using basic functions. Write a loop that reads...

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).

Solutions

Expert Solution

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 Solutions

Done in C language using mobaxterm if you can but use basic C This program is...
Done in C language using mobaxterm if you can but use basic C This program is broken up into three different functions of insert, delete, and main. This program implements a queue of individual characters in a circular list using only a single pointer to the circular list; separate pointers to the front and rear elements of the queue are not used. The linked list must be circular.      The insert and remove operations must both be O(1)    You...
(C++) Write a nested loop that reads an integer n (n>0) from the keyboard and print...
(C++) Write a nested loop that reads an integer n (n>0) from the keyboard and print out "n" lines as follows: 0 0 1 0 1 2 0 1 2 3 … 0 1 2 3 … n-1
Write a program( preferably in C++)  using the extended Euclidean algorithm to find the multiplicative inverse of...
Write a program( preferably in C++)  using the extended Euclidean algorithm to find the multiplicative inverse of a mod n. Your program should allow user to enter a and n. Note: For this question please make sure the code compiles and runs, it is not copied and pasted from elsewhere( I will be checking!). Thanks
Write a program using c++. Write a program that uses a loop to keep asking the...
Write a program using c++. Write a program that uses a loop to keep asking the user for a sentence, and for each sentence tells the user if it is a palindrome or not. The program should keep looping until the user types in END. After that, the program should display a count of how many sentences were typed in and how many palindromes were found. It should then quit. Your program must have (and use) at least four VALUE...
This is meant to be done in C. Write a program using putchar() and getchar() that...
This is meant to be done in C. Write a program using putchar() and getchar() that reads characters form the keyboard and write to the screen. Every letter that is read should be written three times and followed by a newline. Any newline that is read should be disregarded. All other characters should just be copied to the screen. I am stuck on this any help please?
Multiples of 2 and 3: write a c++ program Using a while loop, write a program...
Multiples of 2 and 3: write a c++ program Using a while loop, write a program that reads 10 integer numbers. The program shall count how many of them are multiples of 2, how many are multiples of 3, and how many are NOT multiples of either 2 or 3. The output should be similar to the one shown below.
Bash Script Write a script using while-do-done loop to convert the kilometers to miles. - Ask...
Bash Script Write a script using while-do-done loop to convert the kilometers to miles. - Ask the user to input the number of kilometers they need to travel. - Display the number of equivalent miles for the number. Use formula, Kilometers = miles/0.62137 - Every time the loop runs, it should ask the user if they want to continue, if user enters “Y” or “y”, then the loop runs again, if user enters “N” or “n”, then stop the loop...
write these programs in c++ using getch() function Write a program that reads 20 integers from...
write these programs in c++ using getch() function Write a program that reads 20 integers from a user using a while loop and determines and prints whether each of those integers is an even number. Write a program that uses a while statement to read integers from a user and prints the sum, average, and largest of these numbers. The input should terminate if the user enters -1.
Using c++, write a program that reads a sequence of characters from the keyboard (one at...
Using c++, write a program that reads a sequence of characters from the keyboard (one at a time) and creates a string including the distinct characters entered and displays the string on the screen. The input terminates once the user enters a white-space character or the user has entered 50 distinct characters. Do not use C-Strings. 2. Use the following function to append character “ch” to the string “s”: s.push_back(ch); 3. Read the input characters one by one, i.e. do...
Q20. Using C++ style string to write a program that reads a sentence as input and...
Q20. Using C++ style string to write a program that reads a sentence as input and converts each word of the sentence following the rule below: For every word in the sentence, the first letter is relocated the end of the word. Then append the string “KPU” to the word. More requirements: All letters in the output should be uppercase. More assumptions: The input sentence contains no non-alphabetic letters Sample Run: Please enter the original sentence: i LOVE to program...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT