Docsity

C++ Basics: Variables, Data Types, I/O, Arithmetic, and Comments, Study notes of Electrical and Electronics Engineering

Partial preview of the text

Download C++ Basics: Variables, Data Types, I/O, Arithmetic, and Comments and more Study notes Electrical and Electronics Engineering in PDF only on Docsity!

Programming Basics  Problem Solving  Programming  Comments  Variables  Data Types  Constants  Assignment Statements  Input and Output  Arithmetic Expressions  Operator Precedence Rules  Basic Program Structure  Software Engineering Tips  Initial Sphere Program  Final Sphere Program  Initial Statistics Program  Final Statistics Program

Problem Solving Steps  Understand the problem to be solved  Decompose problem into pieces you know how to solve  Write computer instructions for each piece  Combine pieces into a single program  Compile, test, and debug program  Use program to solve initial problem

Comments  Comments are used to explain program purpose and methods used  Comments are human readable, but ignored by compiler  Should write comments when designing program  Should also add comments explaining major changes  Two types of comments supported /* Old style C comment that can span multiple lines / // New C++ comment that goes to the end of the line Pay attention: / */ style comment cannot be nested.

Variables  We need to store and access data to perform calculations  Can allocate space in computer for data by declaring a variable  Syntax is: data_type variable_name; (data_types are listed below)  Should choose meaningful names for variables (ie "Age" not "A")  Variable names must start with upper or lower case characters  variable names may contain numbers and selected special characters (underscore)  C++ is case sensitive so "Num" is a different variable from "num" Examples of variable names: a, a3, a_3, ab, a_b, bbb_,

Constants  Certain variables have values which will never change  Can use constants to store these values  Define constants by adding reserved word const before declaration  Also need to provide value at declaration time  Can have constants of any variable data type  Constant declarations must end with a semi-colon // Constant example const int SILLY = 42; const float PI = 3.1415926535; const char YES = 'Y'; Pay attention: If you have the following statements in a program: const int c=5; c=7; You will have the following compiler error when you try to compile the code: assignment of read-only variable ‘c’.

Assignment Statements  Can copy one variable to another using the assignment operator =  Variable on left of = is destination, where data is copied to  Value on right of = is source, where data comes from  Data values should be of the same type  Conversion will occur if values are not of same type  Assignment statements must end with a semi-colon // Assignment example int value; int number; float data; int x, y, z; bool flag; data = 2.158; // data variable now equals 2. value = 17; // value variable now equals 17 number = value; // number variable now equals 17 data = SILLY; // data variable now equals 42. number = PI; // number variable now equals 3 x = y = z = 0; // multiple assignments on one line flag = false; // flag is now set to false Pay attention: You cannot give an integer variable a float value. For example: int a; a = 3.5; // a will have a value 3

Arithmetic Expressions  Often need to combine variables to get new values  Calculations can be expressed as arithmetic expressions  The arithmetic operators are:

  • multiplication / division % modulo (remainder after division)
  • addition
  • subtraction // Arithmetic expressions data = PI * 5; // data now equals 15. number = SILLY / 5; // number now equals 8 cout << 1+2+3; // writes "6" value = data - 5; // value now equals 10 number = 27 % 10; // number now equals 7 Pay attention: (1) % cannot be used on float number; (2) int a, b, c; float d; a=42; b=5; c=a/b; // c = 8 and not 8. d= a/ b; // d=8. d= a * 1.0 / b; // d = 8.

Operator Precedence Rules  Values are computed using "natural" operator precedence rules  Parenthesized expressions ( ) evaluated first from inside out  Multiplication, division, modulo have high precedence  Addition, subtraction have lower precedence  High precedence operations computed before low precedence  Values calculated left to right at same precedence level  Number the order of operations by hand for debugging // Complex expressions number = silly * value - number; data = (number + value) * silly; value = PI / data * PI; cout << 1+2-3+4-5+6; number = (1+2)-((3+4)-(5+6));

Software Engineering Tips  Try to pick variable names which are meaningful  Always initialize variables before you use their values  You should write your comments before you write your code  Add to your program a little at a time (grow your solution)

Initial Sphere Program //--------------------------------------------------- // Purpose: Program to calculate the volume and area // of a sphere of a given radius. // Author: //--------------------------------------------------- int main() { // Local variable declarations // Read input parameters // Calculate volume // Calculate surface area // Print output return 0; }

Initial Statistics Program //--------------------------------------------------- // Purpose: Program to calculate mean and variance // of five input values. // Author: //--------------------------------------------------- int main() { // Local variable declarations // Read input parameters // Calculate mean // Calculate variance // Print statistics return 0; }

Final Statistics Program //--------------------------------------------------- // Purpose: Program to calculate mean and variance // of five input values. // Author: //--------------------------------------------------- #include using namespace std; int main() { // Local variable declarations float num1, num2, num3, num4, num5; // Read input parameters cout << "Enter five numbers: "; cin >> num1 >> num2 >> num3 >> num4 >> num5; // Calculate mean float mean = (num1 + num2 + num3 + num4 + num5) / 5.0; // Calculate variance float dif1 = num1 - mean; float dif2 = num2 - mean; float dif3 = num3 - mean; float dif4 = num4 - mean; float dif5 = num5 - mean; float var = (dif1dif1 + dif2dif2 + dif3*dif

  • dif4dif4 + dif5dif5) / 5.0; // Print statistics cout << "Mean = " << mean << endl; cout << "Variance = " << var << endl;