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;
}
Explanation:
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.
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 usecout
and other I/O functionalities.using namespace std;
allows us to use standard library names without needing to prefix them withstd::
.Main Function:
The
main
function is the entry point of any C++ program. The code inside this function is executed when the program runs.Variable Declarations and Initializations:
string gameOneName = "Mario Bros.";
declares a variable namedgameOneName
of typestring
and initializes it with the value "Mario Bros.".Similarly,
gameTwoName
andgameThreeName
are initialized with "Zelda" and "Halo", respectively.double
variablesgameOnePrice
,gameTwoPrice
, andgameThreePrice
are initialized with the prices of the games.double average = 0.0;
declares a variable namedaverage
and initializes it with 0.0.
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.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.
Return Statement:
The
return 0;
statement indicates that the program has finished successfully.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