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...
1. Create a new project. Type in the following program. Add a comment at the top...
1. Create a new project. Type in the following program. Add a comment at the top to include your name and the date. 2. Compile and Run with different values. What data values should you choose? Think about what we discussed in class. 3. Add code to compute the modulus. Hint: you will also have to declare a new variable. //this program demonstrates the use of various operators #include <iostream > using namespace std; int main() { int num1; int...
1. Type in the following program. Compile and run. #include <iostream> using namespace std; int main()...
1. Type in the following program. Compile and run. #include <iostream> using namespace std; int main() { ​cout << "Hello, world." << endl; } Open a Word Document and copy and paste the program and the output. 2. Create a new program: #include <iostream> using namespace std; int main() { ​float num1; ​float num2 ​float avg; ​cout << "Enter two numbers" << endl; ​cin >> num1 >> num2; ​avg = num1 + num2/2; ​cout << "The average is " <<...
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;...
C ++ program that will read in prices and store them into a two-dimensional array //...
C ++ program that will read in prices and store them into a two-dimensional array // It will print those prices in a table form and the lowest price. // EXAMPLE // Input: // Please input the number of rows from 1 to 10 // 2 // Please input the number of columns from 1 to 10 // 3 // // Input price of item (0,0): 2 // Input price of item (0,1): 4 // Input price of item (0,2):...
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;   ...
// This program prints a table to convert numbers from one unit to another. // The...
// This program prints a table to convert numbers from one unit to another. // The program illustrates some implementation techniques. //Include the header file ostream for including all stream. ---------------------------------------------------------*/ #include <iostream> //Provides cout <v1.0> //Include iomanip that is used for set precision. #include <iomanip> //Provides setw function for setting output width <v1.1> //Header file for EXIT_SUCCESS. #include <stdlib.h>// Provides EXIT_SUCCESS <v1.2> //Header file for assert. #include <assert.h>// Provides assert function <1.3> using namespace std; // Allows all standard...
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();...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT