CS201 Lecture 4: Understanding Variables, Expressions & Average Calculation in C++
Reference: Deitel & Deitel – C++ How to Program, Chapter 1, Section 1.22
- How to declare and use variables in C++
- Expressions and operator usage
- Sample program: calculating the average age
π Introduction
In Lecture No. 4 of CS201, we explore the foundational elements of writing practical programs in C++. Our example problem involves calculating the average age of a class of ten students. This task introduces key concepts such as integer variables, arithmetic expressions, and the importance of data types when performing division.
π― Problem Statement
You are required to write a program that calculates the average age of ten students. The program should:
- Prompt the user to enter the age of each student
- Store the ages using integer variables
- Calculate the average age
- Display the average (including decimals)
π ️ Variable Declaration
Since ages are whole numbers (e.g., 18, 21), we will use the int
data type for each student's age.
For simplicity, we will use ten separate variables:
int age1, age2, age3, age4, age5, age6, age7, age8, age9, age10;
➕ Using Expressions & Operators
An expression in C++ is a combination of variables, constants, and operators that evaluate to a value. In our case, we will use the addition operator (+) to compute the total sum of all ages, and the division operator (/) to calculate the average.
π Complete C++ Program
Below is the complete working code for this problem:
#include <iostream>
using namespace std;
int main() {
// Declare age variables
int age1, age2, age3, age4, age5, age6, age7, age8, age9, age10;
// Input: Prompt user for each age
cout << "Enter the ages of 10 students:\n";
cin >> age1 >> age2 >> age3 >> age4 >> age5
>> age6 >> age7 >> age8 >> age9 >> age10;
// Calculate total
int total = age1 + age2 + age3 + age4 + age5 +
age6 + age7 + age8 + age9 + age10;
// Calculate average using float to preserve decimal
float average = static_cast<float>(total) / 10;
// Output result
cout << "Average age is: " << average << endl;
return 0;
}
π§ Explanation
The user is asked to input ten values. These values are stored in integer variables. We then sum all the ages and store the result in the total
variable.
To calculate the average accurately (i.e., not discard decimals), we use static_cast<float>
to convert the total into a floating-point number before dividing by 10.
π Why Use static_cast<float>
?
If we divide two integers in C++, the result is also an integer. This means 173 / 10
would return 17
instead of 17.3
.
To get the correct average with decimal places, at least one value in the division must be a float
or double
. That's why we use:
float average = static_cast<float>(total) / 10;
⚠️ Common Mistakes to Avoid
- Using integer division without casting – leads to incorrect average
- Forgetting to initialize all variables before usage
- Not validating input (e.g., negative ages)
π Learning Outcomes
- Understanding the difference between integer and floating-point arithmetic
- Using arithmetic operators and expressions effectively
- Writing clean, readable code in C++
π Additional Tips
If you're working with a larger number of students, using arrays or loops is more efficient than declaring multiple variables. However, since this lecture focuses on basic syntax, the ten-variable method is used for clarity.
π Homework Exercise
Modify the program to:
- Accept the number of students as input
- Use a loop to gather age data
- Calculate and display the average age
π Conclusion
This lecture introduced how variables, arithmetic operators, and typecasting work together in a C++ program. You also learned how to read multiple inputs and ensure accurate calculations using type conversion. These fundamentals will serve as building blocks for more advanced topics like loops, arrays, and functions.
Written by Entermindy | Virtual University | CS201 Lecture Notes