Thursday, December 13, 2012

C++ TUTORIAL FOR BEGINNERS: Variables and their practical applications in calculations


What are C++ variables?

Variables are simply values that change as opposed to constants which do not change.

To illustrate the usefulness of variables, let us use them in an example to calculate the net profit of a video game store.

Given:
On a certain week, a video game store has a total sales of $9000 and total expenses of $6000. On the following week, total sales rose to $9500 while the total expenses increased to $6500. Calculate the net profit of the video game store on those two weeks.  

Objective: How to calculate the net profit of a given video game store
Directive: #include <iostream>
Namespace: std
Function: main
Variables: sales, expenses, profit
Data types of variables: int (integer)
Statements: cout, return
Output:
Net profit = 6000

C++ source code:

/*
PROGRAM 2: Calculation of net profit of a video game store
AUTHOR: eternaltreasures
DATE: 2010 August 26
*/

#include <iostream>
using namespace std;
int main ()
{

int sales, expenses, profit;

//First week:
sales = 9000;
expenses = 6000;

//Second week:
sales = sales + 9500;
expenses = expenses + 6500;

//Net profit of the video game store:
profit = sales - expenses;
cout << "Net profit = ";
cout << profit;

return 0;
}


Notes:
* During the first week, initial value of the variable sales is 9000 and the variable expenses is 6000.
* On the second week, the value of the variable sales is 9000 + 9500 = 18500.
* Also on the second week, the value of the variable expenses is 6000 + 6500 = 12500.
* Variables change values depending on the operations done to them on the program.

No comments:

Post a Comment