Assignment Demo

// Module 1 Demo // Author: Sam Student // Created on 5-19-2024. // Demonstrate C++ Expressions and Assignment // Demo of setting Video Game name and price and computing the average #include <iostream> using namespace std; int main() { // Create Variables string gameOneName = "Mario Bros."; string gameTwoName = "Zelda"; string gameThreeName = "Halo"; double gameOnePrice = 19.92; double gameTwoPrice = 18.94; double gameThreePrice = 21.95; double average = 0.0; //Process to get the Average Game Price average = (gameOnePrice + gameTwoPrice + gameThreePrice) / 3; cout << "Games:" << endl; cout << gameOneName << ": $" << gameOnePrice << endl; cout << gameTwoName << ": $" << gameTwoPrice << endl; cout << gameThreeName << ": $" << gameThreePrice << endl; cout << "****************************\n"; cout << "Average Price: $" << average << endl; return 0; }

 

image-20240519-162002.png

Explanation:

  1. Comments and Headers:

    // Module 1 Demo // Author: Sam Student // Created on 5-19-2024. // Demonstrate C++ Expressions and Assignment // Demo of setting Video Game name and price and computing the average

    These lines are comments. They provide information about the program, such as its purpose, author, and creation date. Comments are ignored by the compiler and do not affect the program's execution.

  2. Include Directive:

    #include <iostream> using namespace std;

    The #include <iostream> directive tells the compiler to include the standard input-output stream library, which allows us to use cout and other I/O functionalities. using namespace std; allows us to use standard library names without needing to prefix them with std::.

  3. Main Function:

    The main function is the entry point of any C++ program. The code inside this function is executed when the program runs.

  4. Variable Declarations and Initializations:

    • string gameOneName = "Mario Bros."; declares a variable named gameOneName of type string and initializes it with the value "Mario Bros.".

    • Similarly, gameTwoName and gameThreeName are initialized with "Zelda" and "Halo", respectively.

    • double variables gameOnePrice, gameTwoPrice, and gameThreePrice are initialized with the prices of the games.

    • double average = 0.0; declares a variable named average and initializes it with 0.0.

  5. Calculating the Average Price:

    This line calculates the average price of the three games by adding their prices and dividing by 3, and then assigns the result to the average variable.

  6. Outputting the Results:

    • cout << "Games:" << endl; outputs the text "Games:" followed by a newline.

    • cout << gameOneName << ": $" << gameOnePrice << endl; outputs the name and price of the first game, and similarly for the other games.

    • cout << "****************************\\n"; outputs a line of asterisks for visual separation.

    • cout << "Average Price: $" << average << endl; outputs the average price of the games.

  7. Return Statement:

    The return 0; statement indicates that the program has finished successfully.

  8. End of Main Function:

    This closing brace marks the end of the main function.

This program demonstrates basic concepts of C++ such as variable declaration and initialization, arithmetic operations, and console output.

COSC-1336 / ITSE-1302 Computer Science - Author: Dr. Kevin Roark