Question

Write a program that accepts as input the mass, in grams, and density, in grams per...

  • Write a program that accepts as input the mass, in grams, and density, in grams per cubic centimeters, and outputs the volume of the object using the formula: volume = mass / density.

    Format your output to two decimal places.

    ** Add Comments


    ** Print Name and Assignment on screen


    ** Date


    ** Submit .cpp file.

  • Demo

    // This program uses a type cast to avoid an integer division.

    #include <iostream> // input - output stream

    #include <fstream> //working file stream

    #include <iomanip> // format output stream OR format file output stream

    #include <string>

    #include <cmath>

    using namespace std;

    int main()

    {

       //------------------------------------------

        cout << "pow(2, 2) : " << pow(2, 2) << endl;

        

        //------------------------------------------

        //ifstream input; //cin to file

        //ofstream output; // cout to file

        //------------------------------------------

      

        int books, months;

        double booksPerMonth;

        // Get user inputs

       cout << "How many books do you plan to read? ";

        cin >> books;

        cout << "How many months will it take you to read them? ";

        cin >> months;

        // Compute and display books read per month

        booksPerMonth = static_cast<double>(books) / months;

        cout << "That is " << booksPerMonth << " books per month.\n";

        booksPerMonth = (books) / months;

        cout << "That is " << booksPerMonth << " books per month.\n";

        /*--------------------------------------------------------------*/

        int number = 65;

        // Display the value of the number variable

        cout << "Display the value of the number variable: " << number << endl;

        // Use a type cast to display the value of number

        // converted to the char data type

        cout << static_cast<char>(number) << endl;

        cout << static_cast<int>('A') << endl;

        /*--------------------------------------------------------------*/

      

        int num1 = 2897, num2 = 5, num3 = 837,

            num4 = 34, num5 = 7, num6 = 1623,

            num7 = 390, num8 = 3456, num9 = 12;

        // Display the first row of numbers

        cout << num1 << " " << num2 << " " << num3 << endl;

       

        // Display the second row of numbers

        cout << num4 << " " << num5 << " " << num6 << endl;

        // Display the third row of numbers

        cout << num7 << " " << num8 << " " << num9 << endl << endl;

        /*-------------------------FORMAT USING setw( int )-------------------------------------*/

        // Display the first row of numbers

        cout << setw(6) << num1 << setw(6) << num2 << setw(6) << num3 << endl;

        // Display the second row of numbers

        cout << setw(6) << num4 << setw(6) << num5 << setw(6) << num6 << endl;

        // Display the third row of numbers

        cout << setw(6) << num7 << setw(6) << num8 << setw(6) << num9 << endl;

        /*--------------------------------------------------------------*/

        int intValue = 3928;

        double doubleValue = 91.5;

        string stringValue = "Jill Q. Jones";

        cout << setfill('*');

        cout << "(" << setw(5) << intValue << ")" << endl;

        cout << "(" << setw(8) << doubleValue << ")" << endl;

        cout << "(" << setw(16) << stringValue << ")" << endl;

        cout << setfill(' ');

        cout << "(" << setw(5) << intValue << ")" << endl;

        cout << "(" << setw(8) << doubleValue << ")" << endl;

        cout << "(" << setw(16) << stringValue << ")" << endl;

        ///*--------------------------------------------------------------*/

       

         double x = 7;

        cout << x << endl;

        cout << showpoint << x << endl;

        cout << fixed << x << endl;

        cout << setprecision(2) << fixed << x << endl;

        /*--------------------------------------------------------------*/

        string name;

        string city;

        cout << "Please enter your name: ";

        getline(cin, name);

      

        cout << "Enter the city you live in: ";

        getline(cin, city);

        cout << "Hello, " << name << endl;

        cout << "You live in " << city << endl;

        /*--------------------------------------------------------------*/

       string firstName , lastName, fullName;

      

        cout << "Please enter your first name: ";

        cin >> firstName;

        cout << "Please enter your last name: ";

        cin >> lastName;

        fullName = firstName + " " + lastName;

        cout << fullName << endl;

      

        system("pause");

        return 0;

    }


Homework Answers

Answer #1

Here is your C++ code. If you have any query then ask me in comment section. Please give a thumbs up if you find the answer helpful.

#include<iostream>                     //for cin and cout
#include<iomanip>                 //for setprecision() and fixed
using namespace std;

int main()
{
        //Uncomment below part to show Name and Date
        /*
        cout<<"Name : Your_Name_Here"<<endl;
        cout<<"Date : Date_Here<<endl;
        */
        
        float mass, volume, density;                    //variables to be used
        
        cout<<"Enter the mass of object in grams : ";
        cin>>mass;                                                                //reading mass as an user input
        cout<<"\nEnter the density of object in grams per cubic centimeters : ";
        cin>>density;                                                     //reading density as an user input
        
        volume = mass/density;                                  //calculating volume according to the formula
        cout<<fixed<<"Volume of the object is : "<<setprecision(2)<<volume<<" cubic centimeters.";
        /*
        fixed is a Fixed Floating Point Notation.
        The value is represented with exactly as many digits in the exponent part as specified 
        by the precision field (setprecision()).
        */
}
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Identify and explain the kind of error in the following 3 sets of statements a, b...
Identify and explain the kind of error in the following 3 sets of statements a, b and c (Write None if no error exists). a. //Output the content of variable x; int X; X = 10; cout << x << endl; b. //Have the user enter in a gpa char gpaValue; cin>>gpaValue; c. //Find the average of 3 numbers float num1, num2, num3;                               cin>>num1>>num2>>num3;                               float average = num1 + num2 + num3 / 3;
Consider the following program: #include #include #include using namespace std; int main() { int num1; int...
Consider the following program: #include #include #include using namespace std; int main() { int num1; int num2; cout << fixed << showpoint << setprecision(2); cout << "Enter two integers: "; cin >> num1 >> num2; cout << endl; if (num1 != 0 && num2 != 0) cout << sqrt(abs(num1 + num2) + 0.5) << endl; else if (num1 != 0) cout << floor(num1 + 0.5) << endl; else if (num2 != 0) cout << ceil(num2 + 0.5) << endl; else...
can someone edit my c++ code where it will output to a file. I am currently...
can someone edit my c++ code where it will output to a file. I am currently using xcode. #include <iostream> #include <cctype> #include <cstring> #include <fstream> using namespace std; bool inputNum(int [],int&,istream&); void multiply(int[],int,int[],int,int[],int&); void print(int[],int,int,int); int main() {ifstream input; int num1[35],num2[35],len1,len2,num3[60],len3=10,i; input.open("multiplyV2.txt"); //open file if(input.fail()) //is it ok? { cout<<"file did not open please check it\n"; system("pause"); return 1; }    while(inputNum(num1,len1,input)) {inputNum(num2,len2,input); multiply(num1,len1,num2,len2,num3,len3); print(num1,len1,len3,1); print(num2,len2,len3,2); for(i=0;i<len3;i++) cout<<"-"; cout<<endl; print(num3,len3,len3,1); //cout<<len1<<" "<<len2<<" "<<len3<<endl; cout<<endl;    } system("pause"); } void...
Write a program that prompts the user to input a string and outputs the string in...
Write a program that prompts the user to input a string and outputs the string in uppercase letters. (Use dynamic arrays to store the string.) my code below: /* Your code from Chapter 8, exercise 5 is below. Rewrite the following code to using dynamic arrays. */ #include <iostream> #include <cstring> #include <cctype> using namespace std; int main() { //char str[81]; //creating memory for str array of size 80 using dynamic memory allocation char *str = new char[80]; int len;...
Lab 6    -   Program #2   -   Write one number to a text file. Use the write()...
Lab 6    -   Program #2   -   Write one number to a text file. Use the write() and read() functions with binary                                                        data, where the data is not char type.              (Typecasting is required) Fill in the blanks, then enter the code and run the program. Note:   The data is int type, so typecasting is            required in the write() and read() functions. #include <iostream> #include <fstream> using namespace std; int main() {    const int SIZE = 10;   ...
How to stop the program from exiting after display detail. When there is food detail, it...
How to stop the program from exiting after display detail. When there is food detail, it will display and exit the program. What can i do to make it not exit the program and back to main menu. #include <iostream> #include <iomanip> #include<string.h> using namespace std; struct food{ int order_id; string food_code,flavor,customer_id; string address,name; int weight,unit_price,qty,contact_number; struct food *next; };    class Foodsystem{ food *head,*temp,*temp2,*end; static int id;    public: Foodsystem(){ head=NULL;end=NULL;} void Place_Order(); void View_food_details(); void Modify_food_details(); void Delete_food_details();...
Write a C++ program that processes orders for our company Widgets and More. Your program will...
Write a C++ program that processes orders for our company Widgets and More. Your program will read the product code and quantity for all the products in an order from a file, order.txt. You will be given a sample order.txt with the starter code. There are two items on each line of the file the first is a letter that represents the product and the second is an integer representing the number bought. Based on the product and quantity, you...
Write a 4-6 sentence summary explaining how you can use STL templates to create real world...
Write a 4-6 sentence summary explaining how you can use STL templates to create real world applications. In your summary, provide an example of a software project that you can create using STL templates and provide a brief explanation of the STL templates you will use to create this project. After that you will implement the software project you described . Your application must be a unique project and must incorporate the use of an STL container and/or iterator and...
Write a C++ program to run a menu-driven program with the following choices: Compute the factorial...
Write a C++ program to run a menu-driven program with the following choices: Compute the factorial of a number Compute the alternating factorial of a number Quit #include <iostream> using namespace std; void getValidUserInputPosNumGT0 (int *a) { int num; cout << "Enter in a positive number greater than 0... "; cin >> *a; } long double factorial (int num) { int fact = 1; while (num > 1) { fact *= num; num--; } return fact; } long double AlternatingFactorial...
C++ Write a recursive function that reverses the given input string. No loops allowed, only use...
C++ Write a recursive function that reverses the given input string. No loops allowed, only use recursive functions. Do not add more or change the parameters to the original function. Do not change the main program. I had asked this before but the solution I was given did not work. #include #include using namespace std; void reverse(string &str) { /*Code needed*/ } int main() {    string name = "sammy";    reverse(name);    cout << name << endl; //should display...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT